What’s a quick-and-dirty way to make sure that only one instance of a shell script is running at a given time?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s an implementation that uses a lockfile and echoes a PID into it. This serves as a protection if the process is killed before removing the pidfile:
The trick here is the
kill -0which doesn’t deliver any signal but just checks if a process with the given PID exists. Also the call totrapwill ensure that the lockfile is removed even when your process is killed (exceptkill -9).