Systemd.user.services

Trying to put my script/binary into a service does not work properly.

systemd.user.services.datensicherung = {
  enable = true;
  after = [ "network.target" ];
  wantedBy = [ "default.target" ];
  description = "Datensicherung";
  serviceConfig = {
      Type = "simple";
      ExecStart = ''/home/xxx/datensicherung'';
  };
};

Starting the script/binary (/home/xxx/datensicherung) manually works fine.

Starting the script/binary via service does not write to the external disk (/run/media/datensicherung/), but writing the logs to the local disk (/home/xxx/) works.

so how do you mount the external disk? after logging in?
may you include some kind of logging into your script and then check what’s exactly wrong

systemctl --user start datensicherung.service

systemctl --user status datensicherung.service

journalctl --user -xeu datensicherung.service

When the script is started by systemd it runs in a completely different (empty) environment compared to your login shell.

You need to add the packages you need to systemd.user.services.<name>.path, define environment variables with systemd.user.services.<name>.environment, etc.

Also, note that network.target does not exist for user services.

4 Likes

@rnhmjoj @kekneus373
Thank you for the informations.

It was a timeing issue. The USB-disk needs more time to be mounted…

I switched to GNOME autostart…

  home.file = {
      ".config/autostart/datensicherung.desktop" = {
        enable = true;
        text = ''
        [Desktop Entry]
        Name=Datensicherung
        Exec=bash -c "sleep 60 && cd /home/xxx/ && /home/xxx/datensicherung"
        Terminal=false
        Type=Application
        X-GNOME-Autostart-enabled=true
      '';
  };
  };
1 Like