how to detect my script is already running


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to detect my script is already running
# 8  
Old 01-20-2006
Try this

Code:
#!/usr/bin/ksh

#
# Check script in use
#

LockFile=/tmp/$(basename $0).lck
echo $$ >> $LockFile
while read Pid
do
   [ "$Pid" -eq $$ ] && break
   [ -z "$(ps -p $Pid -opid=)" ] && continue
   echo "Script in use (by pid $Pid), exiting..."
   exit
done < $LockFile
trap "rm $LockFile" EXIT

#
# Script code
#

. . . . . . .

# 9  
Old 01-20-2006
Quote:
Originally Posted by aigles
Try this

Code:
#!/usr/bin/ksh

#
# Check script in use
#

LockFile=/tmp/$(basename $0).lck
echo $$ >> $LockFile
while read Pid
do
   [ "$Pid" -eq $$ ] && break
   [ -z "$(ps -p $Pid -opid=)" ] && continue
   echo "Script in use (by pid $Pid), exiting..."
   exit
done < $LockFile
trap "rm $LockFile" EXIT

#
# Script code
#

. . . . . . .

This is similar to the method that I use and so far, nothing has ever conflicted since I ironed out one wrinkle. I haven't tested this particular implementation and maybe it doesn't have the same hole that I found in my first attempt but, you may want to included ps -fp $pid | grep yourscript to eliminate the possibility of other processes giving you false positives; some systems can reuse PIDs fairly quickly. My Solaris implementation was plagued by false positives for a month (between release cycles) until I added the grep.
# 10  
Old 01-24-2009
Doesn't Work

The temp file solution doesn't work, since if the script is interrupted or crashes out before the final line, the temp file is never deleted and you'll always think the script is running.
# 11  
Old 01-25-2009
Quote:
Originally Posted by ChaosFreak
The temp file solution doesn't work, since if the script is interrupted or crashes out before the final line, the temp file is never deleted and you'll always think the script is running.
use 'trap' to catch an interrupt.

look into 'man fuser'
# 12  
Old 01-26-2009
Quote:
Originally Posted by vgersh99
use 'trap' to catch an interrupt.

look into 'man fuser'
Yes, sorry I meant that the temp file version posted by DogDay doesn't work:

Quote:
Originally Posted by DogDay
#!/bin/sh

# our tmpfile
tmpfile="/tmp/mytmpfile"

# check to see if it exists.
# if it does then exit script
if [[ -f ${tmpfile} ]]; then
echo script already running.
exit
fi

# it doesn't exist at this point so lets make one
touch ${tmpfile}

# do whatever now.

# end of script
rm ${tmpfile}
Of course, the version posted by aigles with PID check and "trap" works perfectly every time.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to detect url in use in a script?

Hello, I have a small script and it runs from web application in below format: pipe:///path_to_myscript.sh url1 url2 url3 myscript.sh: #!/bin/bash count=0 while do count=$((count+1)) exec 3>&1 ((ffmpeg -i $1 ...... -f mpegts pipe:1 2>/dev/null 1>&3 ) 2>&1 | \ while read LINE; do echo... (9 Replies)
Discussion started by: baris35
9 Replies

2. HP-UX

Script to detect time drift

Hello there, I am not an expert in networking related stuff but I got a requirement to create UNIX script to query our Company's internal time source via NTP for time drift detect and report it when > +/- 50ms. I have been googling a lot but thought to post it in this forum to get a... (17 Replies)
Discussion started by: Green_Star
17 Replies

3. UNIX for Beginners Questions & Answers

Script to detect time drift on server

Hello there, I am not an expert in networking related stuff but I got a requirement to create UNIX script to query our Company's internal time source via NTP for time drift detect and report it when > +/- 50ms. I have been googling a lot but thought to post it in this forum to get a... (1 Reply)
Discussion started by: Green_Star
1 Replies

4. UNIX for Advanced & Expert Users

A script to detect system type

Hi forum, So I am trying to determine the OS type with the following script: #!/usr/bin/sh OStype1=`uname -s` Sunos1=SunOs if then echo "This system is Linux" exit 0 elif then echo "This system is SunOs" exit 0 elif (1 Reply)
Discussion started by: dampio
1 Replies

5. Shell Programming and Scripting

Htop - How to detect exact command running at background

Hello, I am running a python file from terminal and I wish to see which code is running at background. When I use htop, I see just a few commands, unable to see entire command. htop > report nano report Output: ^ Following parts of ffmpeg line is not shown by htop. Just showing... (7 Replies)
Discussion started by: baris35
7 Replies

6. Shell Programming and Scripting

Help with detect with regex and move script

Hi all, I am needing some help with a script that will search for a video file by known extensions and then do a pattern search (I'm guessing via regex) and then based on a match of one type of another move the file to an assigned directory. I would like to do this with either a shell script... (7 Replies)
Discussion started by: Simplify
7 Replies

7. Shell Programming and Scripting

Detect current shell inside a script

I wish to print my current shell which happens to be bash in my script check.sh more check.sh echo $0 echo `ps -p $$` But instead of printing it prints check.sh i.e the name of the script for both the commands. Can you please suggest how to print the current shell i m on inside the... (2 Replies)
Discussion started by: mohtashims
2 Replies

8. Shell Programming and Scripting

Detect if script starts from queue

Dear community, what I'm try to do is deny users to run a script without parameters from command bash, but the same script should run without parameters only from crontab. Example runs by crontab:*/5 * * * * /tmp/script.sh Here the normal execution starts every 5 minutes Example #1 runs by... (16 Replies)
Discussion started by: Lord Spectre
16 Replies

9. Shell Programming and Scripting

AWK script to detect webpages from file

Hi guys I'm very new to unix and I have to create an awk script that detects webpage addresses from a file/webpage and outputs how many times each webpage was detected.e.g. if my file was: www.google.com www.facebook.com www.google.com the output should be: www.google.com x2... (2 Replies)
Discussion started by: ROFL
2 Replies

10. Shell Programming and Scripting

AWK script to detect webpages from file

Hi guys I'm very new to unix and I have to create an awk script that detects webpage addresses from a file/webpage and outputs how many times each webpage was detected.e.g. if my file was: (Note: The symbol " was added to stop them being created into links) "www.google.com"... (1 Reply)
Discussion started by: ROFL
1 Replies
Login or Register to Ask a Question