Hi Im trying to check a process is running, and if it is, restart it. But if it isn't, start it. The script is run after a batch of commands have been executed remotely from a windows machine using Plink. Then I run another plink command to run this script. The application on the host machine is running continuosly.
SERVICE="./Snowflake.app/Contents/MacOS/Snowflake"
if pgrep "$SERVICE" >/dev/null 2>&1 ;
then
echo "$SERVICE is running"
pkill "$SERVICE"
else
/Applications/snowflake/run.sh
fi
The problem here is that if the application is open it doesnt close it, but instead opens the application a second time. Can anyone tell me where Im going wrong?
I suggest to simply, stupidly kill the process and check the error message like No process found
(which is not really an error in this case).
Every attempt to check whether the process is running before killing it would be prone to a race condition, meaning the process might start right after your check.