Utility for daemonizing processes as non-privileged user

background-processnot-root-userprocessprocess-managementusers

A thing I frequently want to do is launch some long running process or server as my own non-privileged user and then have a way to tell if it's still running and restart it if not.

So for example I might set a cron job that runs every so often checking if my process is running and restart it if it has crashed. This is the essence of process management tools like djb's daemontools, supervisord, launchd etc. except that those tools are configured by default to run as root with config files in /etc but I would like a utility that lets me do the same sort of thing as my non-privileged user from the comfort of my home directory.

Best Answer

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.

Related Question