3

I setup a crontab with crontab -e and checked it was saved with crontab -l:

*/10 6-8 * 1-11 1 /home/asarluhi/Documents/Scripts/monday.sh

The content of monday.sh is:

#!/bin/bash

ruby ~/Documents/Scripts/monday_task.rb >> ~/Documents/Scripts/monday

monday_task.rb is a ruby script which web scrapes some data, too long to report it here, but there are no mistakes in it. monday is a simple empty text file,
The script is working fine when launched from shell.

I looked at var/log/syslog and found the following entries:

Nov  7 07:55:01 SATELLITE-L50-A-161 CRON[6984]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Nov  7 08:00:01 SATELLITE-L50-A-161 CRON[7032]: pam_ecryptfs: Skipping automatic eCryptfs mount
Nov  7 08:00:01 SATELLITE-L50-A-161 CRON[7033]: (asarluhi) CMD (/home/asarluhi/Documents/Scripts/monday.sh)
Nov  7 08:00:01 SATELLITE-L50-A-161 CRON[7031]: (asarluhi) MAIL (mailed 1 byte of output; but got status 0x00ff, #012)
Nov  7 08:00:01 SATELLITE-L50-A-161 CRON[7038]: pam_ecryptfs: Skipping automatic eCryptfs unmount
Nov  7 08:00:43 SATELLITE-L50-A-161 anacron[6485]: Job `cron.daily' terminated
Nov  7 08:00:43 SATELLITE-L50-A-161 anacron[6485]: Normal exit (1 job run)
Nov  7 08:05:01 SATELLITE-L50-A-161 CRON[7112]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Nov  7 08:10:01 SATELLITE-L50-A-161 CRON[7141]: pam_ecryptfs: Skipping automatic eCryptfs mount
Nov  7 08:10:01 SATELLITE-L50-A-161 CRON[7142]: (asarluhi) CMD (/home/asarluhi/Documents/Scripts/monday.sh)
Nov  7 08:10:01 SATELLITE-L50-A-161 CRON[7140]: (asarluhi) MAIL (mailed 1 byte of output; but got status 0x00ff, #012)
Nov  7 08:10:01 SATELLITE-L50-A-161 CRON[7147]: pam_ecryptfs: Skipping automatic eCryptfs unmount
Nov  7 08:15:01 SATELLITE-L50-A-161 CRON[7206]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Nov  7 08:17:01 SATELLITE-L50-A-161 CRON[7221]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
...

As you can notice, cron jobs start at 8:00 am rather than at 6:00 am as in the crontable.
The script is launched but nothing is written in ~/Documents/Scripts/monday
I read in the Cron not running scripts previous askubuntu question that STDERR should end up in the system mail, but there are not directories named after my user name in /var/mail/, so I found nothing.

6

It looks like the executable ruby is not in cron's path, so you need to use the full path to the executable.

Change your monday.sh script from:

#!/bin/bash
ruby ~/Documents/Scripts/monday_task.rb >> ~/Documents/Scripts/monday

to:

#!/bin/bash
/home/asarluhi/.rvm/rubies/ruby-2.3.1/bin/ruby /home/asarluhi/Documents/Scripts/monday_task.rb >> /home/asarluhi/Documents/Scripts/monday

As your syslog demonstrates the monday.sh script is being run on time so absolute path-names is the only answer. As per OP comment ruby also has to be prefixed with absolute path-name.

  • I used the full path in monday.sh: cron launched the script but failed again to write to the file. I thought the use of the full path was important in the crontab, not in the script. As for the ruby interpreter, the full path is /home/.rvm/rubies/ruby-2.3.1/bin/ruby but hidden files cannot be executed from shell. Again, when the original script is launched from shell it works. – Asarluhi Nov 7 '16 at 10:22
  • 1
    Can you try putting /home/.rvm/rubies/ruby-2.3.1/bin/ruby into your monday.sh script and see if cron runs the job correctly? – WinEunuuchs2Unix Nov 7 '16 at 10:25
  • It worked. So as you suggested it is necessary to use the full path everywhere. cron can execute hidden files too. – Asarluhi Nov 7 '16 at 10:53
  • 1
    Wonderful! It's nice to see something working according to plan. I'll update the answer with your full ruby path name. – WinEunuuchs2Unix Nov 7 '16 at 10:58
  • 3
    For root, ~ would expand to /root, not /home/root. And /root definitely exists. – muru Nov 7 '16 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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