Interactive shell with environment identical to cron

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x