I normally have several problems with how cron executes scripts as they normally don't have my environment setup. Is there a way to invoke bash(?) in the same way cron does so I could test scripts before installing them?
I don't believe that there is; the only way I know to test a cron job is to set it up to run a minute or two in the future and then wait.
Cron provides only this environment by default :
HOME
user's home directoryLOGNAME
user's loginPATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
If you need more you can source a script where you define your environment before the scheduling table in the crontab.
You can run:
env - your_command arguments
This will run your_command with empty environment.
By default, cron
executes its jobs using whatever your system's idea of sh
is. This could be the actual Bourne shell or dash
, ash
, ksh
or bash
(or another one) symlinked to sh
(and as a result running in POSIX mode).
The best thing to do is make sure your scripts have what they need and to assume nothing is provided for them. Therefore, you should use full directory specifications and set environment variables such as $PATH
yourself.
Create a cron job that runs env and redirects stdout to a file. Use the file alongside "env -" to create the same environment as a cron job.
Don't forget that since cron's parent is init, it runs programs without a controlling terminal. You can simulate that with a tool like this:
Add this to your cron:
30 08 * * * env > ~/cronenv
After it runs, do this:
env - `cat ~/cronenv` /bin/sh
This assumes that your cron runs sh. I believe this is the default.