I have setup 3 crontab jobs to execute my simple ruby scripts periodically every minute, 5 minutes and hour. They execute, however they do not do anything. I have only one user on the machine (root) and I have setup the crontab by executing the command crontab -e. crontab -l lists my current crontab jobs:

5 * * * * ruby /root/www/server-monitoring/current/tasks/cpu_check.rb
0 * * * * ruby /root/www/server-monitoring/current/tasks/free_disk_space.rb
1 * * * * ruby /root/www/server-monitoring/current/tasks/free_ram_check.rb

I can see that they execute but not in the right intervals and also they do not do anything whereas if I execute those ruby files manually they work perfectly fine. I can confirm that the ruby programs work fine 100%, they pass tests, etc. Here are the crontab logs:

Dec  6 15:45:01 monitoring-jedrzej CRON[28281]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Dec  6 15:55:01 monitoring-jedrzej CRON[28478]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Dec  6 16:00:01 monitoring-jedrzej CRON[28584]: (root) CMD (ruby /root/www/server-monitoring/current/tasks/free_disk_space.rb)
Dec  6 16:01:01 monitoring-jedrzej CRON[28614]: (root) CMD (ruby /root/www/server-monitoring/current/tasks/free_ram_check.rb)
Dec  6 16:05:01 monitoring-jedrzej CRON[28702]: (root) CMD (ruby /root/www/server-monitoring/current/tasks/cpu_check.rb)
Dec  6 16:05:01 monitoring-jedrzej CRON[28703]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Dec  6 16:15:01 monitoring-jedrzej CRON[29214]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)

What am I missing here?

  • Full path to ruby maybe? Like /usr/bin/ruby? – JdeHaan Dec 6 '17 at 17:19
up vote 1 down vote accepted

You have not set these jobs to run every minute, every 5 minutes, and every hour. All three are set to run once per hour, at :01 past the hour, :05 past the hour, and :00 past the hour. Instead you might try something like

* * * * *    echo 'run every minute'
*/5 * * * *  echo 'run every 5 minutes'
0,5,10,15,20,25,30,35,40,45,50,55 * * * *   echo 'alternate every-five-minute'
0 * * * *    echo 'run every hour on the hour'
  • Oh stupid me...I totally missed this. Thank yo uso much! You've made my day! – jedi Dec 6 '17 at 17:42
  • I have accidently found a note on RVM site that 'For every installed ruby and created gemset RVM creates wrappers for basic command line tools like gem, rake, ruby' and I should actually do this: /usr/local/rvm/wrappers/ruby-2.3.0/ruby /path/to/script.rb and it worked! – jedi Dec 6 '17 at 17:45

Generally, you'll find that this is an environment issue. Running a script from cron does NOT execute the /etc/profile script. You can source /etc/profile at the start of the script, or set just those variables that you'll need for your script (PATH is one that comes quickly to mind).

I'd suggest creating a small script that runs the env command and pipe that to a file so that you can see exactly what your environment is when running something from cron. Should help you find what is not set that needs to be.

  • Thank you. I also noticed that they do not run at the right intervals. The first job should run every 5 mintues, the second job every hour and the last one every mintue, however, as you can see in the logs, they do not execute in the right interval. Any ideas? – jedi Dec 6 '17 at 17:26
  • 2
    I hadn't looked at that. The minute in crontab is what minute of the hour to execute that script, not how many minutes apart. You should be able to add a comma separated list of minutes (00,05,10,15, ...) to get it to do what you want, but that gets tedious. Looking into it a little, I see that you should be able to do */5 for every five minutes, */2 for every two minutes, and */1 for every minute. – James C. Woodburn Dec 6 '17 at 17:36

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.