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 <
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):
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
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