A method for installed playback systems. Set as a login item for macOS. Adjust the variables as necessary. The watcher script will check that the defined process exists. If not, it will relaunch the target file. Based on the work of Jim Bell for Generativity by Fernanda D’Agostino, Suyama Space, Seattle WA, 2016. Refined and expanded since then.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
#!/bin/bash # # ---------------------------------------------------------------------------- # FILE: processWatcher_TEMPLATE.sh # AUTHOR: Brendan Hogan | Kreiva Laboratories # DATE: 2023-01-26 # # DESCRIPTION # Monitor for a defined process(es) in macOS to ensure that the process or # processes are valid. If a non-zero reply to the query is returned, the script # will re-launch the defined process or processes. # This script is intended for installation media compositions where continuous # or scheduled operation is required. # # INSTRUCTIONS # Define your first process name as 'firstProcess' and first file name as # 'firstFile'. Define your second process name/file (if applicable) in a # similar manner. Use Activity Monitor to identify process names. # # NOTE: File pathways with spaces are enclosed in single quotes. /usr/bin/open # commands where the file name contains spaces are enclosed in double quotes. # Add additional processes/files as required. Make the script executable, with # Terminal as the default application. Set the shell file as a Login Item. # ---------------------------------------------------------------------------- # # VARIABLES FIRSTPROCESS=someProcess FIRSTFILE=/path/to/file/noSpaces.extension SECONDPROCESS=someOtherProcess SECONDFILE='/path/to/file/with spaces.extension' #MAKEFRONT=/Users/USERNAME/Desktop/makeFront.app # use to bring an app to front LAUNCHSLEEP=30 # sleep time in seconds #FRONTSLEEP=10 # use to sleep the launch time of makeFront LOOPSLEEP=10 # sleep time between checks # # Set Terminal window to 40 x 80 printf '\e[8;40;80t' clear # MAIN RUNNER # Print info to Terminal echo "------------------------------------------------------------------------" echo " Monitor $FIRSTPROCESS and $SECONDPROCESS and run watcher loops. " echo " Restart $FIRSTPROCESS and/or $SECONDPROCESS if they crash. " echo " Waiting for 10 seconds. " echo " -----------------------------------------------------------------------" sleep 10 # LAUNCH APPLICATION(S): /usr/bin/open $FIRSTFILE sleep $LAUNCHSLEEP /usr/bin/open $SECONDFILE #sleep $FRONTSLEEP #/usr/bin/open $MAKEFRONT # # RUN PRIMARY LOOP echo " Starting $FIRSTPROCESS watcher loop. " until [ 0 = 1 ] do /usr/bin/killall -0 $FIRSTPROCESS if [ $? = 0 ] then echo "$FIRSTPROCESS is active. Checking again in $LOOPSLEEP seconds..." else echo "" echo "" echo "$(date): Error: Process not found - restarting $FIRSTPROCESS" echo "" echo "" /usr/bin/open $FIRSTFILE fi sleep $LOOPSLEEP done & # RUN SECONDARY LOOP echo " Starting $SECONDPROCESS watcher loop. " until [ 0 = 1 ] do /usr/bin/killall -0 $SECONDPROCESS if [ $? = 0 ] then echo "$SECONDPROCESS is active. Checking again in $LOOPSLEEP seconds..." else echo "" echo "" echo "$(date): Error: Process not found - restarting $SECONDPROCES" echo "" echo "" /usr/bin/open "$SECONDFILE" fi sleep $LOOPSLEEP done |
Stopping the process watcher loop (AppleScript):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
-- processWatcher_Killer.scpt -- Author: Brendan Hogan -- Version: 1.2 -- Date: 2023-01-01 -- A script to emulate a SUSPEND command ('Ctrl-Z') in the Terminal, then kill the process watcher shell script. – Adjust myApplication variable(s) to quit the watched applications defined in the processWatcher shell loop. – Quit Terminal at end. set firstApplication to “enter name of FIRSTPROCESS above” set secondApplication to “enter name of SECONDPROCESS above” tell application "Terminal" activate end tell delay 5 tell application "System Events" -- script user interface behavior keystroke "z" using {control down} -- send the Unix SUSPEND command "Ctrl+Z" delay 1 keystroke "kill %%" keystroke return end tell delay 10 try tell application firstApplication quit end tell end try delay 5 tell application secondApplication quit repeat until application secondApplication is not running – use this to prevent hangs of slow-responding applications. Comment out if unnecessary. delay 1 end repeat delay 5 tell application "Terminal" to quit |