Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I create a PID/lock file when writing a portable application

I am modifying an application I have written to prevent multiple instances from running at the same time.

My first thought, was (according to the Linux FHS) to simply store a PID file in /var/run. In this way, I can check if the PID file exists, and exit with an error if it does.

Now, if I want my app to be portable, I have to consider that /var/run does not exist on every system (f.ex. Windows).

What is the best practice in this case?

Note: It does not have to be a PID file. A 0-byte "lock" file would do the trick as well. The PID file would have the advantage of also providing the PID to whomever would need to investigate problems.

Clarification: The application is a backup script which is normally run by a well defined user.

like image 607
exhuma Avatar asked Dec 06 '25 04:12

exhuma


1 Answers

One of the way is to make your application rely on a porting framework (like Qt). Such framework provides abstraction of the platform, allowing you to put always at "correct place" (e.g. for windows, it changes between XP, Vista/7). You might for example have a look at QSettings If you can't rely on a framework, then digging into their code might provide advanced answer.

like image 172
Bruce Avatar answered Dec 07 '25 19:12

Bruce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!