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 
link|improve this question

39% accept rate
Am I correct in assuming that you already know about and have disabled the System V startup script that is installed by default? – ændrük Jul 7 '11 at 0:03
Yes, the default startup script is disabled and that script doesn't start transmission-daemon with foreground. – Simón Jul 7 '11 at 9:40
feedback

3 Answers

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.

link|improve this answer
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 – Simón Jul 8 '11 at 12:06
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). – enzotib Jul 8 '11 at 12:28
Thanks, but this doesn't work for me. Tranmission will be executed twice with different users. – Simón Jul 8 '11 at 13:30
Executed twice, and with different users in addition, is very very strange – enzotib Jul 8 '11 at 13:40
Yes, one transmission-daemon for each system user. – Simón Jul 14 '11 at 12:54
feedback

My version:

start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]

# give time to send info to trackers
kill timeout 30

expect fork
respawn

script
        USER=debian-transmission
        test -f /etc/default/transmission-daemon && . /etc/default/transmission-daemon
        test "$ENABLE_DAEMON" != "0" || exit 0
        exec start-stop-daemon --start --quiet --chuid $USER --exec /usr/bin/transmission-daemon -- $OPTIONS
end script

Woks good:

root@ubuntu-server:/etc/init# status transmission-daemon
transmission-daemon start/running, process 754
root@ubuntu-server:/etc/init# ps aux|grep [t]ransmission-daemon
torrent    754  0.0  1.1  41592  5596 ?        Ssl  Oct21   0:56 /usr/bin/transmission-daemon --auth --config-dir /var/lib/transmission-daemon/info
link|improve this answer
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. – Simón Oct 22 '11 at 23:15
feedback

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
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.