简体   繁体   English

作为systemd服务运行时,Python脚本不会写入文件

[英]Python script doesn't write to file when run as a systemd service

The following Python script named "tst_script.py" writes in a file. 以下名为“ tst_script.py”的Python脚本将写入文件。 It works when it is launch via command line but fails to create the file when lauch via a systemd service. 它通过命令行启动时有效,但通过systemd服务启动时无法创建文件。

#!/usr/bin/python
#-*- coding: utf-8 -*-

import time

if __name__ == '__main__':
    with open('test_file', 'a') as fd:
        while True:
            for i in range(10):
                fd.write('test' + str(i) + '\r\n')
                time.sleep(3)
            break

The service script named "tst_script.service" is the following: 名为“ tst_script.service”的服务脚本如下:

[Unit]
Description=Python test script
[Install]
WantedBy=multi-user.target
[Service]                                                                                             
Type=simple                                                                                           
ExecStart=/usr/bin/python -O /home/gilles/tst_script.py

The service script is copied in "/lib/systemd/system" folder and activated by: 服务脚本复制到“ / lib / systemd / system”文件夹中,并通过以下方式激活:

sudo systemctl daemon-reload
sudo systemctl enable tst_script.service

We check that the service is installed and enabled by: sudo systemctl status tst_script.service 我们通过以下方法检查该服务是否已安装和启用: sudo systemctl status tst_script.service

The result is: 结果是:

tst_script.service - Python test script
   Loaded: loaded (/lib/systemd/system/tst_script.service; enabled)
   Active: inactive (dead) since ven. 2018-03-02 13:35:00 CET; 16min ago
 Main PID: 10565 (code=exited, status=0/SUCCESS)

and we launch the service by: sudo systemctl start tst_script.service 然后通过以下方式启动该服务: sudo systemctl start tst_script.service

We check that the script is running: sudo systemctl status tst_script.service 我们检查脚本是否正在运行: sudo systemctl status tst_script.service

The result is: 结果是:

tst_script.service - Python test script
   Loaded: loaded (/lib/systemd/system/tst_script.service; enabled)
   Active: active (running) since ven. 2018-03-02 13:51:17 CET; 1s ago
 Main PID: 10738 (python)
   CGroup: /system.slice/tst_script.service
           └─10738 /usr/bin/python -O /home/gilles/tst_script.py

And after 30 seconds, we check that the process is completed: 30秒后,我们检查过程是否完成:

tst_script.service - Python test script
   Loaded: loaded (/lib/systemd/system/tst_script.service; enabled)
   Active: inactive (dead) since ven. 2018-03-02 13:51:47 CET; 3s ago
  Process: 10738 ExecStart=/usr/bin/python -O /home/gilles/tst_script.py               (code=exited, status=0/SUCCESS)
 Main PID: 10738 (code=exited, status=0/SUCCESS)

but, as a result, the file "tst_file" doesn't exist... 但是结果是文件“ tst_file”不存在...

I googled but don't find any answer who solve my problem. 我用谷歌搜索,但没有找到解决我问题的答案。 The closest answer I found is running python script as a systemd service . 我发现最接近的答案是将python脚本作为systemd服务运行

Have you any idea to solve this problem ? 你有解决这个问题的主意吗?

As Cong Ma mentioned in the comments, the most obvious problem is that you are probably looking in the wrong place. 正如丛马(Cong Ma)在评论中提到的那样,最明显的问题是您看错了地方。 In your code you have the output file as: with open('test_file', 'a') which when you are testing this script appears in the same folder as the script. 在您的代码中,您的输出文件为: with open('test_file', 'a') ,当您测试此脚本时,该文件显示在与脚本相同的文件夹中。

The problem is, that Python does not interpret file_name as /path/to/python/script/file_name but instead reads a relative path as relative to the present working directory. 问题在于,Python不会将file_name解释为/path/to/python/script/file_name ,而是读取相对于当前工作目录的相对路径。 If you are kicking off from systemd, your present working directory is not the same as where the script is located. 如果从systemd开始,则当前的工作目录与脚本所在的目录不同。

You can handle this by configuring your service, but easier in this case is to just provide an absolute path to the output file: 您可以通过配置服务来解决此问题,但是在这种情况下,更简单的方法是提供输出文件的绝对路径:

import time

if __name__ == '__main__':
    with open('/home/gilles/test_file', 'a') as fd:
        while True:
            for i in range(10):
                fd.write('test' + str(i) + '\r\n')
                time.sleep(3)
            break

If you'd rather fool around with the service, you can change the working directory of systemd. 如果您想闲逛该服务,则可以更改systemd的工作目录。 This info might be helpful: Changing Working Directory of systemd service 此信息可能会有所帮助: 更改systemd服务的工作目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当python脚本作为systemd服务运行时,Python Popen不识别scrapy - Python Popen doesn't recognize scrapy when python script is run as systemd service Systemd服务不会将python脚本写入文件,python脚本在从cli执行时名义上运行 - Systemd service does not write python script to file, python script runs nominally when executed from cli 当我尝试在批处理文件中使用python脚本写入文件然后运行它时。 它不会写入文件 - When I try to write to a file using python script in a batch file and then run it. It doesn't write to the file 将 python 脚本作为 systemd 服务运行 - running python script as a systemd service 作为 systemd/systemctl 服务运行时,python 导入本地模块失败 - python import of local module failing when run as systemd/systemctl service 从批处理文件启动时,Python脚本不会将希腊字符写入文本文件 - Python script doesn't write Greek characters to text file when started from batch file 从OS X终端运行时,Python脚本不会写入文本文件 - Python Script Won't Write to text file when run from OS X terminal Python脚本创建文件但不写入任何内容 - Python script makes a file but doesn't write anything 从 Dockerfile 执行的 Python 脚本不写入 HTML 文件 - Python script executed from Dockerfile doesn't write HTML file Python 脚本不将汉字写入 XML 文件 - Python script doesn't write Chinese characters to XML file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM