test if script called by cron

Polytropon freebsd at edvax.de
Mon Sep 16 12:05:22 UTC 2013

On Mon, 16 Sep 2013 12:26:59 +0100, Paul Macdonald wrote:
> 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.

I'd suggest the script creates a file (lock file or,
much easier, just a simple normal file) at its beginning:

	/usr/bin/touch /tmp/scriptrun
	# ... your script content here ...

You could also output the date command to that file
to see when the script has been called:

	/bin/date "+%Y-%m-%d %H:%M:%S" > /tmp/scriptrun
	# ... your script content here ...

Of course you would have to manually remove that file
after you have verified its existence and content.

Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

More information about the freebsd-questions mailing list