cron tab to restart my delayed_job server
-
29-09-2019 - |
Question
I want to create a cront tab to restart my delayed_job server if it breaks.
how may check that my delayed_job server is running or not with ps command?
how may i create a cron tab to check this thing work? Like if my server break than cron tab will restart it. I want to set cront tab to check it every 5 minute.
Solution
This is my script which check if pid files is created or not. If pid file is not exist than it will start the delayed_job server. I have created my script in my application root folder This is my script delayed_job.sh
if ! [ -s delayed_job.pids ]; then
RAILS_ENV=production /app/script/delayed_job start
end
And i have set this script on crontab like this.
*/5 * * * * RAILS_ENV=production /bin/bash /app/delayed_job.sh
It's working fine. If i break delayed_job server than it will automatically start my delayed_job server. Is it a proper way??
OTHER TIPS
The right way if you have one delayed_job process is:
#!/bin/bash
if ! [ -s #{pids_dir}/delayed_job.pid ]; then
RAILS_ENV=production #{RAILS_APP}/script/delayed_job start
fi
I use runit/sv to manage delayed_job. Includes monitoring, auto restarts, logging.
If you don't stick to cron, Monit may be more useful. http://mmonit.com/monit/
Here's my solution. I check for whether you have a single job started or you have more than one. If you do, quit, otherwise start the job with two workers. You probably won't need that /usr/local/bin/ruby
part.
#!/bin/bash
if [[ ! -f /apps/gits/myapp/tmp/pids/delayed_job.pid
&& ! -f /apps/gits/myapp/tmp/pids/delayed_job.0.pid ]];
then
echo "DJ not running"
cd /apps/gits/myapp
RAILS_ENV=production /usr/local/bin/ruby script/delayed_job -n 2 start
echo "DJ Started"
else
echo "DJ running"
fi
And then my cron runs every hour, just make sure the bash is executable.
0 */1 * * * /apps/gits/scripts/start_dj_if_not_running.sh
>> /apps/gits/myapp/log/runner.log 2>&1