(需要先开启screen,设置会话名称为Kuzco_init: screen -S Kuzco_init)
#!/bin/bash
SESSION_NAME="Kuzco_init"
CHECK_INTERVAL=10
KUZCO_COMMAND="kuzco worker start --worker M3EOYBnC_xm5MRQaslSdC --code c0ab6b41-ae63-442d-ae73-42557bb93fe4"
TEMP_LOG="/tmp/screen.log"
LAST_LOG="/tmp/last_screen.log"
# Function to check if the screen session output has been updated
check_update() {
if screen -ls | grep -q "$SESSION_NAME"; then
# Capture current output
screen -S "$SESSION_NAME" -p 0 -X hardcopy "$TEMP_LOG"
if [ -f "$LAST_LOG" ]; then
# Compare current output with last output
if cmp -s "$TEMP_LOG" "$LAST_LOG"; then
return 1 # No update
else
cp "$TEMP_LOG" "$LAST_LOG"
return 0 # Updated
fi
else
# First run, initialize the last log file
cp "$TEMP_LOG" "$LAST_LOG"
return 0 # Assume updated
fi
else
echo "Screen session $SESSION_NAME does not exist."
return 1
fi
}
# Function to run the command in the screen session
run_command() {
echo "No update detected. Running command in screen session..."
screen -S "$SESSION_NAME" -p 0 -X stuff "$KUZCO_COMMAND$(printf \\r)"
}
echo "Starting monitoring script for screen session $SESSION_NAME"
# Main loop
while true; do
sleep "$CHECK_INTERVAL"
if ! check_update; then
run_command
fi
done