Linux Is there a way to make a service start after a few seconds?

6pack

ex-Mod
I'm having a problem in linux. I'm using transmisison-daemon which starts as a service. The external disk to which transmisison daemon writes/reads to is mounted through fstab. I thought fstab mounts the disk during boot, so it should be easy for transmission to see the external disk when it starts.

But it seems, transmission daemon either starts before the disk is mounted to /mnt or that fstab mounts the disk to /mnt after linux boots up.

So I tried putting a sleep parameter in transmission daemon init file. I put sleep = 50 (for sleep = 50 seconds). It doesn't work for some reason.

I tried to think of ways to counter this and came up with 2 ways.

1 - put a script in my home folder that runs as soon as linux boots. The script will just stop transmission daemon.
After a 30 second delay, the script will start transmission daemon again.

Code:
#!/bin/bash

sudo service transmission-daemon stop

sleep = 30

sudo service transmision-daemon start

exit


2 - mount the drive in transmission-daemon script.

could i use mount -a in the beginning of the script? It should mount the disk right? If the script does mount the disk, will fstab throw any error?

Another option would be to put a function in beginning of transmisison-daemon script that checks whether the drive is mounted. But if there are 2 or more drives, it won't look good. Also I don't like this way since the file could be over written if the program gets updated in future.

Is there another way to do this?
 
try just "sleep 30" without the equals sign? (sorry this is just a wild guess) also, if the sleep unit is in milliseconds and not seconds then try "sleep 30000" with and without the equals sign?
 
try just "sleep 30" without the equals sign? (sorry this is just a wild guess) also, if the sleep unit is in milliseconds and not seconds then try "sleep 30000" with and without the equals sign?

I think i used the wrong command. I searched stackoverflow and got this:

Code:
Example:

sleep .5 # Waits 0.5 second.
sleep 5  # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.

Ideal thing would be to make sure transmission starts much later and sure that things more mounted.

Assuming its Debian/variant, this might help - https://wiki.debian.org/LSBInitScripts
If ok with more dependencies, try running transmission through supervisord

Yup, ideally, transmission should start last after every other service has started and machine has become idle. I wonder why the daemon starts so quick.

Which variant? You need the startup file to have a sleep delay.

Variant of transmission-daemon? Its the latest 2.92 or something. I installed it from the official ppa. The daemon is started from an /etc/init.d/transmission-daemon script.
This is the script.
Code:
#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          transmission-daemon
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start or stop the transmission-daemon.
# Description:       Enable service provided by transmission-daemon.
### END INIT INFO


NAME=transmission-daemon
DAEMON=/usr/bin/$NAME
USER=debian-transmission
STOP_TIMEOUT=30

export PATH="${PATH:+$PATH:}/sbin"

[ -x $DAEMON ] || exit 0

[ -e /etc/default/$NAME ] && . /etc/default/$NAME

. /lib/lsb/init-functions

start_daemon () {
    if [ $ENABLE_DAEMON != 1 ]; then
        log_progress_msg "(disabled)"
		log_end_msg 255 || true
    else    
        start-stop-daemon --start \
        --chuid $USER \
		$START_STOP_OPTIONS \
        --exec $DAEMON -- $OPTIONS || log_end_msg $?
		log_end_msg 0
    fi
}

case "$1" in
    start)
        log_daemon_msg "Starting bittorrent daemon" "$NAME"
        start_daemon
        ;;
    stop)
        log_daemon_msg "Stopping bittorrent daemon" "$NAME"
        start-stop-daemon --stop --quiet \
            --exec $DAEMON --retry $STOP_TIMEOUT \
            --oknodo || log_end_msg $?
        log_end_msg 0
        ;;
    reload)
        log_daemon_msg "Reloading bittorrent daemon" "$NAME"
        start-stop-daemon --stop --quiet \
            --exec $DAEMON \
            --oknodo --signal 1 || log_end_msg $?
        log_end_msg 0
        ;;
    restart|force-reload)
        log_daemon_msg "Restarting bittorrent daemon" "$NAME"
        start-stop-daemon --stop --quiet \
            --exec $DAEMON --retry $STOP_TIMEOUT \
            --oknodo || log_end_msg $?
        start_daemon
        ;;
    status)
        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
        ;;
    *)
        log_action_msg "Usage: /etc/init.d/$NAME {start|stop|reload|force-reload|restart|status}" || true
        exit 2
        ;;
esac

exit 0

I put the sleep code after the end init info. Maybe i should put it immediately after the #!/bin line on top so it sleeps first.

If linux version, its Linux Mint 18.1.
 
Back
Top