1834022 Members
2399 Online
110063 Solutions
New Discussion

check process

 
SOLVED
Go to solution
hangyu
Regular Advisor

check process

I have a script which will run manually and by crontab , it will generate different result if I use these two different methods , now I want to know the process whether it is run by crontab job or not , so that if it is crontab then do xxx , if it is not crontab job ( that mean run by manually ) then do yyy , as following


if "it is a crontab job" , then
do
xxx
done

else
do
yyy
done

how to know whether it is a crontab job ?
16 REPLIES 16
Dennis Handly
Acclaimed Contributor

Re: check process

Well, you could pass a different parm if from crontab. This would be easier and take less time than scanning ps(1) output as below.

Otherwise you would have to to use ps -fu $LOGNAME and go up your parent process chain until you see cron under 1 (init):
root 2367 1 ... /usr/sbin/cron
Rasheed Tamton
Honored Contributor

Re: check process

A history of all actions taken by cron is recorded in
/var/adm/cron/log.

When cron executes a job, the job's user and group IDs are set to
those of the user who submitted the job.
Sandman!
Honored Contributor

Re: check process

Use the code snippet below to sift a batch job from an interactive one:

if [ ! -t ]; then
echo "it is a crontab job"
else
echo "NOT a crontab job"
fi
Dennis Handly
Acclaimed Contributor

Re: check process

>Use the code snippet below to sift a batch job from an interactive one:

This won't work if stdout is redirected. Using -t 0 would check stdin, which may not be safer.
mobidyc
Trusted Contributor

Re: check process

Hello,

you can run the command 'tty -s ; echo $?'.
if the result is '0', you'r in a terminal, if it's '1', hte program is not running in a terminal as a cron process.
if it's '2', there is a problem in the comand.

tty(1)
tty, pty - get the name of the terminal

---<-----
tty -s ; rval="$?"
if [ "$rval" = "0" ]
then
echo "you'r in a terminal"
elif [ "$rval" = "1" ]
then
echo "you're not in a terminal"
else
echo "there's a problem"
exit
fi
---<-----

Regards,
Cedrick Gaillard
Best regards, Cedrick Gaillard
Sandman!
Honored Contributor

Re: check process

Dennis:
>This won't work if stdout is redirected.

Works for me...I have a batch job with similar logic and redirected output.

~cheers
SANTOSH S. MHASKAR
Trusted Contributor

Re: check process

Hi,

U can check this by using following code

Check_proc_cron.sh
----------------------------
#! /usr/bin/ksh

Cron_PID=`ps -ef|grep /usr/sbin/cron|grep -v grep|awk '{print $2}'`
ps -ef|grep |grep -v grep|awk -v c_pid=$Cron_PID '$3 == c_pid {print "Process "$2" is generated by cron."}'
----------------------------

This will display the process PID which is generated by cron.
Dennis Handly
Acclaimed Contributor

Re: check process

>Sandman!: Works for me...I have a batch job with similar logic and redirected output.

It fails if an interactive user invokes the script and redirects stdout:

$ itrc_batch.sh > stuff
$ more stuff
it is a crontab job

>Cedrick: you can run the command 'tty -s ; echo $?'.

This also fails if stdin is redirected.
mobidyc
Trusted Contributor

Re: check process

Hello,

Dennis> This also fails if stdin is redirected.

if you pass the test outside the shell script, yes of course.
but the goazl is to insert this test IN the shell script, no matter if there is redirecting or not.

if you want to pass the test outside the shell script, in that case you can redirect the output in another output with something like :
print -u 4 "we are in cron shell"

Regards,
Cedrick Gaillard.
Best regards, Cedrick Gaillard
Dennis Handly
Acclaimed Contributor
Solution

Re: check process

>Cedrick: if you pass the test outside the shell script, yes of course.

I copied yours and Sandman!'s suggestions in the shell script itrc_batch.sh.
This matches the specifications of hangyu.

>but the goal is to insert this test IN the shell script, no matter if there is redirecting or not.

Exactly, the test IS in the script. And your tty(1) and Sandman!'s -t fails. These will indicate you are in a cronjob but you could be in a script too.

When redirecting stdin, tty prints: not a tty
mobidyc
Trusted Contributor

Re: check process

Hello,

there is something i must not understand correctly, that works for me.

#> cat test
#!/bin/ksh

TMPFILE=/tmp/test.log
tty -s ; rval="$?"
if [ "$rval" = "0" ]
then
echo "you are in a terminal" >$TMPFILE 2>&1
elif [ "$rval" = "1" ]
then
echo "you're not in a terminal" >$TMPFILE 2>&1
else
echo "there's a problem"
exit
fi

#> ./test >/dev/null 2>&1 ; cat /tmp/test.log
you are in a terminal

#> rm /tmp/test.log ; at now +1 minutes warning: commands will be executed using /usr/bin/sh
job 1176294683.a at Wed Apr 11 14:31:23 2007
you're not in a terminal

if i launch manually the script, it indicates than i'm in a term, this implies not a cron/at job, if i launch through a cront or at command, it indicates than there is no terminal associated.
am i not in your case?

Regards,
Cedrick Gaillard
Best regards, Cedrick Gaillard
Dennis Handly
Acclaimed Contributor

Re: check process

>Cedrick: there is something i must not understand correctly,
#> ./test >/dev/null 2>&1

I said:
This also fails if stdin is redirected.

So your script (tty) will fail with:
$ ./test < /dev/null

mobidyc
Trusted Contributor

Re: check process

Hello,

sorry, bad understanding from my part.

i've modified the script for your demand:
#> cat test
#!/bin/ksh

PID=$$
PPID=$(UNIX95=1 ps -ef -o pid,ppid |grep "^${PID}[[:space:]]" |awk '{print $2}')
TERM=$(UNIX95=1 ps -ef -o ppid,tty |grep "^${PPID}[[:space:]]" |awk '{print $2}')

TMPFILE=/tmp/test.log

if [ -z "$TERM" ]
then
echo "you're not in a terminal" >$TMPFILE 2>&1
else [ "$rval" = "1" ]
echo "you are in a terminal" >$TMPFILE 2>&1
fi
#> /tmp/cgaillar/test you are in a terminal
#> rm /tmp/test.log ; at now +0 minutes warning: commands will be executed using /usr/bin/sh
job 1176301810.a at Wed Apr 11 16:30:10 2007
you're not in a terminal

Regards,
Cedrick Gaillard
Best regards, Cedrick Gaillard
Sandman!
Honored Contributor

Re: check process

How about a simple string comparison as in:

if [ "$(tty)" = "not a tty" ]; then
echo "it is a crontab job"
else
echo "NOT a crontab job"
fi
Dennis Handly
Acclaimed Contributor

Re: check process

>Cedrick: i've modified the script for your demand:
if [ -z "$TERM" ]

Now that is an obvious solution I can live with. ;-)

>Sandman!: How about a simple string comparison as in:
if [ "$(tty)" = "not a tty" ]; then

Why?, the whole purpose of tty -s is to not have to compare strings. (What if you changed the locale?)

stty -s
if [ $? -ne 0 ]; then
echo "it is a crontab job"
else
echo "NOT a crontab job"
fi
Sandman!
Honored Contributor

Re: check process

Awesome not being a multi-linguist guess I would never have thought of the locale :)


#!/usr/bin/sh

tty -s
if [ $? -ne 0 ]; then
echo "it is a crontab job"
else
echo "NOT a crontab job"
fi