Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred location for PID file of system daemon run as non-root user

My question is related to this question, but the processes in question are run from cron, and by non-root users. As such, many of the users don't really have home dirs (or their home dirs point to /usr/share/package_name which is not an ideal location for a PID file).

Storing in /var/run is problematic, because this directory is not writable except by root.

I could use /tmp, but I wonder if this is ideal for security reasons.

I could arrange for a startup script to create a directory in /var/run which is owned by the appropriate user (I can't do this at package install time, as /var is often mounted as tmpfs, so is not persistent).

What's the best practice here?

like image 479
Flimzy Avatar asked Jun 03 '13 21:06

Flimzy


People also ask

What is pid daemon?

A Pid-File is a file containing the process identification number (pid) that is stored in a well-defined location of the filesystem thus allowing other programs to find out the pid of a running script. Daemons needs the pid of the scripts that are currently running in the background to send them so called signals.

What are pid files?

A PID file is a file which contains the PID of the executable which generated it. When an application terminates, that file is removed. If it is removed while the application is running, the application terminates. If the application restarts, a new PID is written to the file.

Where should pid files be stored?

The normal location for pidfiles is /var/run . Most unices will clean this directory on boot; under Ubuntu this is achieved by /var/run an in-memory filesystem (tmpfs).


1 Answers

Nice question :), I'm having exactly the same at moment. I'm not sure if this is the correct answer but I hope it helps and I would appreciate feedback as well.

I've googled around and found that registering the per user daemon as a dbus service is an elegant solution. dbus could make sure that the service runs just once. no need for a pidfile.

Another solution (my current) would be to create the PID file in a directory like:

$HOME/.yourdaemon/pid

After your comment I realized, that you cannot write to home. I would suggest to look into dbus

Update

I have an idea. What if you are using /tmp, but looking for a pidfile which is called yourdaemon.pid.UNIQUE_KEY and is owned by the daemon's user? This should work fine.

UNIQUE_KEY should be random generated (preferred is using tempnam as it is race condition proof).

like image 191
hek2mgl Avatar answered Oct 21 '22 07:10

hek2mgl