3

I'm packaging some daemon for debian 8 and systemd.

The daemon can create PID-file by itself, but it has no permissions to write into /run because of non-root user. It used to create PID-file via old sysV init-script, but it doesn't work on systemd.

I can use workaround in service-file like this:

Environment="PIDDIR=/var/run/mydaemon"
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p $PIDDIR
ExecStartPre=/bin/chown -R mydaemon. $PIDDIR

But it doesn't looks right.

I can use /tmp as $PIDDIR, but it also seems wrong.

Actually the only reason I need a PID-file is logrotate's postrotate sending SIGUSR1 to the daemon:

[ -s /run/mydaemon.pid ] && kill -USR1 `cat /run/mydaemon.pid`

It's also possible to search daemon's pid with pgrep, but it seems to be unreliable.

copytruncate in logrotate seems to be not the best option because of risc of loosing some part of log.

So, what is the right way to manage PID-files via systemd?

Аnd is there a way to sends random signals to daemons via systemd?

Paul K.
  • 125
  • 1
  • 1
  • 9

2 Answers2

2

Systemd has dedicated mechanism to create temporary directories and files: systemd-tmpfiles and tmpdfiles.d

In short have your package drop a file /usr/lib/tmpfiles.d/mydaemon.conf :

 #Type Path            Mode UID      GID    Age Argument
 d     /run/mydaemon   0755 mydaemon daemon -   -
HBruijn
  • 77,029
  • 24
  • 135
  • 201
  • There is on problem with this solution, however: The files are created very earöly during boot when there is no network available. So if one of your users or groups comes from some NSS service (like LDAP), creation of temporary directories fails. – U. Windl Jul 11 '19 at 11:49
  • @U.Windl Absolutely true, but convention is that service accounts for daemons are added to local /etc/passwd and group files so that mitigates the problem mostly. When your users are in central user directory which isn't available yet, I *assume* that numerical UID/GID's are also permitted... – HBruijn Jul 11 '19 at 13:33
0

in systemd .service file use

ExecReload=/bin/kill -USR1 $MAINPID

and in logrotate script use

systemctl reload SERVICENAME
chicks
  • 3,793
  • 10
  • 27
  • 36
jsaak
  • 221
  • 1
  • 3
  • Actually `reload` may be used for log rotation, but initially `reload` just mean *reload the configuration*. The latter may be unrelated to reopening the log file. – U. Windl Jul 11 '19 at 11:51