I have been working on using "flock"/file lock to prevent two instances of a bash script from being executed. Below is a simplified version of what I have to illustrate the flock part. It works as it is set up there below however the piece I am trying to figure out is how to get it to output to /var/log/messages ONLY when a 2nd instance of the script is attempted to run and subsequently errors out. If I put some "logger" command in the script then it will just run every single time regardless of whether or not I wanted it to. I believe I need something after the || but just what I am not sure. I tried putting ( ) around the piece after the || but that seems to mess with the script itself and actually allowed 2 or more instances to runat the same time so I don't think I did that right.
any advice would be greatly appreciated
Last edited by Scrutinizer; 02-15-2016 at 04:52 PM..
Reason: CODE tags
This User Gave Thanks to infrared013 For This Post:
I think you did it correctly, and it should have worked.
Using { } instead of ( ) can eventually save a sub-shell. Note that the closing } must be on a new line or preceded by a semicolon.
This User Gave Thanks to MadeInGermany For This Post:
You could also try this full manual approach (as i've overseen the 'flock' command, but want to share this anyway):
Hope this helps
EDIT:
When i'm calling RudiC's solution, very close to the origin, i'm having a 5 sec delay if the script is already running.
Likewise in the main execution, it takes 1 min 5 sec - quite a long time to just check wether there is a lock or not, dont you think?
I assume that is caused by will (on purpose) as flock -w 5 is used, personally, i think it is too long for testing.
Hi i have a script that check pings and i use flock to so the script wont run
multipul times :
its not the whole script but this is the idea :
(
flock -x -w 3 200 || exit 1
/usr/sbin/fping -c$count -i$interval -a $hosts > $FILE1 2>&1
) 200>/var/lock/.myscript.exclusivelock
now i... (4 Replies)
I'm changing my mindset from a few big processes moving data from a few sources under an external, dependency-based scheduler to multiple processes moving data from many sources run by each client cron and possibly interfering with each other. It has the benefits of more granular code but I'm... (11 Replies)
Hi,
I have a process which can run one instance at a time. Currently we have multiple scripts trying to kickoff this process. I wanted to implement the semaphore mechanism to achieve this.
I was going through few examples. The below code seems to be reasonable solution.
... (5 Replies)
Hi all,
I have to test some user priviliges. The goal is to be sure that an unauthorized user can't restart some modules (ssh, mysql etc...).
I'm trying to automate it with a shell script but in same cases I got the syslog broadcast message.
Is there any way to simply get a return code... (3 Replies)
Hello all,
If anyone has time, I have a few questions:
How do I do the following in Linux. We are using Red Hat and Oracle Enterprise Linux, which is based on Red Hat too.
1. How to lock the account after a few (like 3) invalid password attempts?
2. How do you lock a screen after 30... (1 Reply)
Hi,
We have a lock file being created called lck8c0001 created in Unixware 2.1.2. This is locking a printer.
According to some websites, 8c0001 relates to the device name.
How does one link 8c0001 to those devices listed in the /dev folder?
I have done a ps -lp for all printers and have... (4 Replies)
how can I lock my keyboard while I'm away from the computer without using lock command. What other commands gives me the option to lock keyboard device?
thanks (7 Replies)