1833431 Members
3683 Online
110052 Solutions
New Discussion

Find out cron stated

 
Ralf Buchhold
Regular Advisor

Find out cron stated

Hello
I must find out whether a shell-script is started by a cron job or by manually by root or an other user.

Thanks
Ralf
7 REPLIES 7
Joseph Loo
Honored Contributor

Re: Find out cron stated

hi,

/var/adm/cron/log will tell u whom and when a cronjob was fired.

regards.
what you do not see does not mean you should not believe
Ralf Buchhold
Regular Advisor

Re: Find out cron stated

Sorry
I mean to find it out in the script file to do different actions.
Ralf
Sanjay Kumar Suri
Honored Contributor

Re: Find out cron stated

Look for #crontab -l (if the job is defined).

Also check cron log /var/adm/cron/log

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Peter Godron
Honored Contributor

Re: Find out cron stated

Ralf,
goto the directory where all the crontabs are held and search for your shellscript:
cd /var/spool/cron/crontabs
grep shell-scriptname *

Is the script being executed on a regular basis? If yes then its most likely cron or 'at'.

Regards

john korterman
Honored Contributor

Re: Find out cron stated

Hi,
perhaps you can make use of the stty command: if you execute "stty -a" in the script and it does not return 0, it must be because no (controlling) terminal is attached - hence I would argue that the script must have been executed from cron.
Just an idea - perhaps other members have obvious arguments against this.

regards,
John K.
it would be nice if you always got a second chance
Mark Ellzey
Valued Contributor

Re: Find out cron stated

Ralf,

Whenever I write a script that can be run either interactively or from cron, I check the $TERM variable. If it has a value, then the script is being run interactively. If not, it's being run from cron or at. Based on that test, I either echo stuff to the terminal, or echo stuff to a log file.

Regards,
Mark
A. Clay Stephenson
Acclaimed Contributor

Re: Find out cron stated

The "standard" way of doing this is to determine if stdin (file descriptor 0) is a tty (terminal) device.

if [ -t 0 ]
then
echo "I'm connected to a terminal. I must be interactive"
else
echo "No terminal"
fi

Man test for details.
If it ain't broke, I can fix that.