Systemd service script "command not found"

Hi,

I am running into some troubles while trying to get a custom shell script running as a systemd service.

The script doesn’t seem to have access to the PATH variable, because the root user can execute the script in the shell.

Do you know if I can fix that ?

× nextcloud-backup.service
     Loaded: loaded (/etc/systemd/system/nextcloud-backup.service; linked; preset: enabled)
     Active: failed (Result: exit-code) since Mon 2024-07-29 20:48:08 UTC; 3min 41s ago
   Duration: 38ms
TriggeredBy: ● nextcloud-backup.timer
    Process: 87696 ExecStart=/nix/store/m8k0zvrmfznlp0xdaagr45diialppf4j-unit-script-nextcloud-backup-start/bin/nextcloud-backup-start (code=exited, status=127)
   Main PID: 87696 (code=exited, status=127)
         IP: 0B in, 0B out
        CPU: 22ms

Jul 29 20:48:08 vps systemd[1]: Started nextcloud-backup.service.
Jul 29 20:48:08 vps nextcloud-backup-start[87698]: /usr/local/sbin/nextcloud-backup: line 5: nextcloud-occ: command not found
Jul 29 20:48:08 vps nextcloud-backup-start[87699]: /usr/local/sbin/nextcloud-backup: line 6: sudo: command not found
Jul 29 20:48:08 vps nextcloud-backup-start[87700]: /usr/local/sbin/nextcloud-backup: line 7: sudo: command not found
Jul 29 20:48:08 vps nextcloud-backup-start[87701]: /usr/local/sbin/nextcloud-backup: line 8: rsync: command not found
Jul 29 20:48:08 vps nextcloud-backup-start[87703]: /usr/local/sbin/nextcloud-backup: line 9: ssh: command not found
Jul 29 20:48:08 vps nextcloud-backup-start[87704]: /usr/local/sbin/nextcloud-backup: line 10: nextcloud-occ: command not found
Jul 29 20:48:08 vps systemd[1]: nextcloud-backup.service: Main process exited, code=exited, status=127/n/a
Jul 29 20:48:08 vps systemd[1]: nextcloud-backup.service: Failed with result 'exit-code'.
2 Likes

Check the options under systemd.services, particularly environment, path, or script depending on how you implemented it.

3 Likes

User shells get stuff like /run/current-system/sw/bin on their PATH that systemd services don’t. Services basically get a clean environment. So yea, the systemd.services.<name>.path option is the easiest way to add things to PATH if you used systemd.services.<name>.script.

2 Likes

oh, I didn’t see this. thanks!
I’ll take a look. Do you know if I can/should link the root PATH variable, with something like $PATH ?

That won’t work. PID 1 itself only has a very minimal PATH. User sessions get more in their PATH through shell profile scripts.

1 Like

Thanks. For the time being, I’ve gone with something like

systemd.services.<name>.path = ["/run/current-system/sw"];

I’d prefer to specify each package path, but I’ll try later, because at the moment nix-locate isn’t usable on a 1GB machine.

2 Likes