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?
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.
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:
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.
Instead of searching for a process number you could have your script create a message file when the script is finished running.
EX:
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)
then
quit process
else
rm /tmp/finished and run process
fi
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)