Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

After a reboot, I'm executing an application from a mounted network share (autofs) using an init.d script. The script starts by waiting 30 seconds before attempting to execute the command, in order to wait for the network/mount point to be up.

I would like to somehow detect that the network is definitively up and the mount has been definitively mounted, before executing the command.

What is your recommendation on how to achieve this (in CentOS 6.4) with this script?

This is what I have now:

#!/bin/bash
#
# chkconfig: 3 95 5
# description: My app
# processname: my-app
#

# Sleep for 30 seconds before attempting to execute command
sleep 30s

# Get function from functions library
. /etc/init.d/functions

# Start the service my-app from autofs mount
start() {
        echo -n "Starting my-app: "
        /share/path/my-app --log /tmp/log.log --supersede
        ### Create the lock file ###
        touch /var/lock/subsys/my-app
        success $"my-app startup"
        echo
}
# Restart the service my-app
stop() {
        echo -n "Stopping my-app: "
        killproc my-app
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/my-app
        echo
}
### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status my-app
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac
exit 0
share|improve this question
    
possible duplicate of How to change Linux services startup/boot order? –  Dennis Williamson Nov 3 '13 at 12:34
    
I believe what the OP means is the problem that some services start asynchronously, meaning the startup script can be done, but whatever it starts not be available right away. –  Marki Nov 3 '13 at 14:28
    
@Marki No, I really like Alexis' answer. Making sure that the application is reachable is a great way of knowing that it can also be executed in my case. –  fredrik Nov 3 '13 at 18:50
    
No? Well, my comment was about Dennis Williamson's comment. So, Yes! –  Marki Nov 3 '13 at 19:37
    
@Marki Oh, yes of course. Sorry. Yes! –  fredrik Nov 3 '13 at 20:02
show 1 more comment

1 Answer

up vote 4 down vote accepted

init scripts are started in order as defined by the S## numbers. Newer versions of Unix (at least on Linux) start the same ## numbers in parallel (Although you can turn that feature off...) All you have to do it use a ## that's after the network and fsmount number. Then it should work. However, if the fsmount starts in the background, the easiest is probably to probe for files on the mounted drive. Something like this:

while ! test -f /mnt/over-there/this/file/here
do
    sleep 1
done

This will wait until the file appears. If not there yet, sleep for a second then try again.

--- Update: timeout after 30 attempts

In shell script you can also count and test numbers:

count=0
while ! test -f /mnt/over-there/this/file/here -a $count -lt 30
do
    sleep 1
    count=`expr $count + 1`
done
if test $count -eq 30
then
    echo "timed out!"
    exit 1
fi

This will stop if count reaches 30 after it printed the error message "timed out!".

share|improve this answer
    
Absolutely right. Although I would add a timeout at some point that throws an error. –  Marki Nov 3 '13 at 14:29
    
@Alexis-Wilke This is almost perfect. How can I throw an error/timeout after say 30 retries? –  fredrik Nov 3 '13 at 18:49
    
@fredrik: Count to thirty and stop when 30 has been reached. –  Marki Nov 3 '13 at 19:38
    
Also, instead of using test -f you could use mountpoint -q to ensure the mountpoint is actually mounted, and that file hasn't somehow appears on the local filesystem. –  fukawi2 Nov 4 '13 at 5:54
    
fukawi2, I thought of the fact that the file could appear locally. But if so it may be a different bug... –  Alexis Wilke Nov 6 '13 at 6:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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