How to simulate your cron environment

When you’re debugging cron jobs, it’s very useful to be able to simulate the environment your crons are running under so you can see what’s happening. Here’s how to do that:

1) Save your cron environment to a file

Enter your crontab and add the following cron:

* * * * * env > ~/.cronenv

As you can probably tell, once the above cron completes, you’ll have your cron environment saved to a file .cronenv in your home directory.

2) Enter your cron environment

To enter your cron environment, all you have to do is:

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

Now you can run commands using the same environment that your crontab runs under.

Originally published at @mwender.