【问题标题】:Linux-Debian: How to solve “[Errno 13] – Permission denied“ when starting Python-script through rc.local?Linux-Debian:通过 rc.local 启动 Python-script 时如何解决“[Errno 13] – Permission denied”?
【发布时间】:2022-01-25 09:07:52
【问题描述】:

为了完成我学校的期末作业,我目前正在尝试在重启后在我的 Linux Debian 11 上自动运行以下 .py 文件 (createTXT.py):

import os
from os import listdir
from os.path import isfile, join

#dateiPfad = os.getcwd()
dateiPfad = "/test_csv/"

filenames = [f for f in listdir(dateiPfad) if isfile(join(dateiPfad, f))]

i = 0

for file in range(len(filenames)):

    i = i + 1

with open('text'+str(i)+'.txt', 'w') as f:
    f.write('Create a new text file!')

我想运行的代码在通过“python3 createTXT.py”执行时运行良好。

我创建了 /etc/rc.local,使其可执行等等。这就是它的样子:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

sleep 10 && sudo -H -u myuser /usr/bin/python3 /python_skript/createTXT.py

exit 0

当我尝试使用“Sudo systemctl start rc-local”运行它时,弹出以下有关权限被拒绝的错误:

$ sudo systemctl status rc-local
● rc-local.service - /etc/rc.local Compatibility
 Loaded: loaded (/lib/systemd/system/rc-    local.service; enabled-runtime; vendor preset: enabled)
Drop-In: /usr/lib/systemd/system/rc-local.service.d
         └─debian.conf
 Active: failed (Result: exit-code) since Tue 2022-    01-25 09:17:38 CET; 4s ago
   Docs: man:systemd-rc-local-generator(8)
Process: 9426 ExecStart=/etc/rc.local start (code=exited, status=1/FAILURE)
    CPU: 52ms

Jan 25 09:17:38 UP-debian sudo[9467]:     root : PWD=/ ; USER=myuser ; COMMAND=/usr/bin/python3 /python_skript/createTXT.py
Jan 25 09:17:38 UP-debian sudo[9467]: pam_unix(sudo:session): session opened for user myuser(uid=1000) by (uid=0)
Jan 25 09:17:38 UP-debian rc.local[9468]: Traceback (most recent call last):
Jan 25 09:17:38 UP-debian rc.local[9468]:   File "/python_skript/createTXT.py", line 19, in <module>
Jan 25 09:17:38 UP-debian rc.local[9468]:     with open('text'+str(i)+'.txt', 'w') as f:
Jan 25 09:17:38 UP-debian rc.local[9468]: PermissionError: [Errno 13] Permission denied: 'text16.txt'
Jan 25 09:17:38 UP-debian sudo[9467]: pam_unix(sudo:session): session closed for user myuser
Jan 25 09:17:38 UP-debian systemd[1]: rc-local.service: Control process exited, code=exited, status=1/FAILURE
Jan 25 09:17:38 UP-debian systemd[1]: rc-local.service: Failed with result 'exit-code'.
Jan 25 09:17:38 UP-debian systemd[1]: Failed to start /etc/rc.local Compatibility.

这是我迄今为止尝试过的:

  1. 运行“sudo chmod 777 /test_csv”(py脚本所在的路径)
  2. 将用户更改为:sudo chown -R myuser:myuser /test_csv
  3. 将用户改回 root:sudo chown -R root:root /test_csv
  4. 将“#!/usr/src”作为 .py 脚本的第一行
  5. 制造#! /usr/bin/env python3“.py-script中的第一行
  6. 添加了“sleep 10 &&” --> sleep 10 && sudo -H -u myuser /usr/bin/python3 /python_skript/createTXT.py
  7. 运行“sudo chmod 755 /test_csv”

不幸的是,没有任何帮助。我之前也尝试过使用 cronetab,但这也没有用。

您有什么进一步的想法可以解决我的问题吗?

【问题讨论】:

  • 我认为您没有在 /test_csv 中写入文件。我认为它正在尝试将其写入您运行脚本的同一目录中。看起来它正在尝试将其写入 /etc。
  • 您的代码尝试在当前工作目录中创建/打开文件。在“open”中使用绝对路径。
  • rc.local 在引导过程中执行(如预期的那样),但您不能指望系统已设置好。我认为您的问题是其他一些初始化进程稍后会创建一些安装点,因此您的脚本失败。 10 秒的等待可能还不够。我在 cronttab @reboot 上使用了 10 秒,该脚本稍后在启动时被调用(连同所有“最后步骤”),但你的 rc 脚本被调用得更早。
  • 大家好,感谢您的快速回复!因此,当我手动运行脚本时,它会在“/test_csv/”中创建文件,因为“#dateiPfad = os.getcwd()”是注释。 @GiacomoCatenazzi 当系统启动并运行一段时间时也会发生这种情况,所以这可能是一个问题,但我想这不是我想要解决的问题。还有什么想法吗?
  • @MichaelButscher 刚刚得到了你想要告诉我的内容!会试一试:-)

标签: python linux debian


【解决方案1】:

所以我有时间使用绝对路径测试新解决方案。它奏效了!

这是新代码:

import os
from os import listdir
from os.path import isfile, join

dateiPfad = "/test_csv/"

filenames = [f for f in listdir(dateiPfad) if isfile(join(dateiPfad, f))]

i = 0

# Iterates through files found
for file in range(len(filenames)):

    i = i + 1

#with open('text'+str(i)+'.txt', 'w') as f:
with open('/test_csv/text'+str(i)+'.txt', 'w') as f:
    f.write('Create a new text file!')

非常感谢您的帮助! :-)

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2019-11-27
    • 2020-12-27
    • 1970-01-01
    • 2020-12-07
    相关资源
    最近更新 更多