tags:

views:

81

answers:

5

Hello all,

Not having much luck Googling this question and I thought about posting it on SF, but it actually seems like a development question. If not, please feel free to migrate.

So, I have a script that runs via cron every morning at about 3 am. I also run the same scripts manually sometimes. The problem is that every time I run my script manually and it fails, it sends me an e-mail; even though I can look at the output and view the error in the console.

Is there a way for the bash script to tell that it's being run through cron (perhaps by using whoami) and only send the e-mail if so? I'd love to stop receiving emails when I'm doing my testing...

+1  A: 

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)

moonshadow
This is perfect! Ideally I'd like it to work for anyone who runs it manually. Thank you very much!
Topher Fangio
+2  A: 

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.

eruciform
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
+4  A: 

Why not have a command line argument that is -t for testing or -c for cron.

Or better yet:

[email protected]

If it's not specified, don't send an email.

sims
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
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
+1  A: 

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
    
Geoff Reedy
A: 

Parse the process table (ps) to see whether the cron daemon is an ancestor of your process.

mobrule