I've tried reading multiple posts that seem related to this question, but I struggle to follow some of them and others don't seem to apply precisely to my problem. Please direct me to a post I may have missed otherwise.
I am running multiple rclone commands to sync files between my local drive and google drive, and I'm trying to adapt the script to monitor each rclone process and notify me (a) immediately when one of them throws an error, and (b) after all of them have finished successfully. I found something handy called 'terminal-notifier' for the desktop notifications. It's on homebrew.
I adapted this useful script and the code below is what I've thrown together. The exit codes for rclone's bisync command are 0, 1, and 2. It seems I'm getting an error for the 0 exit code for some reason, but I don't totally understand what's going on; e.g. line 6: wait: pid 44097 is not a child of this shell
. I still haven't figured out how to code the final success message after all rclone processes succeed.
tldr:
I am trying to fix the below script that monitors the exit codes for multiple background processes (on macOS). Currently, I get pid is not a child of this shell
errors, and I also can't figure out how to code a success message when all processes succeed.
#!/bin/bash
non_blocking_wait() {
PID=$1
if ! ps -p $PID > /dev/null; then
wait $PID
CODE=$?
else
CODE=127
fi
return $CODE
}
PIDS=()
(rclone bisync ~/Documents \
gdrive:docs.rinkjames/Documents \
-vc \
--remove-empty-dirs \
--check-filename .rclone_bisync_check_access \
--filters-file ~/.config/rclone/bisync/filters \
--drive-skip-gdocs --drive-skip-shortcuts \
>> ~/.config/rclone/bisync/log_documents 2>&1) &
pid_rclone_docs=$!
PIDS[$!]=1
(rclone bisync ~/Desktop \
gdrive:docs.rinkjames/Desktop \
-vc \
--force \
--remove-empty-dirs \
--check-filename .rclone_bisync_check_access \
--filters-file ~/.config/rclone/bisync/filters \
--drive-skip-gdocs --drive-skip-shortcuts \
>> ~/.config/rclone/bisync/log_desktop 2>&1) &
pid_rclone_desk=$!
PIDS[$!]=1
(rclone bisync ~/Google\ Drive \
gdrive:drive \
-vc \
--remove-empty-dirs \
--check-filename .rclone_bisync_check_access \
--filters-file ~/.config/rclone/bisync/filters \
--drive-skip-gdocs --drive-skip-shortcuts \
>> ~/.config/rclone/bisync/log_drive 2>&1) &
pid_rclone_drive=$!
PIDS[$!]=1
while [ ${#PIDS[@]} -ne 0 ]; do
date
for PID in ${!PIDS[@]}; do
non_blocking_wait $PID
CODE=$?
if [ $CODE -ne 127 ] && [ $CODE -ne 0 ] && [ $PID = $pid_rclone_docs ]; then
terminal-notifier -message "rclone_docs terminated with errors" -group docs
unset PIDS[$PID]
elif [ $CODE -ne 127 ] && [ $CODE -ne 0 ] && [ $PID = $pid_rclone_desk ]; then
terminal-notifier -message "rclone_desktop terminated with errors" -group desk
unset PIDS[$PID]
elif [ $CODE -ne 127 ] && [ $CODE -ne 0 ] && [ $PID = $pid_rclone_drive ]; then
terminal-notifier -message "rclone_drive terminated with errors" -group drive
unset PIDS[$PID]
fi
done
sleep 2
done