簡體   English   中英

通過systemd運行Python腳本

[英]Running a Python script via systemd

我對編寫systemd服務腳本很systemd 我一直在關注這個有用的頁面來編寫自己的頁面 失敗如下。

Centos 7機器中/etc/systemd/system/中的prodKPI.service內容如下

[Unit]
Description=prodKPI: Aggregation logic to compute device availability

[Service]
Type=simple
PermissionsStartOnly=true
User=deployuser
ExecStart = /usr/bin/python /tmp/app1/folder1/file1.py; /usr/bin/python /tmp/folder2/file2.py
Restart=always

[Install]
WantedBy=default.target

創建文件后我遵循的步驟是

systemctl daemon-reload
systemctl enable prodKPI.service

reboot

# systemctl status prodKPI.service
● prodKPI.service - prodKPI: Aggregation logic to compute device availability
   Loaded: loaded (/etc/systemd/system/prodKPI.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Fri 2017-07-21 19:20:41 UTC; 15min ago
  Process: 1190 ExecStart=/usr/bin/python /tmp/app1/folder1/file1.py; /usr/bin/python /tmp/folder2/file2.py (code=exited, status=2)
 Main PID: 1190 (code=exited, status=2)

Jul 21 19:20:41 device-1 systemd[1]: prodKPI.service: main process exited, code=exited, status=2/INVALIDARGUMENT
Jul 21 19:20:41 device-1 systemd[1]: Unit prodKPI.service entered failed state.
Jul 21 19:20:41 device-1 systemd[1]: prodKPI.service failed.
Jul 21 19:20:41 device-1 systemd[1]: prodKPI.service holdoff time over, scheduling restart.
Jul 21 19:20:41 device-1 systemd[1]: start request repeated too quickly for prodKPI.service
Jul 21 19:20:41 device-1 systemd[1]: Failed to start prodKPI.service: Aggregation logic to compute device availability.
Jul 21 19:20:41 device-1 systemd[1]: Unit prodKPI.service entered failed state.
Jul 21 19:20:41 device-1 systemd[1]: prodKPI.service failed.

並沒有太大幫助

journalctl -xe

-- Unit prodKPI.service has finished starting up.
-- 
-- The start-up result is done.
Jul 21 19:20:41 device-1 systemd[1]: Starting prodKPI.service: Aggregation logic to compute device availability...
-- Subject: Unit prodKPI.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit prodKPI.service has begun starting up.
Jul 21 19:20:41 device-1 python[1190]: /usr/bin/python: can't open file '/tmp/app1/folder1/file1.py;': [Errno 2] No such file or directory
Jul 21 19:20:41 device-1 systemd[1]: prodKPI.service: main process exited, code=exited, status=2/INVALIDARGUMENT
Jul 21 19:20:41 device-1 systemd[1]: Unit prodKPI.service entered failed state.
Jul 21 19:20:41 device-1 systemd[1]: prodKPI.service failed.
Jul 21 19:20:41 device-1 systemd[1]: prodKPI.service holdoff time over, scheduling restart.
Jul 21 19:20:41 device-1: start request repeated too quickly for prodKPI.service
Jul 21 19:20:41 device-1: Failed to start prodKPI.service: Aggregation logic to compute device availability.

下面的錯誤行指示某種權限問題? 服務單元是否應該以不同的用戶(或)訪問級別運行? 我將兩個python文件都更改為可執行文件

Jul 21 19:20:41 device-1 python[1190]: /usr/bin/python: can't open file '/tmp/app1/folder1/file1.py;': [Errno 2] No such file or directory

如果Type不是oneshot ,則只能提供一個命令

另外,您在命令中使用bash語法。 如果您不想使用bash語法,則必須將其包裝在中間的Shell進程中。 即:

ExecStart=/usr/bin/bash -c "python /tmp/app1/folder1/file1.py; /usr/bin/python /tmp/folder2/file2.py"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM