Operating System - HP-UX
1837792 Members
10838 Online
110120 Solutions
New Discussion

Looking for eloquent way to determine who is running script

 
Geoff Wild
Honored Contributor

Looking for eloquent way to determine who is running script

I just came off of an oncall week - so my brain is a little sleepy...

Anyways, I put together this hack:

who am i
if [[ $? = 0 ]]
then
USRN=`who am i|cut -d" " -f1`
else
USRN=NONINTERACTIVE
fi


How to tell if NONINTERACTIVE was called from say cron? Note - job could also have come non interactively from say Oracle or SAP or OVO...etc...


Thanks...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
17 REPLIES 17
Geoff Wild
Honored Contributor

Re: Looking for eloquent way to determine who is running script

Well - I got it down a bit:

if [ -t 0 ]
then
USRN=`who am i|cut -d" " -f1`
else
USRN=NONINTERACTIVE
fi



Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
James R. Ferguson
Acclaimed Contributor

Re: Looking for eloquent way to determine who is running script

Hi Geoff:

I use this in the opening stanza's of many shell scripts:

# typeset INTERACTIVE=0 && [ -t 0 ] && INTERACTIVE=1

Regards!

...JRF...
TwoProc
Honored Contributor

Re: Looking for eloquent way to determine who is running script

One line/single command answer:

#UNIX95= ps -p $$ -o user=""
john
#

"ps -p $$" runs the "ps" command on "this" shell.

"-o user" tells "ps" to just give me the user name

="" makes the column header (and line) for the user column dissapear; if all column headers for the ps command are null, then no column header is printed. Thus, the whole column header is not present in the output.

The above command returns the effective user. If for some reason you need to differentiate between this and the real user, then substitute "ruser" for "user" in the above command.
We are the people our parents warned us about --Jimmy Buffett
TwoProc
Honored Contributor

Re: Looking for eloquent way to determine who is running script

Oops, I'm sorry (it's Monday), and I see that you wanted to know if you were "interactive" or not. I gave you a command for finding the owner of the current process. Please ignore the posting...
We are the people our parents warned us about --Jimmy Buffett
Geoff Wild
Honored Contributor

Re: Looking for eloquent way to determine who is running script

John, looks good, but it shows root either way:

+ ps -p 5502 -o user=
+ UNIX95=
root
+ [ -t 0 ]
+ + who am i
+ cut -d -f1
USRN=gwild


+ ps -p 5634 -o ruser=
+ UNIX95=
root
+ [ -t 0 ]
+ + who am i
+ cut -d -f1
USRN=gwild

And from cron:

# cat /tmp/fsadm.defrag.cronlog
+ ps -p 5650 -o ruser=
+ UNIX95=
root
+ [ -t 0 ]
+ USRN=NONINTERACTIVE

So, how would that help me determine that it was run from cron?

Thanks...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
john korterman
Honored Contributor

Re: Looking for eloquent way to determine who is running script

Hi,

far from eloquent...
you may have to combine several features for judging "cron-probability"; one feature could be by looking for the sparse PATH environment of cron.
E.g. something like this before PATH is extended:

if [ $USRN = NONINTERACTIVE ]
then
if [ "$PATH" = "/usr/bin:/usr/sbin:." ]
then
echo cron triggering is very likely
else
echo probably not triggered from cron
fi
fi


regards,
John K.
it would be nice if you always got a second chance
TwoProc
Honored Contributor

Re: Looking for eloquent way to determine who is running script

Well, see my second posting to my first posting. It was yet another oops posting b/c of exactly what you said, it didn't help determine interactive. But, James gave you the cleanest answer above. Put the following at the top of the script.

But, being that I posted 2x crud above, I will attempt to redeem myself forthwith.

/usr/bin/tty >/dev/null 2>&1 && echo INTERACTIVE || echo NONINTERACTIVE
We are the people our parents warned us about --Jimmy Buffett
Carlos Roberto Schimidt
Regular Advisor

Re: Looking for eloquent way to determine who is running script

Hi Geoff,

Why dont use just `whoami` instead `who am i|cut -d" " -f1` ?

Schimidt
TwoProc
Honored Contributor

Re: Looking for eloquent way to determine who is running script

Oh, I tested my above posting both in cron and from command line, and it works at least for those two cases.
We are the people our parents warned us about --Jimmy Buffett
James R. Ferguson
Acclaimed Contributor

Re: Looking for eloquent way to determine who is running script

Hi (again):

The one-liner typeset that I posted will quite adequately differentiate a cron'd task from one run interactively at the command line.

Regards!

...JRF...
Ivan Ferreira
Honored Contributor

Re: Looking for eloquent way to determine who is running script

Who is running a script? If it's a specific script, what about using fuser -up scriptname.sh to identify who is running the script?
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Geoff Wild
Honored Contributor

Re: Looking for eloquent way to determine who is running script

So far, we can differentiate between an Interactive and a Non interactive session.

Let us be more specific - how to tell if it was from cron as opposed to say something like a batch job, or

su - gwild -c "remsh someserver somecommand"

Thanks for all the comments so far...

Oh - reason I don't use whoami - is it has to be multi OS capable.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
James R. Ferguson
Acclaimed Contributor

Re: Looking for eloquent way to determine who is running script

Hi (again) Geoff:

Well, the shell variable "$0" is going to be the name of the program when it is run from the commandline *or* as a 'cron' task.

For 'at' ('batch") jobs, the program name is simply "sh" aince a Posix shell is built for the commands executed.

Hence this is one crude differentiation.

For remotely executed tasks, the $0 variable holds the program name as you would expect.

Regards!

...JRF...

Ivan Ferreira
Honored Contributor

Re: Looking for eloquent way to determine who is running script

To identify if was started by cron, check the parent process ID:

ps -fea |grep 2649
root 2650 2649 255 20:34:00 ? 1:36 /bin/yes
root 2649 1139 0 20:34:00 ? 0:00 sh -c /bin/yes > /dev/null 2>&1

ps -fea |grep 1139
root 1139 1 0 Feb 26 ? 0:00 /usr/sbin/cron
root 2649 1139 0 20:34:00 ? 0:00 sh -c /bin/yes > /dev/null 2>&1
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ivan Ferreira
Honored Contributor

Re: Looking for eloquent way to determine who is running script

I think that you can identify non interactive process if the PPID process name does not correspond with a known shell.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Bill Hassell
Honored Contributor

Re: Looking for eloquent way to determine who is running script

Determining whether a script is being run interactively or not is a common problem with several solutions. The most common problem is in /etc/profile and .profile where interactive commands like tset, ttytype, tput, tabs are unprotected (they should be). Here is another way to test:

if tty -s
then
echo "we are interactive"
else
echo "we are not interactive"
fi

A good technique is to insert the tty test in front of all interactive commands:

[[ tty -s ]] && tabs

In the above example, tty -s evaluates to zero (or true) if the current shell (script) is interactive and will run tabs. If the script (including /etc/profile and .profile) is run in batch or cron, the test fails and tabs will be skipped.


Bill Hassell, sysadmin
Geoff Wild
Honored Contributor

Re: Looking for eloquent way to determine who is running script

Using some of the answers from all, I came up with the following:

# find out who is running this program
# see if we are interactive
if [ -t 0 ]
then
# yes - so get the user name
USRN=`who am i|cut -d" " -f1`
else
#no - see if we are cron
PPID=`ps -ef | grep $0 | grep "sh -c" | awk '{print $3}'`
CRON=`ps -ef |grep $PPID | grep -v " -c" |awk '{print $9}'`
if [[ $CRON = "/usr/sbin/cron" ]]
then
# yes we are cron
USRN=CRON
else
# no we are someone else but noninteractive
USRN=NONINTERACTIVE
fi
fi

Thanks...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.