简体   繁体   English

通过systemd运行Python脚本

[英]Running a Python script via systemd

I am pretty new to writing systemd service scripts. 我对编写systemd服务脚本很systemd I was following this useful page to write my own. 我一直在关注这个有用的页面来编写自己的页面 It is failing as below. 失败如下。

The prodKPI.service contents in /etc/systemd/system/ in a Centos 7 machine are as 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

The steps I followed after creating the file are 创建文件后我遵循的步骤是

systemctl daemon-reload
systemctl enable prodKPI.service

did a reboot and 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.

which did not help much, from 并没有太大帮助

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.

The error line below indicates some kind of permission issues? 下面的错误行指示某种权限问题? Should the service unit be run with a different user (or) access levels? 服务单元是否应该以不同的用户(或)访问级别运行? I changed the both the python files to be executable also 我将两个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

When Type is not oneshot you can only provide one command . 如果Type不是oneshot ,则只能提供一个命令

Also you are using bash syntax in your command. 另外,您在命令中使用bash语法。 You have to wrap it in an intermediate Shell Process if you wan't to use bash syntax. 如果您不想使用bash语法,则必须将其包装在中间的Shell进程中。 Ie: 即:

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