Wednesday, November 13, 2019

systemd, logrotate and PID-files




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?


Answer




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

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...