abrahab
abrahab

Reputation: 2496

Check that another instance of program already running

How to check that another instance of program already running under UNIX system? And exit() if so.

I'm already use file with pid on it when the process start. Then I check pid that its exists. BUT time to time program run on unexpected behavior and I have tons of copies of the program running.

Upvotes: 1

Views: 2307

Answers (2)

Patrick Mackinlay
Patrick Mackinlay

Reputation: 166

If you are writing pids to a file then you should serialise access to the file with flock.

Upvotes: 1

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

Use an appropriate synchronization mechanism (e.g. a process shared semaphore), to prevent starting your program twice.

Upvotes: 1

Related Questions