Cron Environment Variables – Interactive Shell Setup

cronenvironment-variables

I am aware of a lot of pitfalls in the magic world of crontabs, but sometimes it would help troubleshooting a lot when you have some smart way to enter an interactive (bash) shell with exact identical environment as when a shell script is run from a crontab.

Now I thought myself of /bin/openvt -c8 -- /bin/bash --noprofile -l, but it require root privileges, sets too many variables and a simple su myusername sets a lot of extra environment.

Anybody know of a way to start a interactive bash shell with identical-to-cron environtment and not requiring root privileges on Kubuntu?

Bonus when it works in an ssh session, in the GUI and on one or more of the following OS's too: HP-UX, Solaris and AIX

Best Answer

Run crontab -e and add an entry with

* * * * * export -p > ~/cron-env

(if on Solaris or a system that doesn't use a POSIX shell to interpret that command line, use /usr/xpg4/bin/sh -c 'export -p > ~/cron-env' or whatever the path to the standard sh is on that system).

Wait one minute and remove that line.

You should now have a cron-env file in your home directory.

You can then run:

cd && env -i sh -c '. ./cron-env; exec sh'

To start a shell with the same environment as your cron job got.

Related Question