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

吉尔斯

以下名为“ tst_script.py”的Python脚本将写入文件。它通过命令行启动时有效,但通过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

名为“ 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

服务脚本复制到“ / lib / systemd / system”文件夹中,并通过以下方式激活:

sudo systemctl daemon-reload
sudo systemctl enable tst_script.service

我们通过以下方式检查该服务是否已安装并启用: sudo systemctl status tst_script.service

结果是:

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)

我们通过以下方式启动该服务: sudo systemctl start tst_script.service

我们检查脚本是否正在运行: sudo systemctl status tst_script.service

结果是:

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

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)

但是结果是文件“ tst_file”不存在...

我用谷歌搜索,但没有找到解决我问题的答案。我发现最接近的答案是将python脚本作为systemd服务运行

你有解决这个问题的主意吗?

丢失

正如丛马(Cong Ma)在评论中提到的那样,最明显的问题是您看错了地方。在代码中,您的输出文件为:with open('test_file', 'a')测试该脚本时,该文件将显示在与该脚本相同的文件夹中。

问题是,Python无法解释file_name/path/to/python/script/file_name,而是读取相对于当前工作目录的相对路径。如果从systemd开始,则当前的工作目录与脚本所在的目录不同。

您可以通过配置服务来解决此问题,但是在这种情况下,更简单的方法是仅提供输出文件的绝对路径:

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

如果您想闲逛该服务,则可以更改systemd的工作目录。此信息可能会有所帮助:更改systemd服务的工作目录

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python 脚本在作为 ubuntu 服务运行时出错

从cron运行时,Perl脚本不会将STDOUT输出到文件

从Spring Boot启动时,Python脚本不会写入数据库

使用 systemd 作为服务运行时,从保存的代码中下载的文件在哪里

在将JAR作为Systemd服务运行时出错

Python日志记录不会写入文件

Python脚本不会将汉字写入XML文件

手动运行时,Python脚本会生成所有图像文件,但作为cronjob运行时,则不会生成所有图像文件

当直接从终端运行时,Bash脚本不会为命令打印相同的输出

Nlog不会写入Windows服务中的日志文件

脚本不会将时间写入文件

Python:写入文件会写入先前运行的代码中的数据

尝试使用systemd将python脚本作为服务运行

作为systemd服务运行时,rsync命令不起作用

Linux上的C ++:作为systemd服务运行时侦听键盘输入

Python - 文件不会写入所有数据

写入文件的运行时影响

进程运行时写入日志文件

从桌面运行时,Python 3.2脚本不起作用和/或导入tkinter

使用&在后台运行时,Nohup for Python脚本不起作用

通过 PHP 运行时,工作 Python 脚本不起作用

从 BUTTON 或 SCRIPTEDITOR 运行时脚本不同

Systemd服务脚本无法写入文件

如何使脚本作为systemd服务运行?

Systemd 配置以将脚本作为服务运行

作为服务运行时,Uwsgi不会加载应用程序

如何使用systemd服务或tmpfiles.d自动创建运行时文件夹?

脚本不会运行

当脚本作为cronjob运行时,python脚本在import语句处停止