systemd自启任务

若要让 source ~/miio_venv/bin/activate && cd ~/miio_venv/temp/ && python main.py 这个命令在开机时于后台自动启动,可以采用以下几种方式。下面为你详细介绍常见的方法:

方法:

使用 systemd(适用于大多数基于systemd的Linux发行版,如Ubuntu 16.04及以上、CentOS 7及以上)
#### 步骤:
1. 创建服务文件
你需要创建一个 systemd 服务文件,一般将其命名为 main.service,并放置于 /etc/systemd/system/ 目录下。 doubaocanvas: 注意:你要把 your_username 替换成你自己的用户名。

[Unit]
Description=Run main.py script in virtual environment
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/root/miio_venv/temp    
Environment="PYTHONPATH=/root/miio_venv/lib/python3.11/site-packages"
ExecStart=/root/miio_venv/bin/python /root/miio_venv/temp/main.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
  1. 重新加载 systemd 管理器配置: 运行以下命令,使 systemd 重新加载配置文件:
sudo systemctl daemon-reload 
  1. 启动并设置服务开机自启: 运行以下命令启动服务,并设置其在开机时自动启动:
sudo systemctl start main.service 
sudo systemctl enable main.service 
  1. 验证服务状态: 你可以运行以下命令来检查服务的运行状态:
sudo systemctl status main.service

评论

  1. 2 月前
    2026-4-09 14:27:33

    25phwin, eh? Feels a bit…new. I definitely recommend doing some extra digging before jumping in. Better safe than sorry! Check it out here: 25phwin

  2. 2 月前
    2026-4-09 14:27:50

    MCWCasino777? Yeah, I’ve had a few goes there. Plenty to keep you entertained, fair games, and pretty smooth site experience overall. Check it out: mcwcasino777.

  3. 2 月前
    2026-4-09 14:28:07

    Looking for a game download and found sikkimgameapk.com. Downloaded a game without any issue. So check out sikkim game download to download games.

发送评论 编辑评论


				
上一篇
下一篇