0

I am having troubles executing a systemd service just before state suspend.target but it does not work. Instead the systemd service seems to execute after suspend.target no matter what I do. I tried a variety of things however I don't know what to look for anymore.

This is what I currently have:

  • The "main" service file, running a python script:

    python_service.service:

    [Unit]
    Description=Python Systemd Service
    
    [Service]
    ExecStart=/usr/bin/python3 /home/user/python_service.py --user "user" --password "password"
    Environment=XAUTHORITY=/home/user/.Xauthority
    Environment=DISPLAY=:0.0
    Environment=PYTHONUNBUFFERED=1
    Restart=on-failure
    RestartSec=5
    User=root
    
    [Install]
    WantedBy=multi-user.target
    
  • Now to stop and restart this service I decided to create 2 other systemd service files with the only purpose to execute systemctl stop python_service.service and systemctl restart python_service.service like this:

    python_service_stop.service:

    [Unit]
    Description=Stop python_service before suspend
    Before=suspend.target
    
    [Service]
    Type=simple
    ExecStart=/bin/systemctl stop python_service.service
    User=root
    
    [Install]
    WantedBy=multi-user.target
    

And no matter what I try it always stops this service after the PC resumes despite defining Before=suspend.target.

Now the question is: How can I make it so python_service.service does actually stop before reaching suspend.target?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.