简体   繁体   English

以非root用户身份运行系统守护程序PID文件的首选位置

[英]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. 我的问题与此问题有关 ,但是有问题的进程是从cron运行的,并且由非root用户运行。 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). 因此,许多用户实际上并没有家庭目录(或他们的家庭目录指向/usr/share/package_name ,这不是PID文件的理想位置)。

Storing in /var/run is problematic, because this directory is not writable except by root. /var/run存储是有问题的,因为该目录只能由root用户写入。

I could use /tmp , but I wonder if this is ideal for security reasons. 我可以使用/tmp ,但是我出于安全原因不知道这是否理想。

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). 我可以安排一个启动脚本在/var/run创建一个目录,该目录由适当的用户拥有(我无法在软件包安装时执行此操作,因为/var通常作为tmpfs挂载,因此不是持久性的)。

What's the best practice here? 这里的最佳做法是什么?

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服务是一个很好的解决方案。 dbus could make sure that the service runs just once. dbus可以确保该服务仅运行一次。 no need for a pidfile. 不需要pidfile。

Another solution (my current) would be to create the PID file in a directory like: 另一个解决方案(目前是我的解决方案)是在以下目录中创建PID文件:

$HOME/.yourdaemon/pid

After your comment I realized, that you cannot write to home. 在您发表评论后,我意识到您无法写信回家。 I would suggest to look into dbus 我建议研究一下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? 如果您正在使用/ tmp,但是正在寻找一个守护进程用户拥有的名为yourdaemon.pid.UNIQUE_KEY的pidfile,该怎么办? This should work fine. 这应该工作正常。

UNIQUE_KEY should be random generated (preferred is using tempnam as it is race condition proof). UNIQUE_KEY应该是随机生成的(最好使用tempnam,因为它是竞争条件证明)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM