- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Determining Where Script Was Started From
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-05-2004 04:15 AM
03-05-2004 04:15 AM
Determining Where Script Was Started From
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:22 AM
03-05-2004 04:22 AM
Re: Determining Where Script Was Started From
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:27 AM
03-05-2004 04:27 AM
Re: Determining Where Script Was Started From
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:28 AM
03-05-2004 04:28 AM
Re: Determining Where Script Was Started From
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:30 AM
03-05-2004 04:30 AM
Re: Determining Where Script Was Started From
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:31 AM
03-05-2004 04:31 AM
Re: Determining Where Script Was Started From
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:32 AM
03-05-2004 04:32 AM
Re: Determining Where Script Was Started From
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:36 AM
03-05-2004 04:36 AM
Re: Determining Where Script Was Started From
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:39 AM
03-05-2004 04:39 AM
Re: Determining Where Script Was Started From
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:45 AM
03-05-2004 04:45 AM
Re: Determining Where Script Was Started From
I'm still trying to figure that one out. It's a good exercise for a friday.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:45 AM
03-05-2004 04:45 AM
Re: Determining Where Script Was Started From
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:53 AM
03-05-2004 04:53 AM
Re: Determining Where Script Was Started From
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 04:56 AM
03-05-2004 04:56 AM
Re: Determining Where Script Was Started From
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
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
#
#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:02 AM
03-05-2004 05:02 AM
Re: Determining Where Script Was Started From
Good job SRI !
Congrats on the new salad !
Must just has happened !
Reading 20017 pts 20 secs ago !
Go on
Volker
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:06 AM
03-05-2004 05:06 AM
Re: Determining Where Script Was Started From
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:19 AM
03-05-2004 05:19 AM
Re: Determining Where Script Was Started From
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:24 AM
03-05-2004 05:24 AM
Re: Determining Where Script Was Started From
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:32 AM
03-05-2004 05:32 AM
Re: Determining Where Script Was Started From
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:32 AM
03-05-2004 05:32 AM
Re: Determining Where Script Was Started From
Good Question - its got me thinking.
Fuser on /etc/profile in /etc/profile at start,
fuser -v > /tmp/test-profile
????
Paula
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:33 AM
03-05-2004 05:33 AM
Re: Determining Where Script Was Started From
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:34 AM
03-05-2004 05:34 AM
Re: Determining Where Script Was Started From
fuser -v /etc/profile > /tmp/test-profile
as first line of profile
Paula
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:36 AM
03-05-2004 05:36 AM
Re: Determining Where Script Was Started From
Forgive my density, but I'm not following.
echo "${0}"
ksh
$ sh
$ echo "${0}"
sh
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:40 AM
03-05-2004 05:40 AM
Re: Determining Where Script Was Started From
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:44 AM
03-05-2004 05:44 AM
Re: Determining Where Script Was Started From
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2004 05:48 AM
03-05-2004 05:48 AM
Re: Determining Where Script Was Started From
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