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
# 1  
Old 01-18-2006
how to detect my script is already running

I have a script which must not be run more than once at any given time. THis script will be scheduled to run every 20 mins as a cron job.

In my script can i have logic to say if this script is already running from the previous cron, then exit.

How do i go about doing that. If you describe the approach, can you please specify just the commands, which i can man to construct my approach.
Because I am not familiar with unix commands, if you tell me "find the file and do stuff..." i may not know the command to "find the file".

thanks!
# 2  
Old 01-18-2006
Use ps to list running processes and use grep to filter the list...
Code:
ps -ef|grep '[m]yscript'

# 3  
Old 01-19-2006
Quote:
Originally Posted by Ygor
Use ps to list running processes and use grep to filter the list...
Code:
ps -ef|grep '[m]yscript'

Ygor, this would list the processes whose effective userid is not that of the current user id,

instead,

Code:
ps -fu $LOGNAME | grep '[m]yscript'

# 4  
Old 01-19-2006
You can also create a lock file with your current process PID. When starting your script, check for the lock file and, if found, retrieve the PID and check to see if the process is still running with ps -fp PID. I have encountered instances where ps -ef or -fu provided misleading results. The lock file method has never failed to work.
# 5  
Old 01-19-2006
To add to the above stuff, if you checking with ps -ef for running process, you should also consider the other stuff like... if some opened the script with vi, or doing more with the executable or doing more with the log file name that contains executable name . log etc.... it will also come on ps -ef and your condition becomes true though your script is not running actually.

ps -fu $LOGNAME | grep '[m]yscript' | egrep -v "more | cat | vi | pg | ls | ......."

so better having lock file if possible, otherwise, use the above and do egrep -v with all possible combinations.
# 6  
Old 01-20-2006
Quote:
Originally Posted by rmulchandani
I have a script which must not be run more than once at any given time. THis script will be scheduled to run every 20 mins as a cron job.

In my script can i have logic to say if this script is already running from the previous cron, then exit.

How do i go about doing that. If you describe the approach, can you please specify just the commands, which i can man to construct my approach.
Because I am not familiar with unix commands, if you tell me "find the file and do stuff..." i may not know the command to "find the file".

thanks!
write a tmp file to the /tmp directory.
have your script check to see if the file exists, if it does then don't run.


#!/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}
# 7  
Old 01-20-2006
I guess we can't simply touch a file, it should hold the pid of the process and check for that pid and whether it is same process or not.

#!/usr/bin/ksh

lockfile="/application/data/${pname}.lck"

pname="scripname"

if [ -f $lockfile ]
then
ps_cnt = $( ps -ef | grep $pname | fgrep -cf $lockfile )
fi

if [ ! -f $lockfile ] || (( $ps_cnt < 1 ))
then
echo "process not running, restarting it...."
nohup $lockfile &
sleep 5;
ptree $$ | grep $pname | sed 's/^[ ]*\([0-9]*\) .*$/\1/g' > /application/data/${pname}.lck
fi
fi

not tested...
 
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