Search code examples
pythonbashsystemd

Strange behaviour difference between terminal and bash script for python module


I installed a python script for updating my dyndns record for my nextcloudpi: https://github.com/Domain-Connect/DomainConnectDDNS-Python When i run the program from the terminal by using domain-connect-dyndns update --all it works out well. Now i created a systemd service and a timer calling the service. The service is calling a very simple bash file:

#!/bin/bash

/home/pi/.local/bin/domain-connect-dyndns update --all

when i check the execution of the service it turns out like that:

pi@nextcloudpi:~ $ systemctl status ddns.service
● ddns.service - Update IONOS DNS Record
   Loaded: loaded (/etc/systemd/system/ddns.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2020-10-18 20:20:10 BST; 45s ago
  Process: 18391 ExecStart=/bin/bash /home/pi/own-bashscripts/execddns.sh (code=exited, status=1/FAILURE)
 Main PID: 18391 (code=exited, status=1/FAILURE)

Oct 18 20:20:06 nextcloudpi systemd[1]: Started Update IONOS DNS Record.
Oct 18 20:20:10 nextcloudpi bash[18391]: Couldn't read setttings.
Oct 18 20:20:10 nextcloudpi bash[18391]: Traceback (most recent call last):
Oct 18 20:20:10 nextcloudpi bash[18391]:   File "/home/pi/.local/bin/domain-connect-dyndns", line 10, in <module>
Oct 18 20:20:10 nextcloudpi bash[18391]:     sys.exit(main())
Oct 18 20:20:10 nextcloudpi bash[18391]:   File "/home/pi/.local/lib/python3.7/site-packages/dyndns/command_line.py
Oct 18 20:20:10 nextcloudpi bash[18391]:     domains = config.keys()
Oct 18 20:20:10 nextcloudpi bash[18391]: UnboundLocalError: local variable 'config' referenced before assignment
Oct 18 20:20:10 nextcloudpi systemd[1]: ddns.service: Main process exited, code=exited, status=1/FAILURE
Oct 18 20:20:10 nextcloudpi systemd[1]: ddns.service: Failed with result 'exit-code'.

Can anybody tell me what i'm missing? Why, that python issue is occuring only when i call it with the script, and everything is working when i execute it from the terminal? Thanks


Solution

  • The script cannot find settings.py. You need to set WorkingDirectory config in service configuration file or provide path to settings in the script parameters:

    # don't forget to change the path
    domain-connect-dyndns update --all --config /home/pi/ddnsdir/settings.txt