問題:怎么將Python腳本以systemd的方式運(yùn)行?怎么把Python應(yīng)用設(shè)置為自動(dòng)啟動(dòng)?
Step 1 編寫Python應(yīng)用(最好以virtualenv的方式,將python和模塊安裝到Python應(yīng)用目錄中),不管以模式或者其他方式運(yùn)行,只要記下可以正常運(yùn)行的命令行以及相應(yīng)的目錄。
/data/xncd/python/xnbatchpersistence/venv/bin/python -m app.schedule.scheduler
Step 2 生成systemd服務(wù)文件
vi /lib/systemd/system/xnbatch.service
文件內(nèi)容如下:
[Unit]
Description=xnBatch Service
After=multi-user.target
Conflicts=getty@tty1.service
[Service]
Type=simple
WorkingDirectory=/data/xncd/python/xnbatchpersistence
ExecStart=/data/xncd/python/xnbatchpersistence/venv/bin/python -m app.schedule.scheduler
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
Step 3 啟用服務(wù)
systemctl daemon-reload
systemctl enable xnbatch
systemctl start xnbatch
Step 4 檢測(cè)服務(wù)是否正常運(yùn)行
systemctl status xnbatch
如果顯示為activate,則表示安裝成功:
systemctl status xnbatch
● xnbatch.service - xnBatch Service
Loaded: loaded (/usr/lib/systemd/system/xnbatch.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2019-10-08 21:55:57 CST; 18min ago
Main PID: 13755 (python)
CGroup: /system.slice/xnbatch.service
└─13755 /data/xncd/python/xnbatchpersistence/venv/bin/python -m app.schedule.scheduler
10月 08 21:55:57 izuf66xttbby2as7xqznx2z systemd[1]: Started xnBatch Service.
Step 4 服務(wù)控制
停止服務(wù)
systemctl stop xbatch
啟動(dòng)服務(wù)
systemctl start xnbatch
重啟服務(wù)
systemctl restart xnbatch