在 Linux 中,nohup 本身不负责设置日志权限,日志文件的权限主要由创建用户、umask 以及目录权限决定。下面分几种常见情况说明,并给出推荐做法。
nohup command &
默认会生成:
nohup.out
权限通常是:
-rw-r--r-- 1 user user ...
原因:
666 & ~umaskumask
( umask 022; nohup command > nohup.out 2>&1 & )
常见 umask 对照表:
| umask | 文件权限 |
|---|---|
| 022 | 644 |
| 027 | 640 |
| 077 | 600 |
✅ 推荐生产环境
( umask 027; nohup command > app.log 2>&1 & )
nohup command > app.log 2>&1 &
chmod 640 app.log
或一次性:
nohup command > app.log 2>&1 &
chmod 640 app.log
chown appuser:appgroup app.log
sudo -u appuser bash -c 'nohup command > app.log 2>&1 &'
如果日志目录不可写,nohup 会失败或写入失败:
chmod 750 /data/logs
chown appuser:appgroup /data/logs
[Service]
User=appuser
Group=appgroup
StandardOutput=append:/data/logs/app.log
StandardError=append:/data/logs/app.log
systemd 会自动管理权限。
| 场景 | 建议 |
|---|---|
| 普通应用 | 640 |
| 多用户共享 | 640 + 组 |
| 敏感日志 | 600 |
| 生产环境 | 不要用 nohup.out |
| 长期运行 | 用 systemd / supervisor |
nohup 日志权限 = umask + 目录权限 + 用户身份
最佳实践是:自定义日志文件 + umask + chown/chmod
如果你愿意,可以把你的启动命令、运行用户、目录结构发出来,我可以帮你给出最合适的一套权限方案。