How to know a script is called from cron?
This thread belongs to

2009-08-01 15:20 GMT   |   #1
I'm running SlackWare 12.2 with [the default]dcron 2.3.3.
I would like to run a Bash script from cron, but not see all
the output it gives when run from the command line.
Is it possible to check in the script is it is run from cron
or not?
Or is there nother clevwer way to achieve this?
Thanks in advance for your help.
2009-08-01 15:21 GMT   |   #2
Untested idea: check for status after running 'tty -s'. See 'man tty' for
more info.
2009-08-01 17:20 GMT   |   #3
Two methods.
tty -s # $?=0 yes =1 cron
test -t 0 # $?=0 yes =1 cron and is faster than the above.

(your method of choice here)
if [ $? -eq 0 ] ; then
console interactive code here
cron code here

Some light reading found here
2009-08-01 17:20 GMT   |   #4
Not sure I am following you, Hans. But if I am, what I would
do is put a line like this at the end of the script:

/usr/bin/date >> $HOME/.scriptname.log

Then you just check the log. If the script didn't run at the
times it was supposed to, then you know something is wrong.
2009-08-01 20:07 GMT   |   #5
Well, strictly speaking, these tests look for a terminal, so $?=1 means
there's no terminal. There's no terminal for a cron job, but also for a
remote ssh command:

$ ssh myhost 'test -t 0;echo $?'

So you do need to be a little careful of the context. But it's unlikely
to matter to the OP.

This is one scenario where Vixie cron is slightly better: environment
variables can be set from a crontab file, and queried by a script.
This can't be done in Dillon cron. But again, in this case it probably
doesn't matter. There might be other times where you really need to
distinguish between a terminal, an ssh remote command, or a cron job.
Other than writing a wrapper script for each scenario I don't have any
good ideas for this task.
2009-08-02 03:20 GMT   |   #6
Got it.

You run the main script in a subshell within the script by
putting it in parentheses:



ls -l /usr/doc/fooxxxx # non-existent directory


echo $? >> $HOME/.scriptname.log

echo date >> $HOME/.scriptname.log

#end script

The exit status sent to the log will be 1.

But the exit status of the script will be 0.
2009-08-02 03:20 GMT   |   #7
Would not have to be a separate wrapper script. You could test one of
the SSH variables like SSH_TTY, SSH_CONNECTION, or SSH_CLIENT.


test -t 0
if [ $? -eq 0 ] ; then

if [ -n "SSH_TTY" ] ; then

if [ $_terminal_flag -eq 1 ] ; then
(interactive code here)
(cron code here)
2009-08-02 03:20 GMT   |   #8
First look at "/usr/bin/unicode_stop", there you find an incomplete try
for testing. But the "case" structure is helpful.

The better test condition for that case structure is

TTY="$(/usr/bin/tty 2>/dev/null)" || exit

Maybe you have to change the "go" and "nogo" branches. And the verbosity
(it's not good when a cronjob skript shouts "there was an error").
2009-08-02 03:20 GMT   |   #9
A script running from cron is not going to be connected to a tty
unless this is a part of the script, and the OP said he didn't
want it to do that. See above where he says "...not see all the
output it gives when you run from the command line."

So why is everyone talking about tty's?
2009-08-02 03:20 GMT   |   #10
Shucky dern right back at you! I should have guessed that some SSH
environment variables would be set.

1 2nextlast