Linux-Debian:通过rc.local启动Python-script时如何解决“[Errno 13] – Permission denied“?

Linux-Debian: How to solve “[Errno 13] – Permission denied“ when starting Python-script through rc.local?

为了完成学校的期末作业,我目前正在尝试在 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,但是也没用。

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

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

这是新代码:

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!')

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