Python應(yīng)用安裝成systemd守護(hù)進(jìn)程

問題:怎么將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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • muahao閱讀 2,097評(píng)論 0 3
  • 本書介紹 你是不是對(duì)Django的學(xué)習(xí)感到迷茫?是不是對(duì)網(wǎng)上零星的教程感到絕望?是不是苦于沒有可以迅速上手的實(shí)例而...
    阡陌3536閱讀 1,334評(píng)論 0 0
  • systemd攻略 轉(zhuǎn)自http://www.lxweimin.com/p/d5305104d03a或者這個(gè)http...
    x1596357閱讀 2,937評(píng)論 0 3
  • title: python高級(jí)進(jìn)階 參考文獻(xiàn) https://pythonguidecn.readthedocs....
    采香行處蹙連錢閱讀 2,551評(píng)論 1 5
  • Python 二三事 面向初學(xué)者介紹Python相關(guān)的一些工具,以及可能遇到的常見問題。 最后更新 2013.5....
    hzyido閱讀 67,817評(píng)論 2 42