- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- check process
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
04-10-2007 05:48 PM
04-10-2007 05:48 PM
if "it is a crontab job" , then
do
xxx
done
else
do
yyy
done
how to know whether it is a crontab job ?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 06:57 PM
04-10-2007 06:57 PM
Re: check process
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 07:12 PM
04-10-2007 07:12 PM
Re: check process
/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 08:20 PM
04-10-2007 08:20 PM
Re: check process
if [ ! -t ]; then
echo "it is a crontab job"
else
echo "NOT a crontab job"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 08:57 PM
04-10-2007 08:57 PM
Re: check process
This won't work if stdout is redirected. Using -t 0 would check stdin, which may not be safer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 09:07 PM
04-10-2007 09:07 PM
Re: check process
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 09:10 PM
04-10-2007 09:10 PM
Re: check process
>This won't work if stdout is redirected.
Works for me...I have a batch job with similar logic and redirected output.
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 09:28 PM
04-10-2007 09:28 PM
Re: check process
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
----------------------------
This will display the process PID which is generated by cron.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 09:48 PM
04-10-2007 09:48 PM
Re: check process
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 09:58 PM
04-10-2007 09:58 PM
Re: check process
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2007 10:18 PM
04-10-2007 10:18 PM
SolutionI 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 12:32 AM
04-11-2007 12:32 AM
Re: check process
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 01:22 AM
04-11-2007 01:22 AM
Re: check process
#> ./test >/dev/null 2>&1
I said:
This also fails if stdin is redirected.
So your script (tty) will fail with:
$ ./test < /dev/null
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 02:30 AM
04-11-2007 02:30 AM
Re: check process
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 05:22 AM
04-11-2007 05:22 AM
Re: check process
if [ "$(tty)" = "not a tty" ]; then
echo "it is a crontab job"
else
echo "NOT a crontab job"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2007 03:30 PM
04-11-2007 03:30 PM
Re: check process
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2007 03:33 PM
04-12-2007 03:33 PM
Re: check process
#!/usr/bin/sh
tty -s
if [ $? -ne 0 ]; then
echo "it is a crontab job"
else
echo "NOT a crontab job"
fi