1

I've made a program to log certain system stats, and I would like to run that program as a Linux service so I can easily manage it (start, stop, etc). I've spent hours researching and trying things, but I can't seem to get my systemd service file configured correctly.

So far I have this in my service file: (clopper is my username, resourciter is my python program name)

[Unit]
Description=Simplified Resourciter logger service
After=syslog.target

[Service]
Type=simple
User=clopper
Group=clopper
WorkingDirectory=/home/clopper/resourciter/
ExecStart=/usr/bin/python3 /home/clopper/resourciter/runMonitor.py
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target

But I keep having this error when I try to start the service:

pam_unix(sudo:auth): auth could not identify password for [clopper]

I can't seem to find a definitive reference about the ins and outs of configuring a systemd service file.

Any ideas?

1

Looks like your Python script is trying to use sudo to execute commands as root.

That won't really work out of the box when running your script as a service, since there's no terminal attached to the script, so sudo will not have a way to ask for a password to allow your script to run the command as root.

There are ways to configure sudo to run commands without prompting for a password and without requiring a terminal to be attached... But before we go that route, would you consider running your whole script as root instead and not using sudo inside it? That would simplify things...

  • So my program 'runMonitor.py' actually sudo runs my real logger program every 30 seconds (and logs any errors, etc to a log file). So yeah, if I could get around the sudo command by running the script as root, that would work I think. How would I go about doing that? – Jyclop Aug 16 '18 at 2:53
  • 1
    Just remove the User=clopper and Group=clopper, in which case systemd will run it as root. You might want to remove the calls to sudo from your script, since they're not needed if you're running as root (though it's possible everything will keep working even with sudo if you already run as root.) – filbranden Aug 16 '18 at 2:55
  • That fixed that problem (even leaving the sudo commands in the code). – Jyclop Aug 16 '18 at 2:58
  • However now my python program is crashing because it can't find a package import. Any idea why it's having this issue? – Jyclop Aug 16 '18 at 2:59
  • Possibly because you have the modules installed for your user only, not root... Try to import the module on an interactive Python session and look at sys.modules to see where it's coming from. You can try to set the PYTHONPATH environment variable for it to be able to find it as root... – filbranden Aug 16 '18 at 3:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.