Making sure only one instance of a script is running
so i have a script that takes a while to complete and its cpu intensive. this script is being used by several users. i want to make sure only 1 user can run this script at any given time.
i originally thought of running a while loop to egrep the process table of the PID ($$) of the process, but im not sure if that's going to be efficient. i want to make sure the script does not mistaking someone editing the script as a running process. so im excluding the common editing programs.
im searching for a solution that will be usable across all unix systems. here's what i have tried so far:
You could try using a .pid file and check it at the top of your script like this:
All designs that try to check for a lock-file or pid-file are flawed. They are subjective to racecondition
The problem is that you can not check for the existence of a file and the creation of that file in an atomic action. And in between of those two actions another instance can run.
I use this block of code early in scripts that need to be run separately:-
Naturally, I use a suitable file name for the lock file so that it is easy to recognise and re-use/avoid for other scripts. It seems to work for me. The tracefile variable and other things have already been set.
The -w flag on flock means that it will wait on file descriptor 13 for 30 seconds trying to get an exclusive lock (the -x flag) The file descriptor is left open by the >> redirector so the lock persists.
If the process is killed off ungracefully (i.e. a kill -9) then the file is then closed and the lock released, else it is closed when the script terminates gracefully or can be closed earlier if that is appropriate with exec 13>&- It negates the need for the trap ....... 0 which leaves you free to use it for something else if that is appropriate.
The OP is after a portable solution, so lets test for executable /usr/bin/flock
and use that when available but revert to the mkdir method on systems without it:
These 2 Users Gave Thanks to Chubler_XL For This Post:
All designs that try to check for a lock-file or pid-file are flawed. They are subjective to racecondition
The problem is that you can not check for the existence of a file and the creation of that file in an atomic action.
Actually - you can. The underlying filesystem calls support it (O_CREAT | O_EXCL), some shells have extensions for it, and for those who don't, there are ways to cheat a little - mkdir and mkfifo are atomic, they succeed and create a new filename or fail and don't, you can use them to "mutex" the creation of a PID file.
That a race condition may exist doesn't actually make ps | grep | awk | sed | cut | kitchen | sink | when | will | the | madness | end better than PID files, either. That's far from atomic, itself. It's also complicated, brittle, unreliable, and unportable on top of that.
There's a reason PID file is a de facto standard.
Last edited by Corona688; 01-17-2017 at 06:28 PM..
Hi Guys,
I have a main_script.sh which runs every day and scheduled in crontab.
in the main script i read data from config file
test.config
apple
mango
orange
main_script.sh
for i in `cat test.config`
do
if
then
echo 'Apple' (3 Replies)
Hello All,
Greetings !!
I have a query here to all is as follows:
Question: Let's say we are running a script in a UNIX box and we have opened an another session and then made changes in script of some statements NOT to be print some values(just an example) so when I am monitoring the... (5 Replies)
Hi,
I want to write one program in C in Unix OS which will check the running status of a process time to time. If the process is stopped somehow by any means, it will ensure that the process is restarted and only one copy of the process image should run in memory at any point of time for the user.... (2 Replies)
I have run 10 instances of the process eg, process name is BG
nohup /WP01IRB1_irbapp/IRBWPROD/RB/bin/BG -c 1 -t 23 -a '-caTop TESTBILLCYCLE='5FEB13_81PT19NPT''>a.txt &
nohup /WP01IRB1_irbapp/IRBWPROD/RB/bin/BG -c 2 -t 23 -a '-caTop TESTBILLCYCLE='5FEB13_81PT19NPT''>b.txt &
nohup... (3 Replies)
Hi All,
After reading that the sort command in Linux can be made to use many processor cores just by using a simple script which I found on the internet, I was wondering if I can use similar techniques for programs like the awk and sed?
#!/bin/bash
# Usage: psort filename <chunksize>... (7 Replies)
Hello.
I have a status command in AIX box, which provides output as below:
$ status
You are running the application on pegasus2
----Program Name------|--Avail / Total---------|
MQ | 1/2 |
ORACLE | 10/10 |
TMADMIN ... (3 Replies)
Hi,
PHP user here. I'm using an endless loop to perform to mimic a cron. The script does something every 20 minutes. It sleep()s in the meantime.
I have various checks that ensure that only instance can run, including a "gentleman agreement" locked file.
However, I'd like to make sure... (2 Replies)
Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Hi,
I am writing a shell script to invoke a C++ program. Before I start the C++ program (oi7loadbalancer), I am checking if the process is already running. I start the process only if it is not already running. I have the following check in my script.
proccount=`ps -f -u $USER_NAME | grep... (8 Replies)