Sponsored Content
Top Forums Shell Programming and Scripting Shell Script to find out if a process is running on any all machines Post 302770061 by Corona688 on Thursday 14th of February 2013 10:34:24 AM
Old 02-14-2013
Lock file is a bit of a misnomer here, since it doesn't lock anything. This is what's usually called a PID file. They often belong in /var/run.

Don't bother with ps | grep | awk | sed | cut | kitchen | sink games to check the name. That's not reliable and not portable, one oddly named process will break it, strange systems won't like it. If the PID file and the PID itself exist, trust it.

What's your system? If you're on a system that has /proc/, like Linux, that makes things a bit simpler.

If the script you're launching would be able to create and delete its own PID file, that would make it even more reliable.

I wrote this init script(some task-specific stuff stripped out) a little while ago:

Code:
#!/bin/bash

PIDFILE="/var/run/whatever.pid"

start() {
        # If PID file exists, it can be read, the contents aren't blank, and the process still exists,
        # it's already running.
        if [ -f $PIDFILE ] && read PID < $PIDFILE && [ ! -z "$PID" ] && [ -d /proc/$PID ]
        then
                echo "Already started" >&2
                exit 0
        fi

        echo "starting whatever..." >&2
        nohup whatever & disown
        echo "$!" > $PIDFILE
        echo "started whatever" >&2
}

stop() {
        if [ -f $PIDFILE ] && read PID < $PIDFILE && [ ! -z "$PID" ] && [ -d /proc/$PID ]
        then
                echo "stopping whatever"
                # Give it a polite SIGINT
                kill -INT $PID
                sleep 1

                # If it doesn't die, give it higher signals until it does
                for SIG in QUIT TERM 9
                do
                        [ -d /proc/$PID ] || break
                        kill -$SIG $PID
                        sleep 2
                done

                if [ -d /proc/$PID ]
                then
                        echo "Couldn't stop whatever" >&2
                        return 1
                fi

                echo "stopped whatever" >&2
                return 0
        else
                echo "whatever not started" >&2
                exit 1
        fi
}

case "$1" in
start)  start   ;;
stop)   stop    ;;
*)       echo "unknown option" >&2 ; false ;;
esac

exit $?

I use it to start and stop a micro HTTP server. The script should work in any Bourne shell that has functions, including bash, ksh, dash, and busybox ash. If it complains about 'disown', remove it, some shells don't need that.

Last edited by Corona688; 02-14-2013 at 11:42 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Running a script on multiple machines

To clear the web cache on my web server, I run this command: find $APACHE_HOME/cache/plsql/plsql -type d -name "*" -exec rm -R {} \; To clear the cache on all the web servers(we have 4), I log on to any one machine, clear its cache, ssh to another machine, clear cache etc; Is there any way... (8 Replies)
Discussion started by: nattynatty
8 Replies

2. Programming

to find current running process

Hi All, The scenario is like this: There is a process say "A" which create a child process say "B" if some condition is true and process "A" terminates. "B" invokes some C program say "C" using 'execl' function. The job of program "C" is to keep polling the server until the server will be up.... (2 Replies)
Discussion started by: ranjkuma692
2 Replies

3. UNIX for Advanced & Expert Users

how to check if a process is running in a server from shell script.

I want to write a unix shell script that will check if a process (say debu) is running in the server or not. If no , then send a mail to the corresponding person to start the process??? (2 Replies)
Discussion started by: debu
2 Replies

4. UNIX for Dummies Questions & Answers

How to find the details of the previously running process with PID

OS: Unix or Linux I (only) know the pid of the process which was running earlier (say 5 hrs back) but it is not running now. Is there a way I could find the details of that process? (atleast the name of the process). Please let me know. (2 Replies)
Discussion started by: vijay.d
2 Replies

5. Shell Programming and Scripting

script to monitor process running on server and posting a mail if any process is dead

Hello all, I would be happy if any one could help me with a shell script that would determine all the processes running on a Unix server and post a mail if any of the process is not running or aborted. Thanks in advance Regards, pradeep kulkarni. :mad: (13 Replies)
Discussion started by: pradeepmacha
13 Replies

6. Shell Programming and Scripting

Shell Script, Copy process using find.

Ok, so here is what I am looking for.. Shell script that uses find to look for one days worth of data using the modified date and then copies only those files to a specified directory. I figured I could use grep and the find command to do this. It seems to work just fine from what I can... (4 Replies)
Discussion started by: techjunky
4 Replies

7. Shell Programming and Scripting

Running Shell Script in the cron, background process

Hi, i was looking for an answer for some trouble im having runing a script in the cron, thing is, that when i run it manually it works just fine. But when cron runs it, it just doenst work. I saw a reply on a similar subject, suggesting that the . .profile worked for you, but im kind of... (9 Replies)
Discussion started by: blacksteel1988
9 Replies

8. Shell Programming and Scripting

shell script to find a process by name and kill it

hi, Am a newbie to unix and wasnt able to write script to my requirement. I need a shell script, which should find a process by name and kill it. For eg: let the process name be "abc". I have different processes running by this name(abc), so should kill them all. Condition would be: if... (7 Replies)
Discussion started by: fop4658
7 Replies

9. Solaris

Means to check if some process is running on "n" number of machines

Team, I would like to know, if we have any command in Solaris to verify, if some process is listening on a port on a set of machines. for eg: Wrote the below script, and found that when a process is listening on that port, then it just waits there and doesnt come out. Rather, I would like... (6 Replies)
Discussion started by: msgforsunil
6 Replies

10. Shell Programming and Scripting

Shell script to find out process name that are running for last 10 days.

Hi all, As i am new to shell script.Please help me to write a Shell script to find out process name that are running for last 10 days. Thank's in advance. (8 Replies)
Discussion started by: manas_1988
8 Replies
PIDOF(8)						Linux System Administrator's Manual						  PIDOF(8)

NAME
pidof -- find the process ID of a running program. SYNOPSIS
pidof [-s] [-x] [-o omitpid] [-o omitpid..] program [program..] DESCRIPTION
Pidof finds the process id's (pids) of the named programs. It prints those id's on the standard output. This program is on some systems used in run-level change scripts, especially when the system has a System-V like rc structure. In that case these scripts are located in /etc/rc?.d, where ? is the runlevel. If the system has a start-stop-daemon (8) program that should be used instead. OPTIONS
-s Single shot - this instructs the program to only return one pid. -x Scripts too - this causes the program to also return process id's of shells running the named scripts. -o Tells pidof to omit processes with that process id. The special pid %PPID can be used to name the parent process of the pidof pro- gram, in other words the calling shell or shell script. NOTES
pidof is simply a (symbolic) link to the killall5 program, which should also be located in /sbin. When pidof is invoked with a full pathname to the program it should find the pid of, it is reasonably safe. Otherwise it is possible that it returns pids of running programs that happen to have the same name as the program you're after but are actually other programs. SEE ALSO
shutdown(8), init(8), halt(8), reboot(8) AUTHOR
Miquel van Smoorenburg, miquels@cistron.nl 01 Sep 1998 PIDOF(8)
All times are GMT -4. The time now is 08:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy