大网数据广告活动
服务器运维问答

systemd, logrotate and PID-files

· 52CE 我爱测

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

问题原文

原帖链接 · Paul K. · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · HBruijn · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-03-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。