Operating System - HP-UX
1833154 Members
3177 Online
110051 Solutions
New Discussion

Re: Determining Where Script Was Started From

 
Pete Randall
Outstanding Contributor

Determining Where Script Was Started From

I'm trying to consolidate the setting of environemnt variables into one location: /etc/profile. Then /etc/profile would be sourced at the start of all scripts to setup the environment regardless of whether the job was started from the command line or from cron. I've put in logic to test whether the environemnt is interactive so we don't trip over terminal I/O:

if [[ -t 0 ]]
then
do term-oriented stuff like tset, tabs, etc
fi



Now I'm wondering if there's a way to tell if a script is started from the command line or if it's been started via cron. The issue is that the /etc/copyright message gets displayed at the start of every script that's started from the command line - I would like to only see the /etc/copyright when I login.

Any suggestions?


Pete



Pete
34 REPLIES 34
Steven E. Protter
Exalted Contributor

Re: Determining Where Script Was Started From

Hi Pete,

I noticed from ps -ef my cron jobs have TTY as ?

My command line jobs have TTY set to ttype3

I know you are skilled with awk and can read the TTY characteristics.

Usually cron has no TERM variable set, so that's how I'd approach it.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
RAC_1
Honored Contributor

Re: Determining Where Script Was Started From

Jobs started from cron will not have tty attached to it. ps -ef will show ? under tty, while if started from command line, it will show some tty name.

Anil
There is no substitute to HARDWORK
A. Clay Stephenson
Acclaimed Contributor

Re: Determining Where Script Was Started From

One method to distinguish those processes started by cron would be to examine ${PPID} and compare it the cron's PID. Of course, if a process has been spawned by another process that was a child of cron this simple method would miss it. To do it right, you have to climb the process tree until either the PPID is 1 (init) or the PPID equals the PID of cron.

You can avoid this expensive test most of the time, by first using -t 0. Iff you are willing to assume that anything that has a tty device assigned to stdin, is by definition interactive and thus could be spawned by cron. Of course, it is possible to contrive a situation where stdin is connected to a tty device and yet was started by cron.
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: Determining Where Script Was Started From

Hi Pete,

One another way is to check the PPID of the shell spawned by the script is of 'cron''s. For ex., I put the following

PID=$$
PPID=$(ps -ef|awk -v pid=$PID '$2 == pid {print $3}')
PPPID=$(ps -ef|awk -v pid=$PPID '$2 == pid {print $3}')
echo $PID, $PPID, $PPPID > /tmp/cron.log

In there PPPID corresponds to /usr/sbin/cron.

You can use UNIX95 to do the actual work. Above is only to give you an idea.



-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Steven E. Protter
Exalted Contributor

Re: Determining Where Script Was Started From

if [ -z "$TERM" ]
then
echo "No terminal" > /tmp/log
else
cat /etc/copyright
fi

Check out the term logic in my /etc/profile, which I'm attaching. It might help.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Patrick Wallek
Honored Contributor

Re: Determining Where Script Was Started From

Hey Pete,

How about checking the PPID of the process and if the parent is NOT telnetd then don't display /etc/copyright.

Something like:

PP=$(ps -p $PPID | grep -v PID | awk '{print $4}')

if [ "${PPID}" = "telnetd" ] ; then
cat /etc/copyright
fi
RAC_1
Honored Contributor

Re: Determining Where Script Was Started From

If the script is started from cron, cron will have an entry for this. Scripts started from command line altogether different.

Anil
There is no substitute to HARDWORK
Paula J Frazer-Campbell
Honored Contributor

Re: Determining Where Script Was Started From

Pete

Sorry but I had to do it.

1. If it has coffee stains on it I would suggest your desk.

2. If it is sandy - perhaps the beach.

3. If wet the bathroom.

4. If cold the frezer.

5. If cold and dry the comms room.

6. If wet and cold London.

7. If it smells salty the Titanic.



TGIF

Paula
If you can spell SysAdmin then you is one - anon
Patrick Wallek
Honored Contributor

Re: Determining Where Script Was Started From

OK, my script don't work right! It would have to be if the parent of the parent is telnetd then display /etc/copyright.

I'm still trying to figure that one out. It's a good exercise for a friday.

Mark Grant
Honored Contributor

Re: Determining Where Script Was Started From

Actually, I think all the above solutions could miss out. Even walking the process tree doesn't always work (though you have to be making a bit of an effort for your script to be owned by init).

I think RAC is the closest though perhaps the lowest tech. If your $* matches a crontab entry AND the time is the same as that entries time in cron you are going to be pretty damn close.

Alternatively, why don't you prefix all cron entries with "export FROMCRON=1". Then test for this variable in your /etc/profile. You could even have a cron job that inserts ths into all the cron entries taht haven't got one already.
Never preceed any demonstration with anything more predictive than "watch this"
Paula J Frazer-Campbell
Honored Contributor

Re: Determining Where Script Was Started From

Pete

Can you not put a test in /etc/profile to see what is reading it and if not a login sequence then do not display /etc/copyright.



Paula
If you can spell SysAdmin then you is one - anon
Volker Borowski
Honored Contributor

Re: Determining Where Script Was Started From

Nice task,

tried and verified this ! Get rid of the "echo" stuff when tested.
Have fun
Volker

#!/bin/sh
set `ps -ef | grep cron | grep -v grep`
PID_CRON=$2
NEXT_PID=$$
while [ $NEXT_PID -ne 1 ] && [ $NEXT_PID -ne $PID_CRON ]
do
set `ps -fp $NEXT_PID | grep -v 'UID PID PPID'`
echo PID $NEXT_PID has parent $3
NEXT_PID=$3
done

if [ $NEXT_PID -eq $PID_CRON ]
then
echo I am a somehow grandchild of cron
else
echo cron is no relative of mine
fi


#### End Script

# mail
From root Fri Mar 5 18:58:03 MET 2004
Received: (from root@localhost)
by cthhp10.cth.de (8.8.6 (PHNE_17190)/8.8.6) id SAA18916
for root; Fri, 5 Mar 2004 18:58:02 +0100 (MET)
Date: Fri, 5 Mar 2004 18:58:02 +0100 (MET)
From: root
Message-Id: <200403051758.SAA18916@cthhp10.cth.de>
Subject: cron

PID 18904 has parent 18903
PID 18903 has parent 677
I am a somehow grandchild of cron


*************************************************
Cron: The previous message is the standard output
and standard error of one of your crontab commands:




# cd /home/root
# ./script
PID 18943 has parent 18887
PID 18887 has parent 18886
PID 18886 has parent 567
PID 567 has parent 1
cron is no relative of mine
#
#
Volker Borowski
Honored Contributor

Re: Determining Where Script Was Started From

OT:

Good job SRI !
Congrats on the new salad !
Must just has happened !
Reading 20017 pts 20 secs ago !

Go on
Volker
Pete Randall
Outstanding Contributor

Re: Determining Where Script Was Started From

OK, Folks. I think I misled you on the goal here. What I'm really trying to determine is if /etc/profile is being parsed by a login or a script (regardless of whether it from command line or cron).

If it's because of login, I want to display /etc/copyright. If from a script, then no copyright.

I think Paula is closest to what I'm after with "Can you not put a test in /etc/profile to see what is reading it and if not a login sequence then do not display /etc/copyright."
I don't know how to test if it's a login sequence though.


Pete

Pete
Paula J Frazer-Campbell
Honored Contributor

Re: Determining Where Script Was Started From

Pete

Dont have a server to test this on but,

Follow a normal login sequence through and find the pid of the read of /etc/profile

Then in /etc/profile you have to put a sequence to look for this pid and if equal then skip the copyright bit.

It might work.


Paula
If you can spell SysAdmin then you is one - anon
Pete Randall
Outstanding Contributor

Re: Determining Where Script Was Started From

OK, Paula, that sounds somewhat promising, but how do I follow a login sequence through?


Pete

Pete
A. Clay Stephenson
Acclaimed Contributor

Re: Determining Where Script Was Started From

Rather than answering directly, I'll give you a hint now that the question is better defined:

From a known login shell:
echo "${0}"
next

and again from within that shell:
sh
echo "${0}"

Note the first character in ${0}; if it is a '-' is is assumed to be a login shell.
If it ain't broke, I can fix that.
Paula J Frazer-Campbell
Honored Contributor

Re: Determining Where Script Was Started From

Pete

Good Question - its got me thinking.

Fuser on /etc/profile in /etc/profile at start,

fuser -v > /tmp/test-profile


????


Paula
If you can spell SysAdmin then you is one - anon
Volker Borowski
Honored Contributor

Re: Determining Where Script Was Started From

Well no solution yet,

but the best starter I think is to look up the own name. A login-shell always starts with a dash.

# ps -ef | grep sh
root 4 0 0 Aug 27 ? 11:19 unhashdaemon
root 18982 18981 3 19:34:08 pts/ta 0:00 -sh
root 19003 18982 3 19:34:59 pts/ta 0:00 grep sh
# echo $0
-sh
# sh
# ps -ef | grep sh
root 4 0 0 Aug 27 ? 11:19 unhashdaemon
root 19006 19004 3 19:35:13 pts/ta 0:00 grep sh
root 18982 18981 0 19:34:08 pts/ta 0:00 -sh
root 19004 18982 2 19:35:06 pts/ta 0:00 sh
# echo $0
sh
#

Although I still have no idea how to validate it. May be it works directly with something like "... cut -c1 from_my_name ..."

Volker



Volker
Paula J Frazer-Campbell
Honored Contributor

Re: Determining Where Script Was Started From

Sorry that should be:-

fuser -v /etc/profile > /tmp/test-profile

as first line of profile

Paula
If you can spell SysAdmin then you is one - anon
Pete Randall
Outstanding Contributor

Re: Determining Where Script Was Started From

Clay,

Forgive my density, but I'm not following.


echo "${0}"
ksh
$ sh
$ echo "${0}"
sh


Pete

Pete
Steven E. Protter
Exalted Contributor

Re: Determining Where Script Was Started From

Pete,

I think my /etc/profile shows you can do testing.

We have stuff coming down and hitting our building from nearby construction sites. I'm bailing to an alternate location if I can find one.

The windy city is at 60 mph and dangerous.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor

Re: Determining Where Script Was Started From

Here's the deal:

When executing as a login shell, ${0} is "-sh" but otherwise it is simply "sh".

Here's a pretty good test of what I am talking about:
telnet dumbo

$ echo ${0}
-sh
$ sh
echo ${0}
$ sh
If it ain't broke, I can fix that.
Pete Randall
Outstanding Contributor

Re: Determining Where Script Was Started From

Clay,

True. I can make that work if I telnet to the box. However, we login via CDE and have hpterm/dtterm windows opened - they do not behave this way.


Pete

Pete