1

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?

1
  • Full path to ruby maybe? Like /usr/bin/ruby?
    – JdeHaan
    Commented Dec 6, 2017 at 17:19

2 Answers 2

2

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.

2
  • 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
    Commented Dec 6, 2017 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. Commented Dec 6, 2017 at 17:36
1

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'
2
  • Oh stupid me...I totally missed this. Thank yo uso much! You've made my day!
    – jedi
    Commented Dec 6, 2017 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
    Commented Dec 6, 2017 at 17:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .