I'm trying to write a cron job script that will check if my services are running and restart them if they aren't so I don't have to do it manually.
Now, I've looked up online how to check the status of a service in a bash script, and have found basically the following, with a few variations:
ps auxw | grep <service_name> | grep -v grep
if [[ $? != 0 ]]; then
/etc/init.d/<service_name> start
fi
I did some thinking and thought it might be a bit less hacky and more of a way of using the init script's general functionality to check it this way:
/etc/init.d/<service_name> status
if [[ $? != 0 ]]; then
/etc/init.d/<service_name> start
fi
Please correct me if I'm wrong, but wouldn't this always work? Is this a property of init scripts in general, that they return that exit code? Thanks in advance. :)