Making sure only one instance of a script is running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Making sure only one instance of a script is running
# 8  
Old 01-17-2017
Actually, how about this as a simple mutex method?

Code:
MASK=$(umask)
umask 773

# PID file will be atomically created as read-only
# The first shell can write to it, but no others
if ! echo "$$" > /tmp/pidfile
then
        echo "Failed to get PID file"
        read PID < /tmp/pidfile
        echo "PID $PID has it"
        exit 1
fi

umask "$MASK"


trap "rm -f /tmp/$$" EXIT

# everything else

Still one potential race -- the file may exist but still be empty when other shells try and read it. That's not a fatal flaw though.

Another bigger problem is that root can plow straight through ignoring all permissions to write to that read-only file. Only run as a user.

Last edited by Corona688; 01-17-2017 at 06:46 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To trigger script on particular instance

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)
Discussion started by: Master_Mind
3 Replies

2. Shell Programming and Scripting

Regarding a query on making changes to a running script

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)
Discussion started by: RavinderSingh13
5 Replies

3. UNIX for Advanced & Expert Users

How to check a single process instance is always running?

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)
Discussion started by: sanzee007
2 Replies

4. Shell Programming and Scripting

Process Instance not running properly.

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)
Discussion started by: ankitknit
3 Replies

5. Shell Programming and Scripting

Making use of multiple cores for running sed and awk scripts

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)
Discussion started by: shoaibjameel123
7 Replies

6. Shell Programming and Scripting

Print available running instance out of total

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)
Discussion started by: panchpan
3 Replies

7. Shell Programming and Scripting

How to restrict running one instance of scp at any time in fsniper

How to restrict running one instance of scp at any time? (2 Replies)
Discussion started by: proactiveaditya
2 Replies

8. Shell Programming and Scripting

[PHP] endless loop mimics a cron. Make sure only one instance is running

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)
Discussion started by: jjshell
2 Replies

9. Shell Programming and Scripting

replace first instance(not first instance in line)

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)
Discussion started by: IronHorse7
3 Replies

10. Shell Programming and Scripting

How to check if another instance of the process is running

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)
Discussion started by: sim
8 Replies
Login or Register to Ask a Question