Writing a PID File on Linux

I am currently working on a linux daemon which should be a single instance (i.e. limited to one process 1 user 1). What would be the best way to do this without using getpid()to manually write the pid to / var / run / and then block it with flock()?

+5
source share
5 answers

Just lock the executable file.

+1
source

Enable startup and shutdown using start-stop-daemon .

+3
source
+1

- initd, . COMMAND ,

PIDFILE=/var/run/service.pid
COMMAND="java -jar start.jar"
$COMMAND > /dev/null 2>&1 &
echo $! > $PIDFILE

@dogane, .

+1

If you really don't have a lock file, use a socket instead. Another instance will not be able to start because the address will already be used.

0
source

All Articles