How to check if process is running?

Hi

I would like to check if an instance of a script is already running.

I've seen many different examples, but I haven't the slightest idea as to how to do this.

Can you please help.

Thank you.

u can use ps command

ps -ef|grep "process name"

A traditional way is for a process to create a PID file under /var/run/. I create it under /tmp/ just as an example that can run as any user. I also use a fifo as a lock file to prevent a race condition.

#!/bin/bash

function rm_pidfile
{
        rm -f /tmp/myscript.pid
}

function is_running
{
        # File does not exist, we can run
        [ -f /tmp/myscript.pid ] || return 1
        # Read PID from file
        read PID < /tmp/myscript.pid
        # File exists but process does not.
        [ -d /proc/${PID} ] || return 1
        return 0
}

# This guarantees other scripts will wait for each other
# before checking the pidfile.  otherwise one might create the PID
# file while we're not looking.
while ! mkfifo /tmp/myscript.lock 2> /dev/null
do
        sleep 1
done

# beyond this point, /tmp/myscript.lock exists and belongs to us.  Other
# instances of the script will loop.

if is_running
then
        echo "An instance(PID ${PID}) is already running"
        rm -f /tmp/myscript.lock
        exit 1
fi

# create PID file
echo "$$" > /tmp/myscript.pid

# make sure we delete it when the program quits
trap "rm_pidfile" EXIT

# Remove the lock file
rm -f /tmp/myscript.lock

# the rest of the code goes here

while true
do
        sleep 1
done

---------- Post updated at 03:41 AM ---------- Previous update was at 01:23 AM ----------

[/COLOR]I have altered my script a bit .... now I get syntax errors.

To me this script looks fine.

Can somebody please assist me in finding what the problem is.

Thank you

 
#!/bin/bash
#---------------------------------------------------------------------------------------------------------------------- 
#This script allows the user to import a dsx file 
#
# Usage:
#./import.sh -s -d -r -f
#
#-----------------------------------------------------------------------------------------------------------------------
dbname="$dbname"
dsn="$dsn"
dsxfile="$dsxfile"
rangechk="$rangechk"
verbose=""
reverb=""
module=`basename $0`
DSXLOG_MODULE=$module 
export DSXLOG_MODULE
dsxdir="/var/local/dsx/import"
dsximp="/usr/local/bin/dsximp.exe"
dsxpids="/var/run"
function rm_pidfile
{ 
rm -f $dsxpids/$module.pid 
} 
function is_running 
{ 
# File does not exist, we can run 
[ -f $dsxpids/$module.pid ] || return 1 
# Read PID from file 
read PID < -f $dsxpids/$module.pid 
# File exists but process does not. 
[ -d /proc/${PID} ] || return 1 return 0 
} 
#------------------------------------------------------------------------#
# This guarantees other scripts will wait for each other 
# before checking the pidfile. otherwise one might create the PID 
# file while we're not looking. 
#------------------------------------------------------------------------#
while ! mkfifo -f $dsxpids/$module.lock 2> /dev/null 
do sleep 1 done 
#------------------------------------------------------------------------#
# beyond this point, /var/run/import.sh.lock exists and belongs to us. 
# Other instances of the script will loop. 
#------------------------------------------------------------------------#
if is_running then echo "An instance(PID ${PID}) is already running"
rm -f $dsxpids/$module.lock 
exit 1
fi
echo "$$" > -f $dsxpids/$module.pid
trap "rm_pidfile" EXIT
rm -f $dsxpids/$module.lock
while true do sleep 1 done
function displayHelp()
{
echo ""
echo " Usage: ./import.sh [ -s -d -r -f ]"
echo ""
echo " -s, --dsn identified by the Data Source Name "
echo " -d, --dbname identified by the Database Name "
echo " -r, --rangechk specify range checking option " 
echo " -f, --dsxfile identified by the file in dsx format "
echo " -h, --help identified by the help menu "
echo ""

}
while getopts " s: d: r: f: e h " option
do
case $option in

f ) dsxfile="$OPTARG";;
d ) dbname="$OPTARG";;
s ) dsn="$OPTARG";;
r ) rangechk="$OPTARG";;
e ) verbose="--verbose"
reverb="--echo";;
h | ? | * ) displayHelp;exit;;

esac;
done
if [[ $# -eq 0 ]]; then
displayHelp
exit 1
fi
 

Get what syntax errors?

[edit] OK, I see some obvious ones. My line breaks weren't just ornamental, this:

while true do sleep 1 done

is not the same as

while true
do
        sleep 1
done

If you want to cram it on one line, use semicolons:

while true ; do sleep 1 ; done

You'll need to fix the other while loop as well, and that if-statement.

And please remove the "while true ; do sleep 1 ; done". Take a close look at what it does.

Also

if ps aux | grep '[/]usr/sbin/daemon' then      echo Running else      /usr/sbin/daemon & fi