Starting a service (upstart) from a shell script, from a cron job

I am trying to use the cron job to invoke the healthcheck script that I wrote to check the status of the web application (api) that I wrote (the URL is not sufficient to check the full functionality, hence the user health check). The healthcheck application has several endpoints that are called from the shell script (see below), and this script restarts the larger web application that we are testing. Naturally, I have a problem.

How it works: 1) cron runs every 60 seconds 2) healthcheck script runs cron job 3) healthcheck script checks url, if url returns a non-200 response, it stops and starts the service

What works: 1) I can run the script (healthcheck.sh) as the user ec2 2) I can run the script as root 3) The cron task calls the script and it starts, but it does not stop / does not start the service (I see this, watching /tmp/crontest.txt and ps aux).

This is completely similar to a permission issue or some very basic Linux thing that I don't know about.


Log when I run it as root or ec2-user (/tmp/crontest.txt):

Fri Nov 23 00:28:54 UTC 2012 healthcheck.sh: api not running, restarting service! api start/running, process 1939 <--- it restarts the service properly! 

Log when cron job runs:

 Fri Nov 23 00:27:01 UTC 2012 healthcheck.sh: api not running, restarting service! <--- no restart 

Cron File (in / etc / cron.d):

 # Call the healthcheck every 60s * * * * * root /srv/checkout/healthcheck/healthcheck.sh >> /tmp/crontest.txt 

Upstart script (/etc/init/healthcheck.conf) is for a healthcheck application that provides the endpoints that we call from the shell script healthcheck.sh:

 #/etc/init/healthcheck.conf description "healthcheck" author "me" env USER=ec2-user start on started network stop on stopping network script # We run our process as a non-root user # Upstart user guide, 11.43.2 (http://upstart.ubuntu.com/cookbook/#run-a-job-as-a-different-user) exec su -s /bin/sh -c "NODE_ENV=production /usr/local/bin/node /srv/checkout/healthcheck/app.js" $USER end script 

Script shell permissions:

 -rwxr-xr-x 1 ec2-user ec2-user 529 Nov 23 00:16 /srv/checkout/healthcheck/healthcheck.sh 

Shell script (healthcheck.sh):

 #!/bin/bash API_URL="http://localhost:4567/api" echo `date` status_code=`curl -s -o /dev/null -I -w "%{http_code}" $API_URL` if [ 200 -ne $status_code ]; then echo "healthcheck.sh: api not running, restarting service!" stop api start api fi 
+1
source share
2 answers

Add the path to the start / stop command for your script:

 #!/bin/bash PATH=$PATH:/sbin/ 

or your full path to start and stop commands:

 /sbin/stop api 

you can check the path to them using whereis:

 $ whereis start /sbin/start 
+4
source

The answer is found in another question !

Most cron jobs work in a restricted environment, so no start command was found in 'start [service]'!

Modifying the script to look like this makes it work:

 #!/bin/bash PATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/usr/bin:/opt/usr/sbin:/usr/local/bin:/usr/local/sbin" ... 
+3
source

All Articles