How to run systemd command at startup?

How can I apply this method to systemctl restart nut-server; ?

This is what I’m trying to solve:

systemctl status nut-server
● nut-server.service - Network UPS Tools - power devices information server
     Loaded: loaded (/usr/lib/systemd/system/nut-server.service; enabled; vendor preset: disabled)
     Active: active (running) since Mon 2021-03-08 10:07:01 AST; 28s ago
    Process: 670 ExecStart=/usr/bin/upsd (code=exited, status=0/SUCCESS)
   Main PID: 685 (upsd)
      Tasks: 1 (limit: 19133)
     Memory: 728.0K
     CGroup: /system.slice/nut-server.service
             └─685 /usr/bin/upsd

Mar 08 10:07:01 pyro-ab350gaming3 systemd[1]: Starting Network UPS Tools - power devices information server...
Mar 08 10:07:01 pyro-ab350gaming3 upsd[670]: fopen /run/nut/upsd.pid: No such file or directory
Mar 08 10:07:01 pyro-ab350gaming3 upsd[670]: listening on 127.0.0.1 port 3493
Mar 08 10:07:01 pyro-ab350gaming3 upsd[670]: listening on 127.0.0.1 port 3493
Mar 08 10:07:01 pyro-ab350gaming3 upsd[670]: listening on ::1 port 3493
Mar 08 10:07:01 pyro-ab350gaming3 upsd[670]: listening on ::1 port 3493
Mar 08 10:07:01 pyro-ab350gaming3 upsd[670]: Can't connect to UPS [upsilon-2000] (blazer_usb-upsilon-2000): No such file or directory
Mar 08 10:07:01 pyro-ab350gaming3 upsd[670]: Can't connect to UPS [upsilon-2000] (blazer_usb-upsilon-2000): No such file or directory
Mar 08 10:07:01 pyro-ab350gaming3 upsd[685]: Startup successful
Mar 08 10:07:01 pyro-ab350gaming3 systemd[1]: Started Network UPS Tools - power devices information server.

What I have tried:

/usr/share/applications/org.upsd-session.desktop  
[Desktop Entry]
Name=UPSD NUT Server
GenericName=UPSD NUT Server
Comment=Control your computer with ease using AC events
Exec=systemctl restart nut-server
Icon=corectrl
StartupNotify=true
Terminal=true
Type=System
Categories=System;Settings;Utility;
Keywords=nut;ups;system;hardware;

/etc/polkit-1/rules.d/90-upsd.rules
polkit.addRule(function(action, subject) {
  if (action.id == "org.freedesktop.upsd-session.manage-units" &&
    action.lookup("unit") == "docker.service")  &&
    subject.isInGroup("pyro"))
    {
      return polkit.Result.YES;
    }
})

This is the service @ /usr/lib/systemd/system

[Unit]
Description=Network UPS Tools - power device driver controller
After=local-fs.target network.target
StopWhenUnneeded=yes

[Service]
ExecStart=/usr/bin/upsdrvctl start
ExecStop=/usr/bin/upsdrvctl stop
Type=forking

I added multi-user.target and now i get:

systemctl status nut-server 
● nut-server.service - Network UPS Tools - power devices information server
     Loaded: loaded (/usr/lib/systemd/system/nut-server.service; enabled; vendor preset: disabled)
     Active: inactive (dead)

[Unit]
Description=Network UPS Tools - power device driver controller
After=multi-user.target local-fs.target network.target
StopWhenUnneeded=yes

[Service]
ExecStart=/usr/bin/upsdrvctl start
ExecStop=/usr/bin/upsdrvctl stop
Type=forking

Don’t know a thing about docker. That said…

What are you trying to do? Not use sudo/enter a sudo password for the systemctl command?

Why are you trying to fudge a polkit rule that was written for a different application?

Have you looked at/read the wiki article (starting/enabling the service)?

Thank you for the answer. Yes, I’m just guessing what I can do. The problems seems to be that the NUT Server service start before USB is active.

When I log and run the status for the service I get:

systemctl status nut-server
● nut-server.service - Network UPS Tools - power devices information server
     Loaded: loaded (/usr/lib/systemd/system/nut-server.service; enabled; vendor preset: disabled)
     Active: active (running) since Mon 2021-03-08 15:11:08 AST; 2min 14s ago
    Process: 615 ExecStart=/usr/bin/upsd (code=exited, status=0/SUCCESS)
   Main PID: 619 (upsd)
      Tasks: 1 (limit: 19133)
     Memory: 752.0K
     CGroup: /system.slice/nut-server.service
             └─619 /usr/bin/upsd

Mar 08 15:11:08 pyro-ab350gaming3 systemd[1]: Starting Network UPS Tools - power devices information server...
Mar 08 15:11:08 pyro-ab350gaming3 upsd[615]: fopen /run/nut/upsd.pid: No such file or directory
Mar 08 15:11:08 pyro-ab350gaming3 upsd[615]: listening on 127.0.0.1 port 3493
Mar 08 15:11:08 pyro-ab350gaming3 upsd[615]: listening on ::1 port 3493
Mar 08 15:11:08 pyro-ab350gaming3 upsd[615]: listening on 127.0.0.1 port 3493
Mar 08 15:11:08 pyro-ab350gaming3 upsd[615]: listening on ::1 port 3493
Mar 08 15:11:08 pyro-ab350gaming3 upsd[615]: Can't connect to UPS [upsilon-2000] (blazer_usb-upsilon-2000): Connection refused
Mar 08 15:11:08 pyro-ab350gaming3 upsd[615]: Can't connect to UPS [upsilon-2000] (blazer_usb-upsilon-2000): Connection refused
Mar 08 15:11:08 pyro-ab350gaming3 upsd[619]: Startup successful
Mar 08 15:11:08 pyro-ab350gaming3 systemd[1]: Started Network UPS Tools - power devices information server.

PID files is there

sudo ls /run/nut/upsd.pid
/run/nut/upsd.pid

When I run this command it seems to be fixed

systemctl restart nut-server && systemctl status nut-server
● nut-server.service - Network UPS Tools - power devices information server
     Loaded: loaded (/usr/lib/systemd/system/nut-server.service; enabled; vendor preset: disabled)
     Active: active (running) since Mon 2021-03-08 15:17:47 AST; 9ms ago
    Process: 5334 ExecStart=/usr/bin/upsd (code=exited, status=0/SUCCESS)
   Main PID: 5335 (upsd)
      Tasks: 1 (limit: 19133)
     Memory: 624.0K
     CGroup: /system.slice/nut-server.service
             └─5335 /usr/bin/upsd

Mar 08 15:17:47 pyro-ab350gaming3 systemd[1]: Starting Network UPS Tools - power devices information server...
Mar 08 15:17:47 pyro-ab350gaming3 upsd[5334]: fopen /run/nut/upsd.pid: No such file or directory
Mar 08 15:17:47 pyro-ab350gaming3 upsd[5334]: listening on 127.0.0.1 port 3493
Mar 08 15:17:47 pyro-ab350gaming3 upsd[5334]: listening on ::1 port 3493
Mar 08 15:17:47 pyro-ab350gaming3 upsd[5334]: listening on 127.0.0.1 port 3493
Mar 08 15:17:47 pyro-ab350gaming3 upsd[5334]: listening on ::1 port 3493
Mar 08 15:17:47 pyro-ab350gaming3 upsd[5334]: Connected to UPS [upsilon-2000]: blazer_usb-upsilon-2000
Mar 08 15:17:47 pyro-ab350gaming3 upsd[5334]: Connected to UPS [upsilon-2000]: blazer_usb-upsilon-2000
Mar 08 15:17:47 pyro-ab350gaming3 upsd[5335]: Startup successful
Mar 08 15:17:47 pyro-ab350gaming3 systemd[1]: Started Network UPS Tools - power devices information server.

I’ve not tried this myself but this question + awnsers might be something to look into to solve the problem of a service starting only after a USB device is present.

1 Like

Yes, I did all that but never saw the error I was having. But that thread is closed