Re: check program if running

From: Jean-Marc Monnez (monnez.jean-marc@AGORA.MSA.FR)
Date: Wed Jul 03 2002 - 05:59:06 EDT


More precise if you use the -o option to restrict the # of fields :
ps -e -o "comm=" | grep [p]gm_name
If pgm_name is a shell, use "args=" instead of "comm=", because for most
shells the command name is the interpreter name and the script name is
an argument.

If it's a script, the best would be to store the pid in a pid_file at
the beginning of pgm_name, and execute something like :
------------------------------------------------------------------------------------

............
[[ -f "${PID_FILE}" && "$(< ${PID_FILE})" != "$$" && -n "$(ps -o "args="
-p $
(< ${PID_FILE}) 2>/dev/null | grep ${PGM_NAME})" ]] && {
    echo "${PGM_NAME} may not run more than once at the same time.
Stop." >&2
    exit 1
    }
echo $$ >${PID_FILE}
............
------------------------------------------------------------------------------------

I use this in script daemons.

Regards

-- JMM

"Leyden, Joseph" wrote :

> Other than using pf -ef | grep pgm_name
> is there another way to find out if a program
> is running (in background or foreground) so I
> would not bring up another copy of the same program?
>
> TIA
> Joe Leyden



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:16:02 EDT