I want to have a certain script (rails rake task) running on my server at all times - it basically populates my DB using a live feed. I can create a screen (http://linux.die.net/man/1/screen) and run the task easily, but I'd like to automate it in case it fails or stops (whenever I deploy to production, it stops).
I've created a script that does this perfectly:
# my_script
#!/bin/bash
start_myscreen () {
screen -dmS myscreen bash -c 'cd /var/app/current/; rake my_task RAILS_ENV=production'
}
if screen -list | grep -q "No Sockets found"; then
echo "no screens available"
echo "starting myscreen"
start_myscreen
else
echo "screens available!"
if screen -list | grep -q "myscreen"; then
echo "myscreen exists - restarting"
screen -X -S myscreen quit
else
echo "myscreen does NOT exist - creating now"
fi
start_myscreen
echo "Finished!"
fi
It is designed so that no matter what, the screen "myscreen" should be initiated with the rake task started by the time the script finishes. Obviously in production I won't be running the script every minute, this is currently only for testing. This script works perfectly when executed from terminal via:
./my-script
The problem occurs when I run it using this very simple crontab:
* * * * * ./my-script
What is strange is that the script is outputting everything correctly (based on the message sent to me by mail from crontab), but I do not see any screens running after the cron executes when I run:
screen -list
Very strange.
BTW - the server runs 64bit Amazon Linux (red-hat I believe)
Thanks for any help.