Skip to content

Instantly share code, notes, and snippets.

@pca2
Last active January 4, 2024 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pca2/d8d5375d19ad8ed530b889b1b77e1275 to your computer and use it in GitHub Desktop.
Save pca2/d8d5375d19ad8ed530b889b1b77e1275 to your computer and use it in GitHub Desktop.
XBar/BitBar Monitor
#!/bin/bash
# xbar seems to freeze when computer goes to sleep. this monitors that and restarts it if detected
# Parameters
log_file="/tmp/xbar.log"
command_to_run="/Applications/xbar.app/Contents/MacOS/xbar"
# Check if log file exists
if [ ! -f "$log_file" ]; then
touch "$log_file"
fi
# Function to check if the command is already running
is_command_running() {
pgrep -f "$command_to_run" > /dev/null
return $?
}
# Function to run the command
run_command() {
if is_command_running; then
echo "$(date +"%Y/%m/%d %H:%M:%S") - Command is already running." >> $log_file
return 0
fi
$command_to_run &> $log_file &
echo $!
}
# Start the command and get PID
pid=$(run_command)
# Monitor log file for ERR
while true; do
if grep --line-buffered -q "ERR: signal: killed" $log_file; then
echo "$(date +"%Y/%m/%d %H:%M:%S") - ERROR detected, restarting" >> $log_file
# Kill original command and re-run
kill -TERM $pid
sleep 2
kill -0 $pid 2>/dev/null && kill -KILL $pid
> $log_file
pid=$(run_command)
fi
sleep 5 # Wait for log file to update
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment