Solved

Start apache service mix on linux boot

Posted on 2014-12-02
3
623 Views
Last Modified: 2015-01-02
I'm running apache service mix 5.1.2.  I'd like to have it start up as service in ubuntu when I boot linux.  How can I accomplish this?
0
Comment
Question by:HyperBPP
3 Comments
 
LVL 23

Accepted Solution

by:
Dr. Klahn earned 500 total points
ID: 40479996
First you need a shell script that starts up the products.  If you look in /etc/init.d, you'll see many scripts used during the startup sequence to serve as examples of how to do this.

This is by far the biggest stumbling block -- writing a shell script that both (a) starts up the software as you want it started up, and (b) does it legally and acceptably according to the proper method.

You may find it easier to write individual scripts for each individual product and execute them in the proper order if there are interdependencies.  For this, the "provides" and "required-start" parameters are helpful.  update-rc.d (see bottom) will sort out the dependencies for you and start the products in the required order as long as the "required-start" parameters are set properly.

Below is the /etc/init.d/apache script from my own system:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          apache
# Required-Start:    $syslog networking
# Required-Stop:     $syslog
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# Default-Start:     S 2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Apache
# Description:       Provides the Apache web server
### END INIT INFO
#
# apache        Start the apache HTTP server.
#
# The variables below should not normally be changed.  They are there to make the
# script more readable.

NAME=apache
# Don't use the Debian default; our install is custom
# DAEMON=/usr/sbin/$NAME
DAEMON=/usr/local/apache2/bin/httpd
# Don't use the Debian default; our install is custom
# PIDFILE=/var/run/$NAME.pid
PIDFILE=/usr/local/apache2/logs/httpd.pid
# Don't use the Debian default; our install is custom
# CONF=/etc/$NAME/httpd.conf
CONF=/usr/local/apache2/conf/httpd.conf
# Don't use the Debian default; our install is custom
# APACHECTL=/usr/sbin/${NAME}ctl
APACHECTL=/usr/local/apache2/bin/apachectl
# note: SSD is required only at startup of the daemon.
SSD=`which start-stop-daemon`
ENV="env -i LANG=C PATH=/bin:/usr/bin:/usr/local/bin"

trap "" 1

# Check that we're not being started by inetd
if egrep -q -i "^[[:space:]]*ServerType[[:space:]]+inet" $CONF
then
    exit 0
fi

test_config() {
    if [ ! -x $APACHECTL ]; then
        echo "$APACHECTL is not executable, exiting"
        exit 0
    fi

    # ensure we don't leak environment vars into apachectl
    APACHECTL="$ENV $APACHECTL"

    if ! $APACHECTL configtest 2> /dev/null
    then
        printf "Configuration syntax error detected. Not reloading.\n\n"
        $APACHECTL configtest
        exit 1
    fi
}

should_start() {
    if [ ! -x $DAEMON ]; then
        echo "apache is not executable, not starting"
        exit 0
    fi
}

case "$1" in
  start)
    should_start
    test_config
    echo "Starting web server: $NAME"
#   $ENV $SSD --start --pidfile $PIDFILE --exec $DAEMON > /dev/null
    $APACHECTL start
    ;;

  stop)
    echo "Stopping web server: $NAME"
#   start-stop-daemon --stop --pidfile $PIDFILE --oknodo
    $APACHECTL stop
    ;;

  reload | force-reload)
    test_config
    echo "Reloading $NAME configuration"
    start-stop-daemon --stop --pidfile $PIDFILE --signal USR1
    ;;

  restart)
    test_config
    echo "Restarting $NAME"
    if ! start-stop-daemon -q --stop --pidfile $PIDFILE --signal HUP; then
        $ENV $SSD --start --pidfile $PIDFILE --exec $DAEMON > /dev/null
    fi
    ;;

  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|reload|force-reload|restart}"
    exit 1
    ;;
esac

if [ $? -eq 0 ]; then
        exit 0
else
        echo " failed"
        exit 1
fi

Open in new window


Copy the script to /etc/init.d and and use chmod to make sure that it's executable.  Set the protection bits to 755.

Then use update-rc.d to add the script to the startup scripts.

update-rc.d (scriptfilename) defaults

Open in new window


After this comes more debugging.  It's probable that the script won't work the first, second or tenth time, and the only way to test it is by rebooting the system, looking at the syslog, and checking operation to see if it started correctly.

If you think this is a colossal hassle, you are correct.  debian (Ubuntu is based on debian) installs both the packages and the startup scripts for you if you use apt-get install ... but the resulting installation is the debian "stable" packages, not the latest.  If you want to use anything other than the debian distribution packages the only way to do it reliably is by rolling your own shell script.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Read about why website design really matters in today's demanding market.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now