Why can't run start delayed job command when use crontab

Viewed 314
#!/bin/bash
#!/usr/bin/env ruby

cd /var/www/project
if [ ! -f tmp/pids/delayed_job.pid ]; then
  echo "File not found!"
  pwd
  ./bin/delayed_job start

  echo "Run script successful!"
fi

In my code start normally is work but run with crontab not work.

any idea pls.

2 Answers

It solved.

I'm use rbenv for ruby install and setup it in to .bash_profile. Yes environment work but i run ruby in crontab not work.

Solution: I copy some config in .bash_profile to my script see below

#!/bin/sh

#below script from .bash_profile

PATH=$PATH:$HOME/bin export PATH export PATH=$HOME/.rbenv/bin:$PATH eval "$(rbenv init -)" export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
#end script from .bash_profile

RAILS_HOME="/var/www/{project}" if [ ! -f $RAILS_HOME/tmp/pids/delayed_job.pid ]; then echo "Starting Delayed job" $RAILS_HOME/bin/delayed_job start fi

I had slightly different requirement, but similar. We always run 5 DJ threads typically all the time. But in many cases, what happens is, if all the 5 threads are working on some long running jobs(even lower priority jobs), then it block high priority jobs and creats long wait time, which is not acceptable to many of our customers. Hence, I did similar thing, and via crontab only. So encountered same problem as mentioned in the ticket. So adding my solution here.

  1. Run a cronjob to detect, how many jobs with priority 0 waiting in the queue, if its more than a fix no. e.g. 2 or 5 or anything.
  2. Then, start another thread automatically.

Here is solution that I have done.

  1. I have created a shell script, that query DJ table using mysql command from shell script. The shell script return 0 if new DJ Thread to be launched, otherwise 1. Pay attention to my && in crontab command.

    #step.1 script #!/bin/bash

    MYSQL_PWD="XXXX"

    field=$(mysql -h<yourhost> -u <user> --password="$MYSQL_PWD" <dbname> -A -BNe 'USE dbname; select count(1) from delayed_jobs where priority=0 and locked_at is null;' )

    if [ $field -gt 2 ]; then return 0 else return 1 fi exit;

  2. then added a crontab command itself of launch on DJ thread with ID 5(tough it can be anything) code for 1&2 are mentioned below.

    #step 2 crontab entry

    */5 * * * * sh /your/sh/file/path/in/step/1/delayed_jobs_count.sh && cd /your/dj/app/path && RAILS_ENV=production /your/bundle/exec/path/bundle exec bin/delayed_job start -i 5 >> /your/home/logs/path/dj-start-cron-t.log 2>&1

Related