Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 16 Sep 2013 07:17:09 -0500
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Paul Macdonald <paul@ifdnrg.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: test if script called by cron
Message-ID:  <20130916121709.GA97298@dan.emsphone.com>
In-Reply-To: <5236EB03.7040001@ifdnrg.com>
References:  <5236EB03.7040001@ifdnrg.com>

next in thread | previous in thread | raw e-mail | index | archive | help
In the last episode (Sep 16), Paul Macdonald said:
> Is there a simple way of testing whether a given script was called via cron,
> 
> I'd rather find a solution that would work from within the script rather 
> than setting an environment variable in the crontab.

You check to see if stdin is a terminal, but that's not conclusive.  One way
to know for sure is to look at the name of the process that launched you:

if [ ! -t 0 ] ; then
 echo "no tty, possibly run from cron"
fi

parent=$(ps -o command= -p $PPID)
case $parent in 
 *cron* ) echo "parent is $parent, almost certainly cron" ;;
esac

-- 
	Dan Nelson
	dnelson@allantgroup.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130916121709.GA97298>