79

I'm asking in both contexts: technically and stylistically.

Can my application/daemon keep a pidfile in /opt/my_app/run/?

Is it very bad to do so?

My need is this: my daemon runs under a specific user, and the implementor must mkdir a new directory in /var/run, chown, and chgrp it to make my daemon run. Seems easier to just keep the pidfile local (to the daemon).

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
gmoore
  • 5,506
  • 5
  • 29
  • 36

4 Answers4

115

I wouldn't put a pidfile under an application installation directory such as /opt/my_app/whatever. This directory could be mounted read-only, could be shared between machines, could be watched by a daemon that treats any change there as a possible break-in attempt…

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).

If you start your daemon from a script that's running as root, have it create a subdirectory /var/run/gmooredaemon and chown it to the daemon-running user before suing to the user and starting the daemon.

On many modern Linux systems, if you start the daemon from a script or launcher that isn't running as root, you can put the pidfile in /run/user/$UID, which is a per-user equivalent of the traditional /var/run. Note that the root part of the launcher, or a boot script running as root, needs to create the directory (for a human user, the directory is created when the user logs in).

Otherwise, pick a location under /tmp or /var/tmp, but this introduces additional complexity because the pidfile's name can't be uniquely determined if it's in a world-writable directory.

In any case, make it easy (command-line option, plus perhaps a compile-time option) for the distributor or administrator to change the pidfile location.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • /run is now writable only by owner (root). How would apps write a pidfile there? – TSG Jan 18 '18 at 03:03
  • @TSG Modern distributions usually create a per-user directory in `/run/user`. I don't recall that `/run` itself was ever writable by everybody. But pidfiles in `/run` are common, nonetheless, they're the norm for system services. The pidfile is written by a supervisor that runs as root, or by a launch script that's running as root before it launches the actual daemon with possibly reduced privileges. – Gilles 'SO- stop being evil' Jan 18 '18 at 08:19
  • 1
    This answer is only partially correct: `/run/user/$UID` only exists during sessions (more or less when a user is logged in). Use /tmp or ~ instead, see: https://superuser.com/a/1127720/71795 – Tim May 18 '18 at 20:55
  • 1
    for systemd users, see https://serverfault.com/questions/779634/create-a-directory-under-var-run-at-boot – rogerdpack Jul 18 '19 at 21:34
9

The location of the pid file should be configurable. /var/run is standard for pid files, the same as /var/log is standard for logs. But your daemon should allow you to overwrite this setting in some config file.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
9

/opt is used to install 'self-contained' applications, so nothing wrong here. Using /opt/my_app/etc/ for config files, /opt/my_app/log/ for logs and so on - common practice for this kind of application.

This away you can distribute your applications as a TGZ file instead of maintaining a package for every package manager (at least DEB since you tagged ubuntu). I would recommend this for in-house applications or situations where you have great control over the environment. The reasoning is that it makes no sense if the safe costs more than what you are putting inside (the work required to pack the application should not eclipse the effort required to write the application).

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
7

Another convention, if you're not running the script as root, is to put the pidfile in ~/.my_app/my_app.pid. It's simpler this way while still being secure as the home directory is not world-writeable.

pestrella
  • 9,786
  • 4
  • 39
  • 44
  • 2
    This would be good if each user can run an instance of your app without conflict. Otherwise I'd stick to /var/run – dlite922 Dec 22 '16 at 16:20