Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 10 down vote accepted

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.

share|improve this answer
Interesting, will check it when I return in about an hour. – jippie Nov 23 '12 at 20:19
Brilliant, works like a dream! – jippie Nov 23 '12 at 21:14

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.

share|improve this answer
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 '12 at 22:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.