Can a bash script tell if it's being run via cron?

You can try "tty" to see if it's run by a terminal or not. That won't tell you that it's specifically run by cron, but you can tell if its "not a user as a prompt.

You can try "tty" to see if it's run by a terminal or not. That won't tell you that it's specifically run by cron, but you can tell if its "not a user as a prompt". You can also get your parent-pid and follow it up the tree to look for cron, though that's a little heavy-handed.

2 Thanks for this answer! The former answer I picked was actually not what I wanted (interactive shell). Due to your answer, I found what I really needed by Googling "bash tty test".

Basically, call tty -s in your script and then check $? : if it is 0, you are in a tty, if it's greater than 0, you aren't. – Topher Fangio Aug 3 '10 at 15:52.

Why not have a command line argument that is -t for testing or -c for cron. Or better yet: -e=email@address. Com If it's not specified, don't send an email.

I'm dropping the script in /etc/cron. Daily so I can't easily add arguments, but I guess I could move it to a real crontab. I was just hoping not to have to add arguments or set environment variables on the command line...although: setting a TESTING environment variable in my .

Bashrc file would do the trick. Thanks for the suggestions. – Topher Fangio Jul 9 '10 at 18:59 There is no problem with putting myscript.Sh arg1 arg2 arg3 in a script file in /etc/cron.daily.

I don't usually put my script/binary file directly into the /etc/cron. * dirs. I usually leave that for scripts that execute the cron job (may be another script).

Then I can write my script more generic and usable in other environments. – sims Jul 12 '10 at 0:51.

Not without outside help, but it can tell if it's running from an interactive shell, which it will be when you're running it manually to test it: if -z "$PS1" ; then echo This shell is not interactive else echo This shell is interactive fi (Code sample from here).

This is perfect! Ideally I'd like it to work for anyone who runs it manually. Thank you very much!

– Topher Fangio Jul 9 '10 at 19:01.

Here's two different options for you: Take the emailing out of your script/program and let cron handle it. If you set the MAILTO variable in your crontab, cron will send anything printed out to that email address. Eg: [email protected] # run five minutes after midnight, every day 5 0 * * * $HOME/bin/daily.

Job Set an environment variable in your crontab that is used to determine if running under cron. Eg: THIS_IS_CRON=1 # run five minutes after midnight, every day 5 0 * * * $HOME/bin/daily. Job and in your script something like if -n "$THIS_IS_CRON" ; then echo "I'm running in cron"; else echo "I'm not running in cron"; fi.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions