Linux 我的 pidfile 必须位于 /var/run 中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5173636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:27:15  来源:igfitidea点击:

Must my pidfile be located in /var/run?

linuxubuntuunixpid

提问by gmoore

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

我在两种情况下都在问:技术上和风格上。

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

我的应用程序/守护进程可以保留 pidfile/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).

我的需要是:我的守护进程在特定用户下运行,实现者必须在/var/run、chown 和 chgrp 中mkdir 一个新目录,以使我的守护进程运行。将 pidfile 保持在本地(对于守护程序)似乎更容易。

采纳答案by Gilles 'SO- stop being evil'

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…

我不会将 pidfile 放在应用程序安装目录下,例如/opt/my_app/whatever. 该目录可以只读挂载,可以在机器之间共享,可以被守护进程监视,将那里的任何更改视为可能的入侵尝试……

The normal location for pidfiles is /var/run. Most unices will clean this directory on boot; under Ubuntu this is achieved by /var/runan in-memory filesystem (tmpfs).

pidfiles 的正常位置是/var/run. 大多数 unice 会在启动时清理这个目录;在 Ubuntu 下,这是通过/var/run内存文件系统 (tmpfs) 实现的。

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

如果你从一个以 root 身份运行的脚本启动你的守护进程,让它创建一个子目录,/var/run/gmooredaemon并在suing 给用户并启动守护进程之前将它 chown 给运行守护进程的用户。

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

在许多现代的Linux系统中,如果您从一个脚本或发射器未作为root运行的后台程序,你可以把pidfile进程文件/run/user/$UID,这是每个用户相当于传统的/var/run。请注意,启动器的 root 部分,或以 root 身份运行的引导脚本,需要创建目录(对于人类用户,该目录是在用户登录时创建的)。

Otherwise, pick a location under /tmpor /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.

否则,请在/tmp或下选择一个位置/var/tmp,但这会引入额外的复杂性,因为如果 pidfile 的名称位于全局可写目录中,则无法唯一确定它。

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.

在任何情况下,让分发者或管理员更容易(命令行选项,可能加上编译时选项)更改 pidfile 位置。

回答by Costi Ciudatu

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.

pid 文件的位置应该是可配置的。/var/run 是pid 文件的标准,与/var/log 是日志的标准相同。但是您的守护进程应该允许您在某些配置文件中覆盖此设置。

回答by Paulo Scardine

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

/opt 用于安装“自包含”应用程序,所以这里没有错。使用/opt/my_app/etc/的配置文件,/opt/my_app/log/用于记录等-共同实践这种应用。

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

这样你就可以将你的应用程序作为一个 TGZ 文件分发,而不是为每个包管理器维护一个包(至少是 DEB,因为你标记了ubuntu)。我建议将其用于内部应用程序或您可以很好地控制环境的情况。理由是,如果保险箱的成本高于您放入的内容(打包应用程序所需的工作不应该超过编写应用程序所需的工作量),那是没有意义的。

回答by pestrella

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.

另一个约定,如果您不是以 root 身份运行脚本,则将 pidfile 放在~/.my_app/my_app.pid. 这种方式更简单,同时仍然安全,因为主目录不是世界可写的。