LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 10-05-2012, 01:15 PM   #1
Rupadhya
Member
 
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
Code:
#! /bin/bash
# back.sh - Backup Script.
#save the pid of this run
checkPID() {
while read a 
do 
echo "Checking for process: $a" 
for (( ; ; ))
do
if (ps -ef | awk '{ print $2 " " $3 }'|grep -q $a)
then
      echo "Found the back.sh process, exiting the script"
      exit 1
else
      break
fi
done
done < /var/run/back.pid
}
#
### Main program
#
if [ -f /var/run/back.pid ] 
then
   # Check if PID is still running, otherwise ignore it.
   checkPID
fi
echo $BASHPID > /var/run/back.pid
 
Old 10-06-2012, 06:40 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.

Code:
function howto() { echo "Here's some Bash scripting guide help:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls"; }
 
Old 10-06-2012, 08:45 AM   #3
konsolebox
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.
Code:
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.
 
  


Reply

Tags
backup, bash, bash scripting, pid



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:52 AM.

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