温馨提示×

怎样启动ubuntu进程

小樊
43
2026-10-21 08:59:08
栏目: 智能运维

在 Ubuntu 中“启动进程”有多种方式,取决于你想启动的是临时命令、后台服务、开机自启程序还是系统服务。下面按常见场景说明:


一、直接在终端启动进程(前台 / 后台)

1. 前台运行

./your_program

或

python3 app.py

关闭终端会结束进程。

2. 后台运行

./your_program &

或

nohup python3 app.py &
  • &:后台运行
  • nohup:终端关闭也不退出

3. 查看进程

ps aux | grep your_program

或

top
htop

4. 结束进程

kill PID
kill -9 PID

二、使用 systemd 启动系统服务(推荐)

1. 创建一个服务

sudo nano /etc/systemd/system/myservice.service

示例:

[Unit]
Description=My Service
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/user/app.py
WorkingDirectory=/home/user
Restart=always
User=user

[Install]
WantedBy=multi-user.target

2. 启动服务

sudo systemctl start myservice

3. 设置开机自启

sudo systemctl enable myservice

4. 查看状态

systemctl status myservice

5. 停止 / 重启

sudo systemctl stop myservice
sudo systemctl restart myservice

三、开机自动启动普通程序(非服务)

方法 1:使用 crontab

crontab -e

添加:

@reboot /home/user/start.sh

方法 2:Ubuntu 桌面环境

  • 打开 “启动应用程序”
  • 添加命令或脚本

四、常见场景示例

启动 Web 服务(如 Flask)

nohup python3 app.py &

启动 Docker 容器

docker run -d image_name

启动 Nginx

sudo systemctl start nginx

如果你能说明:

  • 是 命令行程序 / 脚本 / 服务
  • 是否要 开机自启
  • Ubuntu 版本

我可以给你更精确的启动方式。

0 踩