在 Debian 上用 nohup 时“报错”通常分几种情况:命令本身报错、权限问题、环境变量问题、输出重定向问题、或进程启动后立刻退出。下面按“如何定位”的思路给你一套实用方法。
nohup your_command > out.log 2>&1 &
如果报错,复制完整信息,常见有:
nohup: failed to run command 'xxx': No such file or directorynohup: ignoring input and appending output to 'nohup.out'(这不是错误)Permission deniedwhich nohup
正常应是:
/usr/bin/nohup
如果没有:
apt update
apt install coreutils
nohup: failed to run command 'xxx': No such file or directory
which xxx
ls -l /path/to/xxx
如果是脚本:
head -1 your_script.sh
确认 shebang 正确,例如:
#!/bin/bash
✅ 解决:用绝对路径
nohup /usr/local/bin/xxx > out.log 2>&1 &
Permission denied
ls -l your_command
如果不是可执行:
chmod +x your_command
如果是脚本,还要确认解释器存在:
ls -l /bin/bash
nohup: failed to open 'nohup.out': Permission denied
/root 下用普通用户nohup your_command > /tmp/out.log 2>&1 &
检查磁盘:
df -h
ps aux | grep your_command
找不到进程
nohup your_command > debug.log 2>&1 &
cat debug.log
常见原因:
nohup bash -lc 'your_command' > out.log 2>&1 &
-l 会加载 login shell 环境
如果你在 Debian 9+,长期运行程序不建议 nohup,推荐:
sudo systemctl edit --force --full myapp.service
示例:
[Unit]
Description=My App
[Service]
ExecStart=/usr/local/bin/myapp
WorkingDirectory=/opt/myapp
User=myuser
Restart=always
[Install]
WantedBy=multi-user.target
然后:
sudo systemctl enable --now myapp
journalctl -u myapp -f
✅ 好处:
# 1. 看命令是否存在
which your_command
# 2. 手动运行(不用 nohup)
your_command
# 3. 用绝对路径 + 日志
nohup /abs/path/your_command > /tmp/log 2>&1 &
# 4. 看日志
cat /tmp/log
# 5. 看进程
ps aux | grep your_command
如果你愿意,可以把具体的 nohup 报错信息原样贴出来,我可以帮你精准判断是哪一类问题。