12

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

2 Answers 2

17

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.

0
2

Cron isn't an interactive shell, so that's your first problem. It's mostly ignorable though.

The real thing is: environment variables. Look at the headers from a cron mail to find them,then set variables in your shell to match them and unset all remaining variables.

Now you're pretty close to an environment as cron sees it.

1
  • Never noticed the mail headers containing some of the environment, by default Thunderbird hides these from the user. The headers unfortunately, do not represent complete the complete environment.
    – jippie
    Nov 23, 2012 at 22:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .