I have a cron job that is running a script. When I run the script via an interactive shell (ssh'ed to bash) it works fine. When the script runs by itself via cron it fails.
My guess is that it is using some of the environmental variables set in the interactive shell. I'm going to troubleshoot the script and remove these.
After I make changes, I know I could queue up the script in cron to have it run as it would normally, but is there a way I can run the script from the command line, but tell it to run as it would from cron – i.e. in a non-interactive environment?
Best Answer
The main differences between running a command from cron and running on the command line are:
/bin/sh
);cron(8)
orcrontab(5)
man page; generally there's justHOME
, perhapsSHELL
, perhapsLOGNAME
, perhapsUSER
, and a smallPATH
);%
character specially (it is turned into a newline);The following invocation will run the shell snippet pretty much as if it was invoked from cron. I assume the snippet doesn't contain the characters
'
or%
.See also executing a sh script from the cron, which might help solve your problem.