1

I have a python service that is running via systemd on a raspberry-pi.

I want to use subprocess.call to copy a file from ~ to /etc/wpa_supplicant/

When I start the python process from the comandline I have no problem executing the subprocess.call, unfortunately when started as systemd service its not working.

service file:

[Unit]
Description=some-description

[Service]
TimeoutSec=infinity
Type=simple
Restart=on-failure
RestartSec=15s
WorkingDirectory=/home/%i/
ExecStart=/home/%i/start_script.sh

[Install]
WantedBy=default.target

Python:

subprocess.call(["sudo", "cp", file_path_str, "/etc/wpa_supplicant/wpa_supplicant.conf.temp"])

But that gives following log-output:

pi : TTY=unknown ; PWD=/home/pi/ ; USER=root ; COMMAND=/bin/cp file_path_str /etc/wpa_supplicant/wpa_supplicant.conf.temp

Then I tried running it without sudo in the call(), that just tells me that permission is denied:

cp: cannot create regular file '/etc/wpa_supplicant/wpa_supplicant.conf.save': Permission denied

I thought that services are run as root anyway, why do I have permission problems? Is there a neat way to solve this?

  • "But that gives folloing error" - what you show here is not an error message but a log statement. Also, the first thing to check would be that input path is correct (and that the output directory exists, but I'd assume the wpa_supplicant dir should be created by default on raspbian). – l4mpi Jan 11 '21 at 15:01
  • @l4mpi Yes That assumption is correct. It is created by default. All paths are correct and double-checked. – Simon Schuler Jan 11 '21 at 15:09
  • Check that the target file either doesn't exist, or has write permissions so it can be overwritten. Also, please edit the question with the actual error message instead of the line logging the command. You're correct that services are run as root unless you specify a user, as you should be able to easily verify in your start script. I'd also suggest to try the copy command in the shell script and see what happens, might give you another indication what's wrong. – l4mpi Jan 11 '21 at 16:15
  • As said, it works fine when I start the python from the command line. This implies that everything with the code is fine. The problem is with the fact that it dosent work anymore when I start it as service. – Simon Schuler Jan 12 '21 at 07:38
  • The env is different as a service so that's one thing that you can't verify by running the script yourself. However that should be irrelevant here as the journalctl line shows that the cp command is executed as root, so as long as that command looks correct to you it should work (assuming the `file_path_str` in that line isn't the actual output, but your substitution of the source path with the var name from python). I'd recommend to redirect the whole output of the start script by adding `exec 1>>/tmp/wpaservice.log 2>&1` after the shebang and checking if the log contains further messages. – l4mpi Jan 12 '21 at 10:17

0 Answers0