3

A user crontab entry that reads:

0 */4 * * * $HOME/postscript/fprogcache-sync

works fine. Wanting to add a few environment variables to the cron-job context, I then tried

0 */4 * * * source $HOME/.profile; $HOME/postscript/fprogcache-sync

That did not work until I changed source for .. The error message (sent by postfix mail to root) was /bin/sh: 1: source: not found, followed by confirmation that the script went on to execute happily.

I remember having read somewhere that source is defined as an alias of the builtin . in the bash shell. If so, it would explain why source is not recognized in the cron context.

Is it the case? If so, where is the system wide alias for . defined?

Cbhihe
  • 2,761
  • 3
  • 24
  • 47

2 Answers2

7

Is it the case? If so, where is the system wide alias for . defined?

It is the case, but there's no such thing as a system-wide alias.

. and source are the same builtin in Bash, but that's defined directly at the source code's level (https://askubuntu.com/a/25491/380067).

The reason behind having two names for the same builtin most likely resides in the fact that Bash aims to be POSIX-compliant to some extent; so source for the sake of providing a mnemonic name and . for the sake of complying with POSIX's standards.

The real reason why source doesn't work in cron is that by default cron jobs in the crontab are excuted in sh, which is a symlink to /bin/dash (a shell that aims to be POSIX-compliant as well) which simply doesn't implement source. However it does implement ..

From man dash:

. file
       The commands in the specified file are read and executed by the
       shell.
kos
  • 35,891
1

I think you should run your script on bash, try this :

0 */4 * * * /bin/bash -c "$HOME/postscript/fprogcache-sync"

and if you want to add log in your command and debug scripts add log like this :

0 */4 * * * /bin/bash -c "$HOME/postscript/fprogcache-sync" > /var/log/automatic_backup.log 2>&1