Has my script finished?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Has my script finished?
# 1  
Old 04-29-2002
Question Has my script finished?


I'm writing a script which will be run by cron every X minutes.

I don't want cron to run my script again if the previous one has not yet finished.

When the script first runs, I had the idea to store the Process ID in a file. When cron tries to run the script again, I would check the file to see if the previous process ID still existed.

My dilemma is the following:

1. How likely is it that a totally seperate process may have taken the original PID in the file, hence I will be checking the wrong process.

2. Is there any better way to see if my script has finished?

Helen Smilie
# 2  
Old 04-29-2002
here is something someone posted a while back, I couldn't find the thread but had saved the reply:

Easiest way is:

ps -ef | grep "[l]d_data" || ld_data

That will run the program "ld_data" if and only if it is not found already running. Placing the first character within square brackets protects the "grep" command line from being found and triggering a false positive.
# 3  
Old 04-29-2002
Nice little tool, although in this case it doesn't seem to work.
For some reason the scriptname is not displayed just '-sh'

ie if I do a ps -ef and grep for the process ID I get

helen 25452 16598 255 16:34:53 pts/tg 1:57 -sh

I thought the script name should appear insead of -sh.

Any ideas?

# 4  
Old 04-29-2002
In a korn shell you can do the following:

Add to your script a lock condition.


while [ `ps -ef |grep your_script_name |grep -v grep |wc -l ` -gt 1 ]
echo "waiting to the end of the previous instance"
sleep 10

#your code

Regards. Hugo.
# 5  
Old 04-29-2002
There is no perfect solution to this problem. Someone else on the system might be running an identically named script. And pids do recycle.

Here is a quick script that illustrates my solution:
#! /usr/bin/ksh

if [[ -f lockout.pid ]] ; then
        if kill -0 $(cat lockout.pid) ; then
                echo still running
                exit 0
echo $$ > lockout.pid
trap "rm lockout.pid" 0
sleep 30

Is the script is functioning properly, it will create and delete the lockout.pid file correctly. If the lockout.pid file exists, then the process should also exist.

If the script is abruptly killed and cannot remove the lockout.pid file then the next instance of the script will detect it. The new instance will try to send signal zero to the old pid. If the script is not running as root, it will not be able to signal another user's process.

If the previous instance of the script finished normally, the new instance of the script cannot be fooled. If the previous instance of the script is still running, the new instance of the script cannot be fooled. These are the normal conditions we expect to find.

So to fool the script, first it must abort, second the pid must have recycled and the pid must now be in use by a new, unrelated process, and third, the new process be belong to the same user that is running the script. The third condition is lifted if root runs the script.
# 6  
Old 07-23-2002
if your still looking

Instead of searching for a process number you could have your script create a message file when the script is finished running.

echo 'finished' > /tmp/finished

If you do this you will have to edit the begining of the script also. You will have to search for the created file and if it doesn't exist stop the process from running again if it does remove it and continue running the script.

*NOTE: it is important to remove the file BEFORE running the script again.

General outline:

search for /tmp/finished

if (not there)
quit process
rm /tmp/finished and run process

echo 'finished' > /tmp/finished
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[BASH] Script to manage background scripts (running, finished, exit code)

Heyas, Since this question (similar) occur every now and then, and given the fact i was thinking about it just recently (1-2 weeks) anyway, i started to write something :p The last point for motivation was... (17 Replies)
Discussion started by: sea
17 Replies

2. Shell Programming and Scripting

How to make sure that sub shells have finished before moving on?

Hello, I have a script that is running multiple instances of an application in parallel. # learn on f0 emergent -nogui -p $ScriptLoc/$PROJ fold_tag=f0 & sleep 5 # learn on f1 emergent -nogui -p $ScriptLoc/$PROJ fold_tag=f1 & sleep 5 # learn on f2 emergent -nogui -p... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

3. Shell Programming and Scripting

Wait until firefox has finished

I am running a macro script from the command line. But the script doesn't wait until the task has finished. firefox imacros://run/?m=macro_script.iim firefox imacros://run/?m=macro_script2.iim How do I get it to wait until the macro has been completed? I am using imacros, a firefox... (1 Reply)
Discussion started by: locoroco
1 Replies

4. Shell Programming and Scripting

how to proceed when curl is finished

I have a script which uses cli curl to download the source code of a webpage and then tests if a specific string exists in the source. The problem is that the website has a slow response, so the eval expression hasn't completed when the test starts. The test returns a negative, and the curl... (8 Replies)
Discussion started by: locoroco
8 Replies

5. Shell Programming and Scripting

check if file finished to copy

Hi all, I have a script that is monitoring a hot folder. This script works fine with one exception when the script is executed while a file is being copied to the hot folder. What is the easiest method to check if the copy file is completed? I'd like to get the solution in bash :) (8 Replies)
Discussion started by: gigagigosu
8 Replies

6. Shell Programming and Scripting

How do I know all processes are finished?

Hi all, I am writing a C shell script that starts a program. The program forks of several child processes. Only when all child processes are done, I want to archive my log files. Below is what I have so far, but unfortunately it doesn't work. MyProgram if (-e processes.txt) then rm... (2 Replies)
Discussion started by: Carla
2 Replies

7. Shell Programming and Scripting

How to know a executable has finished his task

Hi frnds, I want to know is there a way by which we can know that a C++ executable has finished its job in shell script. My task is as follows: 1.Shell script calls a executable 2.Executable executes and performs its job of generating some reports. Now i want my shell script to... (4 Replies)
Discussion started by: electroon
4 Replies

8. Shell Programming and Scripting

** Finished ** Syncid.rc

So, the script I've been working on, since I was starting to learn Shell scripting is now complete. This was coded in ksh, and I am very proud of it. What this script does, is syncs up uid's across the network. So if you have 10 servers, with 10 usernames with different UID's - this will... (1 Reply)
Discussion started by: syndex
1 Replies

9. UNIX for Dummies Questions & Answers

background job finished notification

In my last job someone gave me the command to put in my .profile that let me know when a job I had running in the background finished. It was a word about 5 char long. I can't remember it! (4 Replies)
Discussion started by: nkeller
4 Replies

10. UNIX for Advanced & Expert Users

Finding Out When A Process Has Finished?

Problem I have an application which basically runs lots of UNIX programs remotely, using the Telnet protocol. For each program it remotely executes, it stores the process ID (PID) for that process. At regular intervals, I would like my application to take the PID for every process still... (5 Replies)
Discussion started by: 1cuervo
5 Replies
Login or Register to Ask a Question