Issue
For finishing my school’s final assignment, I currently am trying to automatically run the following .py-file (createTXT.py) on my Linux Debian 11 after reboot:
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!')
The code I want to run works perfectly fine when being executed via “python3 createTXT.py”.
I created /etc/rc.local, made it executable etc. This is what it looks like:
#!/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
When I try to run it using “Sudo systemctl start rc-local” the following error regarding a permission denied pops up:
$ 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.
This is what I tried so far:
- Ran “sudo chmod 777 /test_csv” (the path the py-script is located)
- Changed user to : sudo chown -R myuser:myuser /test_csv
- Changed user back to root: sudo chown -R root:root /test_csv
- Made „#!/usr/src“ the first line in the .py-script
- Made #! /usr/bin/env python3“ the first line in the .py-script
- Added „sleep 10 &&“ --> sleep 10 && sudo -H -u myuser /usr/bin/python3 /python_skript/createTXT.py
- Ran “sudo chmod 755 /test_csv”
Unfortunately, nothing helped. I also tried using cronetab before but this didn’t work either.
Do you have any further ideas what could solve my problem?
Solution
So I just had time to test the new solution using an absolute path. And it worked!
Here is the new code:
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!')
Many thanks for your help! :-)
Answered By - BlackLime93 Answer Checked By - Dawn Plyler (WPSolving Volunteer)