How to simulate the environment cron executes a script with?

Jorge Vargas picture Jorge Vargas · Jan 25, 2010 · Viewed 89.6k times · Source

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?

Answer

mmccoo picture mmccoo · Mar 30, 2010

Add this to your crontab (temporarily):

* * * * * env > ~/cronenv

After it runs, do this:

env - `cat ~/cronenv` /bin/sh

This assumes that your cron runs /bin/sh, which is the default regardless of the user's default shell.