Upstart script for a transmission-daemon executed as a normal user

6,556

Solution 1

Ok, the solution is to start transmission-daemon in foreground (no expect fork or daemon) and that start-stop-daemon creates the pid file.
The complete script:

description "Transmission daemon for user"

start on (local-filesystems and net-device-up IFACE=eth0 and runlevel [235])
stop on runlevel [016]

kill timeout 50

respawn

env USER=user
env PIDFILE=/var/run/transmission-user.pid

script
    DAEMON=$(which transmission-daemon) || exit 0
    CONFIGDIR=/home/$USER/.config/transmission-daemon

    exec start-stop-daemon --start --quiet --chuid $USER --pidfile $PIDFILE --make-pidfile --exec $DAEMON -- -f --config-dir $CONFIGDIR --logfile $CONFIGDIR/daemon.log
end script

post-stop exec rm -f $PIDFILE

Solution 2

Using --foreground is correct—you want upstart to track the progress of your transmission process. To omit --foreground add expect daemon to the init config which will follow two forks of the process, which is how services daemonize.

Using start-stop-daemon is suboptimal as upstart can't watch your process and respawn it as necessary in case of crash, etc.

Here's my config:

start on (runlevel [2345] and filesystem and networking)
stop on runlevel [!2345]

respawn
kill timeout 30

setuid transmission

exec /usr/bin/transmission-daemon --foreground --config-dir /var/lib/transmission

Solution 3

When you start an app with su -c, su will wait for the app to terminate. In your case, having added the --foreground option, make transmission to not detach from its parent. So you will see su as a parent process of transimssion-daemon for all the time the latter lives.

If you remove that option, you will see that su process will terminate as soon as transmission-daemon goes to the background.

Apart from removing that option that seems inopportune for a service, I suggest to use

sudo -u <your-user> app-name options

instead of su, being more close to the Ubuntu way of doing things, and being more simple to manage options without the need to use single quotes.

Share:
6,556

Related videos on Youtube

Juan Simón
Author by

Juan Simón

Updated on September 18, 2022

Comments

  • Juan Simón
    Juan Simón over 1 year

    I have done a script to start transmission-daemon as a normal user:

    start on filesystem
    stop on runlevel [!2345]
    
    respawn
    respawn limit 10 5
    
    pre-start script
        test -x /usr/bin/transmission-daemon || { stop; exit 0; }
        test -d /home/user/.config/transmission-daemon || { stop; exit 0; }
    end script
    
    exec su -l -c 'transmission-daemon --foreground --config-dir /home/user/.config/transmission-daemon --logfile /home/user/.config/transmission-daemon/daemon.log' user
    

    This script works but I see two processes in execution of transmission-daemon:

    user     5041  0.0  0.0  48556  1516 ?        Ss   01:10   0:00 su -l -c transmission-daemon --foreground --config-dir /home/user/.config/transmission-daemon --logfile /home/user/.config/transmission-daemon/daemon.log user
    user     5048  0.5  0.0 150432  2960 ?        Sl   01:10   0:00 transmission-daemon --foreground --config-dir /home/user/.config/transmission-daemon --logfile /home/user/.config/transmission-daemon/daemon.log
    

    Is this correct? Is there another way to execute this better?

    Note: The default startup script of transmission package is disabled.

    More info:

    If I execute transmission as a daemon (without foreground) the problem is the detected PID by init:

    start on filesystem
    stop on runlevel [!2345]
    
    expect fork
    
    pre-start script
        test -x /usr/local/bin/transmission-daemon || { stop; exit 0; }
        test -d /home/mario/.config/transmission-daemon || { stop; exit 0; }
    end script
    
    exec sudo -u user transmission-daemon --config-dir /home/user/.config/transmission-daemon --logfile /home/user/.config/transmission-daemon/daemon.log
    

    .

    $ sudo initctl list | grep trans
    trans-test start/running, process 3110
    

    but really this is the PID of sudo (finished process), the transmission-daemon PID is another:

    $ ps aux 
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    user     3148  0.0  0.0 154848  2708 ?        Ssl  13:33   0:00 transmission-daemon 
    
    • RusGraf
      RusGraf almost 13 years
      Am I correct in assuming that you already know about and have disabled the System V startup script that is installed by default?
    • Juan Simón
      Juan Simón almost 13 years
      Yes, the default startup script is disabled and that script doesn't start transmission-daemon with foreground.
  • Juan Simón
    Juan Simón almost 13 years
    But if I supress foreground, init detects wrong the process PID. It saves the PID of sudo but sudo is finished: initctl: trans-test start/running, process 3110 but the process has other PID: user 3148 0.0 0.0 154848 2708 ? Ssl 13:33 0:00 transmission-daemon
  • enzotib
    enzotib almost 13 years
    An untested alternative could be to get ownership of the executable (sudo chown $USER:$USER /usr/bin/transmission-daemon), then make it setuid (chmod u+s /usr/bin/transmission-daemon).
  • Juan Simón
    Juan Simón almost 13 years
    Thanks, but this doesn't work for me. Tranmission will be executed twice with different users.
  • enzotib
    enzotib almost 13 years
    Executed twice, and with different users in addition, is very very strange
  • Juan Simón
    Juan Simón almost 13 years
    Yes, one transmission-daemon for each system user.
  • Juan Simón
    Juan Simón over 12 years
    This script doesn't work to me: transmission-simon start/running, process 5880 but process 5880 doesn't exist. I think upstart takes the PID of start-stop-daemon command instead of the transmission-daemon.