1

I have a python script that get started automatically as a service (activated with systemd). In this python script, I call a bash script using subprocess.call(script_file,shell=True).

When I call the python script manually ($ python my_python_script.py), everything works perfectly. However, the automatically started program does not execute the bash script (however it does run, I checked this my making it edit a text file, which it indeed does).

I (think) I gave everyone read-write permissions to the bash scripts. Does anyone have ideas as to what I'm doing wrong?

Addendum: I want to write a small script that sends me my public IP address via telegram. The service file looks like this:

[Unit]
Description=IPsender
After=networking.service

[Service]
Type=simple
User=root
WorkingDirectory=/home/pi/projects/tg_bot
ExecStart=/home/pi/miniconda3/bin/python /home/pi/projects/tg_bot/ip_sender_tg.py
Restart=always


[Install]
WantedBy=multi-user.target
11
  • 1
    Does the python script need to fully-path the bash script? Commented Sep 12, 2018 at 15:50
  • 1
    How, exactly, does the python script call the bash script? Commented Sep 12, 2018 at 15:51
  • 1
    I use os.getcwd() to get the correct path to the bash script. Commented Sep 12, 2018 at 15:51
  • 2
    Sounds like an environment problem: there's something in your environment that is not in the daemon's environment. Do you use absolute paths to call the shell script? Do you rely on any environment variables? Commented Sep 12, 2018 at 15:51
  • 2
    @glennjackman What do you mean? I concatenate the entire path and the script name, and give that as the argument, i.e., it looks something like subprocess.call('/home/pi/.../myscript.sh',shell=True) Commented Sep 12, 2018 at 15:57

1 Answer 1

2

Protawn, welcome to the Unix and Linux StackExchange.

Why scripts work differently under system is a common question. Check out this answer to the general question elsewhere on the site.

Without the source code for your Python and Bash scripts it's hard to guess which difference you have encountered.

My personal guess is that your bash script is calling some other binaries without full paths, and those paths are found in your shell $PATH but not the default systemd path.

Add set -x to the top of your bash script so that all actions are logged to standard out, which will be captured in the systemd journal. Then after it fails, use journalctl -u your-service-name to view the logs for your service to see if you can find the last command that bash executed successfully. Also consider using set -e in the bash script to have it stop at the first error.

Despite the two "off-topic" "close" votes on this topic, why things work differently under systemd is on topic for this Stack Exchange site.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure an Answer is warranted yet, if you're guessing and not sure how the OP is calling the script. The output of set -x and journalctl would be clarifying comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.