- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Looking for eloquent way to determine who is r...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 04:43 AM
03-20-2006 04:43 AM
Looking for eloquent way to determine who is running script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 04:54 AM
03-20-2006 04:54 AM
Re: Looking for eloquent way to determine who is running script
if [ -t 0 ]
then
USRN=`who am i|cut -d" " -f1`
else
USRN=NONINTERACTIVE
fi
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 05:05 AM
03-20-2006 05:05 AM
Re: Looking for eloquent way to determine who is running script
I use this in the opening stanza's of many shell scripts:
# typeset INTERACTIVE=0 && [ -t 0 ] && INTERACTIVE=1
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 05:31 AM
03-20-2006 05:31 AM
Re: Looking for eloquent way to determine who is running script
#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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 05:34 AM
03-20-2006 05:34 AM
Re: Looking for eloquent way to determine who is running script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 05:44 AM
03-20-2006 05:44 AM
Re: Looking for eloquent way to determine who is running script
+ 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 06:43 AM
03-20-2006 06:43 AM
Re: Looking for eloquent way to determine who is running script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 06:48 AM
03-20-2006 06:48 AM
Re: Looking for eloquent way to determine who is running 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 06:58 AM
03-20-2006 06:58 AM
Re: Looking for eloquent way to determine who is running script
Why dont use just `whoami` instead `who am i|cut -d" " -f1` ?
Schimidt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 07:33 AM
03-20-2006 07:33 AM
Re: Looking for eloquent way to determine who is running script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 07:43 AM
03-20-2006 07:43 AM
Re: Looking for eloquent way to determine who is running script
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 09:07 AM
03-20-2006 09:07 AM
Re: Looking for eloquent way to determine who is running script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 09:13 AM
03-20-2006 09:13 AM
Re: Looking for eloquent way to determine who is running script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 09:33 AM
03-20-2006 09:33 AM
Re: Looking for eloquent way to determine who is running script
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 09:38 AM
03-20-2006 09:38 AM
Re: Looking for eloquent way to determine who is running script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 09:51 AM
03-20-2006 09:51 AM
Re: Looking for eloquent way to determine who is running script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2006 12:21 PM
03-20-2006 12:21 PM
Re: Looking for eloquent way to determine who is running script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2006 02:29 AM
03-21-2006 02:29 AM
Re: Looking for eloquent way to determine who is running script
# 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