0

OS: Ubuntu 20.04.6 LTS

I created a service that executes python script checker.py. The problem is this service does not execute python script with root privileges, although sirjay user has root privileges and, moreover, executing sirjay $ sudo python checker.py works.

How I figured out it? This python script does not restart and reboot system when it executes by service (but directly sirjay $ sudo python checker.py restarts and reboots):

import os
os.system("sudo systemctl restart other-task")
os.system("sudo reboot")

Services:

[Unit]
Description=checker.py service
After=network.target

[Service]
User=sirjay
# User=root. <-- does not work also
# Group=www-data
WorkingDirectory=/home/dir

Environment=PYTHONUNBUFFERED=1
Environment="PATH=/home/sirjay/miniconda3/envs/myenv/bin"
ExecStart=/home/dir checker.py
StandardOutput=file:/home/dir/service_checker_output.log
StandardError=file:/home/dir/service_checker_error.log

[Install]
WantedBy=multi-user.target

I don't see any errors in service_checker_error.log. Any ideas what's wrong?

2
  • What does systemctl status unitname show?
    – vidarlo
    Oct 17, 2023 at 23:53
  • Furthermore your unit is littered in mistakes. The period after root in username. The space in ExecStart. Is this in verbatim what the unit file looks like?
    – vidarlo
    Oct 18, 2023 at 0:21

1 Answer 1

0

Answer: os.system("/usr/bin/sudo /sbin/reboot")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .