- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Test for process already running and backquotes
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
08-19-2004 11:16 PM
08-19-2004 11:16 PM
Test for process already running and backquotes
if [ 1 != `ps -fu$USER | grep mpr0200.sh | grep -v grep |wc -l` ] ; then
# then mpr0200 is already running ABORT this run
echo
echo "****** ABORTING MPR0200 ********"
echo "****** ********"
echo "****** MPR0200 IS ALREADY RUNNING ********"
echo
exit 3
fi
The problem seems to be that the script will occasionally and unpredictably detect itself and abort, even when no other copy is running.
I suspect that this is a problem caused by the backquote operator. According to the man page for ksh, the use of backquotes may cause a separate process to be created, although the exact conditions when this happens are not clear to me.
So, assuming that the backquotes cause a separate process to be forked, and that the forked process is initially an exact clone of the parent until the actual test command is exec'd, I think that sometimes the test is finding the earlier (pre-exec'd) process.
Feasible? If so how do I fix ? Is there a better way for a script to check that another copy is not already running ?
I know that the use of backquotes is deprecated in favour of $(command), but this is a 3rd party application which I cannot change without invalidating my support contract.
Except I'll probably have to to fix this glitch.
Thanks in advance
-- Graham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2004 11:20 PM
08-19-2004 11:20 PM
Re: Test for process already running and backquotes
Clearly your wc -l expects "1" to return (ie the current process) so you'd need to change it to look for "0", but this should work i think.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2004 11:21 PM
08-19-2004 11:21 PM
Re: Test for process already running and backquotes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2004 01:02 AM
08-20-2004 01:02 AM
Re: Test for process already running and backquotes
Get the script to check if a trigger files exists - if it does, the script is already running, and this run can abort, otherwise, create the trigger and continue;
#!/usr/bin/sh
TRIGGER=/tmp/trigger.txt
if [ -f $TRIGGER ]
then
echo "MPR0200 already running. Aborting"
exit 3
else
echo "MPR0200 not running. Creating trigger."
echo "MPR0200_NOW_RUNNING" > $TRIGGER
fi
Then just rm $TRIGGER once the rest of your script is complete.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2004 01:32 AM
08-20-2004 01:32 AM
Re: Test for process already running and backquotes
a trap command can help this at the beginning of the script: -
if [ -f $TRIGGER ]
then
echo "MPR0200 already running. Aborting"
exit 3
else
echo "MPR0200 not running. Creating trigger."
echo "MPR0200_NOW_RUNNING" > $TRIGGER
trap "rm $TRIGGER" 0
fi
This will trap the exit of the script (whether killed, error, normal exit etc) and remove the trigger file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2004 01:55 AM
08-20-2004 01:55 AM
Re: Test for process already running and backquotes
And more the format you used,
ps -fu$USER | grep mpr0200.sh | grep -v grep |wc -l
is not effective one.
You can avoid current process from being check as,
ps -fu $USER | grep -v grep | grep -v $$ | grep mpr02000.sh | wc -l
It is good to check with shell too as,
ps -fu $USER | grep -v grep | grep -v $$ | grep mpr02000.sh | grep sh | wc -l
Else,
Use a separate file as,
mpr02000.pid as like other services. Make an entry over there. If you are going to stop the service then remove entry there or make null.
Example script:
===============
if [[ ! -z /var/tmp/mpr02000.pid ]]
then
# then mpr0200 is already running ABORT this run
echo
echo "****** ABORTING MPR0200 ********"
echo "****** ********"
echo "****** MPR0200 IS ALREADY RUNNING ********"
fi
NOTE:
You have to make null the file while exiting from running instance.
Before Exiting make as,
> /var/tmp/mpr02000.pid
collect the process ID $$ and make an entry if there is not pid there out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2004 02:55 AM
08-20-2004 02:55 AM
Re: Test for process already running and backquotes
Ahhh, my favourite piece of code, the infamous "am I running already?" script.
I have had endless arguments about this one, for using lock files, process tables, database queries and all that.
Here's the quick and dirty code that works 99.999% of the time (the 5 nines).
export PID=$$
export LNCOUNT=`ps -ef |grep [script name] |grep -v grep |awk -vpid=$PID '$2 != pid && $3 != pid '{print $0}' |wc -l`
Ignores all occurences of grep and all processes either being this process or spawned by this process.
The odd exception where this does not work is if there is a 3rd level ov shell spawning involved, which is very rare but I have seen it (SAP Automated Archives with Data Protector). In that case, then the complexity is so great that a C program should be written.
Share and Enjoy! Ian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2004 03:15 AM
08-20-2004 03:15 AM
Re: Test for process already running and backquotes
(looking for POSIX shell processes: sh)
# ps -e | grep sh
4 ? 2:44 unhashdaemon
558 ? 0:00 sshd
12181 pts/td 0:00 sh
12233 pts/td 0:00 csh
12117 pts/tc 0:00 sh
11810 pts/tb 0:00 sh
12232 pts/td 0:00 ksh
11756 pts/ta 0:00 sh
(hummm, that's not what I wanted)
# UNIX95= ps -C sh
PID TTY TIME CMD
12117 pts/tc 00:00 -sh
11810 pts/tb 00:00 -sh
11756 pts/ta 00:00 -sh
Now that's much better. grep looks at everything, user names, pathnames, portions of process names. That's why unhashdaemon, ksh and csh were caught. We want only "sh" processes. BTW: the - in front of sh means that this process is a login shell.
So for your case:
UNIX95= ps -fC mpr0200.sh
Now you'll see exactly *all* of the mpr0200.sh processes and not: mpr0200.sh1 or mpr0200.share, etc. Note that -C finds all mpr0200.sh processes so now you can safely match based on username:
USER=bill
QTY=$(UNIX95= ps -fC mpr0200.sh | grep ^$USER | wc -l)
if [ $QTY -gt 0 ]
then
echo
echo "****** ABORTING MPR0200 ********"
echo "****** ********"
echo "****** mpr0200.sh quantity $QTY ALREADY RUNNING ********"
echo
exit 3
fi
The above version will also detect if more than 1 copy is running and report the quantity for that user.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2004 03:22 AM
08-20-2004 03:22 AM
Re: Test for process already running and backquotes
Bill Hassell, sysadmin