2

As a super-user I crontab -e and add a line to source a cronsh.sh file like this:

0 0 * * * source /home/myname/cronsh.sh

The content of the cronsh.sh file is this line:

date >> /home/myname/cronlog.txt

However when I check the cronlog.txt file the next day, it's empty. What am I doing wrong?

Thank you in advance

3
  • 3
    Cron uses sh as its default shell and source is a bash shell builtin command so not recognized in whatever sh is linked to other than Bash which is not the case for system shell (usually dash by default) ... On the other hand, the equivalent of for the source command . should work there, so try changing source to .
    – Raffa
    Nov 20 at 11:19
  • 1
    The same applies to the contents of /home/myname/cronsh.sh as well ... So check its syntax is dash friendly or alternatively tell CRON to use bash with SHELL=/bin/bash set before your cronjob in the crontab file.
    – Raffa
    Nov 20 at 11:23
  • 1
    Thank you very much! You can put it as an answer if you want
    – SO_32
    Nov 20 at 11:36

1 Answer 1

7

CRON uses sh as its default shell and source is a Bash shell builtin command so not recognized in whatever sh is linked to other than Bash which is not the case for system shell (usually Dash by default) ... On the other hand, the equivalent of for the source command . should work there ... See demonstration below:

$ echo "$0"
bash
$
$ type . source
. is a shell builtin
source is a shell builtin
$
$
$ sh
$
$ echo "$0"   
sh
$
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Jul 11 12:03 /bin/sh -> dash
$
$ type . source
. is a special shell builtin
source: not found

so try changing source to . ... The same applies to the contents of /home/myname/cronsh.sh as well. So check its syntax is Dash friendly or alternatively tell CRON to use Bash with SHELL=/bin/bash set before your cronjob in the crontab file like so:

SHELL=/bin/bash
0 0 * * * source /home/myname/cronsh.sh

It's worth mentioning that you don't generally need to source a Bash script to execute it (unless you deliberately want it to execute in the current invoking shell and not in a sub-shell), but a more preferred way is to invoke the Bash interpreter itself and provide the script file as an argument like so:

0 0 * * * /bin/bash /home/myname/cronsh.sh

... which should also work if you add a shebang to your script specifying the correct interpreter to invoke i.e. #!/bin/bash and then invoke the script as an executable itself after you give it execution permissions of course like so:

0 0 * * * /home/myname/cronsh.sh

... and that should save you from needing to apply the above mentioned other suggestions.

You must log in to answer this question.

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