Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-05-2012, 01:15 PM   #1
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
Check if a script is already running by saving its PID.

Somebody might have a more elegant solution, but I thought I would share what I came up with. I made a script that saves its PID in /var/run and then if another instance of the script is executed, it exits without running. Check this out. I welcome any comments or suggestions.
- Raj Upadhyaya
#! /bin/bash
# - Backup Script.
#save the pid of this run
checkPID() {
while read a 
echo "Checking for process: $a" 
for (( ; ; ))
if (ps -ef | awk '{ print $2 " " $3 }'|grep -q $a)
      echo "Found the process, exiting the script"
      exit 1
done < /var/run/
### Main program
if [ -f /var/run/ ] 
   # Check if PID is still running, otherwise ignore it.
echo $BASHPID > /var/run/
Old 10-06-2012, 06:40 AM   #2
Registered: May 2001
Posts: 29,383
Blog Entries: 55

Rep: Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558
Unless I misunderstand this saving a Process Id can be done (should be done) inside the first-running script filling a variable with "$!" right after command execution and writing out the value to a PID file. Then any subsequent process can just check for existence of the PID file and if necessary, restart for instance, check its value with the PID of the current process. Note checking the PID of the current process may be found with 'pidof' if available (or compatible), or 'pgrep processname', or 'pgrep -f "/path/to/process --any-args" if you need to select from say several running processes doing away with any 'ps|grep|grep -v grep'-like gremlins.

function howto() { echo "Here's some Bash scripting guide help:"; }
Old 10-06-2012, 08:45 AM   #3
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
You could check if a process is running with 'kill -s 0' or by checking if directory /proc/$PID exists.
function check_process {
	local PID=$1
	[[ -e /proc/$PID ]] && kill -s 0 "$PID" # could be not both; best arrangement depends on system
* Wonders if any tutorial has this; I discovered it by my own * ... * Browsed and looks like there were some *

Last edited by konsolebox; 10-06-2012 at 08:48 AM.


backup, bash, bash scripting, pid

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the more easier way to check the pid and kill the pid cmx08 Linux - General 5 09-09-2008 10:57 PM
Script To Check Which Scripts Are Running runnerpaul Solaris / OpenSolaris 2 08-13-2008 03:53 AM
How can I check to see if a script is running? slick_willie Programming 13 03-16-2007 10:18 AM
bash script, monitoring running PID? thebover Programming 4 08-26-2004 02:42 PM
[script] check for a running process mikshaw Linux - Software 2 01-13-2004 08:33 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration