Article View: comp.unix.shell
Article #1Re: Help in programming a shell...
From: chris ross
Date: Tue, 27 Oct 1992 13:17
Date: Tue, 27 Oct 1992 13:17
55 lines
1940 bytes
1940 bytes
root@fns.gold.sub.org (Peter Bechtold) writes:
> jkscott@EOS.NCSU.EDU (JENNIFER KAYE SCOTT) writes:
>>I need to program a shell script that can check to see if a certain process
>>is running, and if it noe, then start up the process.
>Try this :
>#!/bin/sh
>if test -z "`ps -e|grep PROCESSNAME`"
>then
> exec PROCESSNAME
>else
> echo "PROCESSNAME is running"
>fi
Slight flaw here:
$ sleep 10 &
16151
$ ps | grep sleep
16151 q0 S 0:00 sleep 10
16152 q0 S 0:00 grep sleep
$
You need to run the output of the first grep thru "grep -v grep".
Also, if your grep returns a status, you can just say
if ps | grep $PROCESSNAME | grep -v grep >/dev/null; then
...
Unfortunately, this _still_ isn't reliable. Even if the target
process isn't running, the string you're searching for may be present
as part of the command line of another process.
Here's a better solution. Given the following output from ps
PID TT STAT TIME COMMAND
14807 p5 IW 0:00 -sh (csh)
15881 p5 IW 0:02 nn
...
where the command starts in column 5, do
if ps | awk "\$5 ~ /^-?(.*\/)?$PROCESSNAME\$/ { S=0; }
END { exit(S); }" S=1 -; then
echo foo
fi
which will, for example, find "sh" if it appears as "sh, "-sh", or
"/bin/sh", but not as "rsh" or (say) "emacs showme.txt".
chris ross <cjross@bbn.com> BBN Advanced Simulations (617) 873-3272
WARNING: article may contain flammable material. Do not expose to open
flames. In case of accidental ignition, douse keyboard with water.
Technical Details
Message-ID:
<leqgb4INNj9m@news.bbn.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!noc.near.net!news.bbn.com!news.bbn.com!newsReferences:
<1992Oct25.050628.20672@ncsu.edu> <1992Oct25.203301.1824@fns.gold.sub.org>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!noc.near.net!news.bbn.com!news.bbn.com!news From: cjross@bbn.com (chris ross) Newsgroups: comp.unix.shell Subject: Re: Help in programming a shell... Date: 27 Oct 1992 13:17:24 GMT Lines: 55 Message-ID: <leqgb4INNj9m@news.bbn.com> References: <1992Oct25.050628.20672@ncsu.edu> <1992Oct25.203301.1824@fns.gold.sub.org> NNTP-Posting-Host: bbn.com