Shell script not executing from Cron

Hi All,

Greetings of the day!!
I have made a shell script to check a application service to be running and if found not running, then it should start the service. I did run the script manually on the server however when I schedule it to run every 5 minutes through cron, it does not. Could sone please check and suggest if I am missing something.

Script used:-


# Define the name of the service

# Check if the service is running
if systemctl is-active --quiet $SERVICE_NAME ; then
    echo "$SERVICE_NAME is running."
    # Attempt to start the service
    systemctl start $SERVICE_NAME
    # Check if the service started successfully
    if systemctl is-active --quiet $SERVICE_NAME ; then
        echo "$SERVICE_NAME started successfully."
        echo "Failed to start $SERVICE_NAME."

In cron I am using the below line to make the script run which I have stored in /tmp with 755 permissions:-

*/5 * * * * /path/to/


Hi VT, welcome to the Forums!

So, two things … :slight_smile:

First, this is re-inventing the wheel a little. There is a standard tool called “monit” which does this (and many many other things) which doesn’t need cron for scheduling. Even better, it provides an optional remote API if you want it to feed a dashboard of some kind. (and it’s really easy to use)

Second, in the case of this script, the if-test needs to evaluate the result of the systemctl command, rather than (effectively) a string value, so it should look something like this;

if `systemctl is-active --quiet $SERVICE_NAME` 
    echo "Running"

i.e. you need the back-ticks around the shell command to evaluate it’s result. OR, you could use $() nomenclature like this;

if $(systemctl is-active --quiet $SERVICE_NAME)
    echo "Running"

Historically I would have surrounded the expression with ‘’ but in this instance it’s not needed. You can check this easily by running it twice against something you know is or isn’t running, use a “!” to negagate the result.

if ! $(systemctl is-active --quiet $SERVICE_NAME)
    echo "NOT Running"

You can find "monit here;

The client (which does what you want) is Open Source and available in standard repo’s (apt get monit), the authors also sell a non-OS grapical dashboard.

Sample “monit” config file;

 check process nginx with pidfile /var/run/
   group www
   group nginx
   start program = "/etc/init.d/nginx start"
   stop program = "/etc/init.d/nginx stop"
   if 5 restarts with 5 cycles then timeout
1 Like

Thanks a LOT in taking time out and responding to my query :slight_smile:

What I did, I updated the script as suggested and saved it to my local profile and gave 755 and it worked with Cron and started the service however I will definitely explore the monit as well.


OK - I admit, this mightn’t help with this specific query, but I’ll add it anyway! :slight_smile:

Because I use multiple different distros, and quite frequently rebuild my laptop… I used to frequently hit this kind of problem.

i have learned to set all commands as variables within scripts…

ECHO=$(which echo)
RM=$(which rm)
if [ -f ${testFile} ]; than
${RM} ${testFile}
if [ ${?} -eq 0]; then
${ECHO} deleted
${ECHO} problem
${ECHO} nothing to delete

Remember that not all distros use the same shell, so I also recommend you ensure you use the same shell, installing bash, ash (or whatever) and ensuring the symlink (at /bin/sh) points to it (or your scripts do!)

these actions, will make your scripts more transferable, from one distro/computer to another.