Is there a utility for daemonizing processes as non-privileged user?

Using systemd:

1. Create a service unit configuration file

~/.config/systemd/user/<service_name>.service

[Unit]
Description=<Write some description here>

[Service]
ExecStart=<Write some command here>
Restart=always

[Install]
WantedBy=graphical.target

See man systemd.service and man systemd.unit for more options.

2. Enable service in userspace

$ systemctl --user enable <service_name>

3. Start it emmidiately and check it's status

$ systemctl --user start <service_name>
$ systemctl --user status <service_name>

4. Ensure it starts automatically

Now you can reload PC and login to your graphical environment, then check the service status again.

5. Enjoy!


deamontools you mentioned work just fine as user. See https://cr.yp.to/daemontools/supervise.html

Update - solutions

as per the above suggestion the OP got this working using the svscan program from daemontools after trying two different methods:

  1. Put it in like this a modern crontab: @reboot /usr/bin/svscan $HOME/.local/service 2>&1 > $HOME/.local/service/log
  2. Make ~/.config/autostart/svscan.desktop with the Exec=... line set to launch svscan with a wrapper script. My wrapper script looks like this:

    #!/usr/bin/env sh
    (
      echo "Starting svscan."
      date
      /usr/bin/svscan $HOME/.local/service 2>&1
    ) >> $HOME/.local/service/log
    

Both methods work but each is good for a different situation. The first way is good if you're doing it on a headless machine where you want to allow a non-privileged user to install their own long running services and processes. The second way is good if you want all of the services to inherit the environment, ssh-agent etc. of your currently logged in X user, which means the processes effectively become a proxy of the currently logged in user themself.


If your server runs systemd, you can run services in user-space with systemd --user start myservice. The service files must the be placed in ~/.config/systemd/user/.

If you configure the Restart option of that service, systemd will automatically restart that service when it exits (with different configuration options).

Another option that is independent of systemd might by to start screen. You can check if a screen session exists by calling screen -list, and start if necessary (detached). You can "remotely" execute commands in screen with screen -S "$screen_session" -X stuff 'command\n'. You would then simply start the program in background in that screen session, and remember the PID in a variable.

The cron job uses screen's stuff command to check if the process is still running, and re-starting otherwise.