I am working to restart service if stopped.

I was looking for upstart, to restart service. I found some answers.

I took apache2 service for testing. I changed code as below.

# File : /etc/init.d/apache2
# Function that stops the daemon/service
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred

        # either "stop" or "graceful-stop"
        local STOP=$1
        # can't use pidofproc from LSB here
        local AP_RET=0

        # send stopping apache event
        echo "Stopping....."
        initctl emit stopping-apache2

        if pidof $DAEMON > /dev/null 2>&1 ; then
                if [ -e $PIDFILE ] && pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE) > /dev/null 2>&1 ; then

        # AP_RET is:
        # 0 if Apache (whichever) is not running
        # 1 if Apache (whichever) is running
        # 2 if Apache from the PIDFILE is running

        if [ $AP_RET = 0 ] ; then
                # send stopping apache event
                echo "Stopped.....1"
                initctl emit stopped-apache2

                return 1

        if [ $AP_RET = 2 ] && apache_conftest ; then
                $APACHE2CTL $STOP > /dev/null 2>&1
                apache_wait_stop $?
                # send stopping apache event
                echo "Stopped.....2"
                initctl emit stopped-apache2
                return $?
                if [ $AP_RET = 2 ]; then
                        APACHE2_INIT_MESSAGE="The apache2$DIR_SUFFIX configtest failed, so we are trying to kill it manually. This is almost certainly suboptimal, so please make sure your system is working as you'd expect now!"
                        killproc -p $PIDFILE $DAEMON
                        apache_wait_stop $?
                        # send stopping apache event
                        echo "Stopped.....3"
                        initctl emit stopped-apache2
                        return $?
                elif [ $AP_RET = 1 ] ; then
                        APACHE2_INIT_MESSAGE="There are processes named 'apache2' running which do not match your pid file which are left untouched in the name of safety, Please review the situation by hand".
                        # send stopping apache event
                        echo "Stopped.....4"
                        initctl emit stopped-apache2
                        return 2


Note: emit message using initctl emit stopped-apache2

Then changed apache2.conf to perform start operation on this event.

# File /etc/init/apache2.conf
# apache2
# the purpose of this job is
# * start apache2 in upstart rather than SysV startup

start on stopped-apache2
stop on runlevel [016]

pre-start script
export RUNLEVEL=2
[ -f /var/lib/apache2.enable ] && /etc/init.d/apache2 start
end script

post-stop exec /etc/init.d/apache2 stop

When I stop apache I got message.

root@localhost:~# /etc/init.d/apache2 stop
[....] Stopping web server: apache2Stopping.....
. ok

But seems its not executing start on stopped-apache2.

I also restart job using initctl restart apache2.

I missed something ?

  • You are trying to make apache automatically restart when you stop it with that script? That's an odd way to go about it. Does /var/lib/apache2.enable exist? – psusi Oct 30 '14 at 13:37
  • Your issue is that Upstart does not get events about when init scripts are stopped, only when upstart jobs are stopped. – CameronNemo Oct 31 '14 at 0:57
  • @psusi I want to do same what you mention, automatically restart. I know this is odd way to do things, but I have to do that, no choice for me. :( – Lafada Oct 31 '14 at 4:18
  • @CameronNemo, that's why he explicitly emitted the event in the script – psusi Oct 31 '14 at 15:18
  • Oops did not see that, I guess I should have read better. – CameronNemo Oct 31 '14 at 21:09

You can use the Upstart respawn feature. Use the upstart job for apache found here: https://bitbucket.org/CameronNemo/upstart-jobs/src/2090612200fa588412e3fe22aec5ba3747ae88d2/apache2.conf?at=master

then just add a line that reads respawn.

| improve this answer | |
  • Indeed... you don't want the job restarting itself if you explicitly try to shut it down; only if it crashes. That's what the respawn stanza is for. – psusi Oct 31 '14 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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