在 Debian(以及大多数 Linux 发行版)中,“启动进程”有多种方式,取决于你是手动启动、开机自启,还是以服务方式运行。下面按常见场景说明。
./my_program
或
/usr/bin/python3 app.py
这种方式是前台进程,关闭终端会终止进程。
./my_program &
查看后台任务:
jobs
nohup ./my_program > output.log 2>&1 &
或更现代的方式:
screen -S myproc
./my_program
# Ctrl+A D 离开
或:
tmux
./my_program
# Ctrl+B D
sudo nano /etc/systemd/system/myprogram.service
示例:
[Unit]
Description=My Program
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/my_program
User=myuser
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start myprogram
sudo systemctl stop myprogram
sudo systemctl restart myprogram
sudo systemctl enable myprogram
查看状态:
systemctl status myprogram
如果是老 Debian(< 8):
sudo /etc/init.d/myservice start
crontabcrontab -e
添加:
@reboot /path/to/program
/etc/rc.local(部分系统)sudo nano /etc/rc.local
添加:
/path/to/program &
ps aux | grep my_program
top
htop
结束进程:
kill PID
kill -9 PID
| 场景 | 推荐方式 |
|---|---|
| 临时测试 | 直接运行 |
| 长期运行 | systemd |
| 服务器服务 | systemd ✅ |
| 脚本任务 | crontab |
| 交互程序 | screen / tmux |
如果你愿意,可以告诉我:
cat /etc/debian_version)我可以给你完全定制的启动方案。