LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting a INIT script to work as expected (https://www.linuxquestions.org/questions/programming-9/getting-a-init-script-to-work-as-expected-864595/)

bombshell 02-23-2011 01:53 PM

Getting a INIT script to work as expected
 
This is my first init script, so far, it atleast starts the daemon but it fails at creating a pid, so later on I can use to, to either stop it or reload it. Here's the current code:

Further more, the script returns ok that it started the pid, when in fact, the pid fails in the background, this script needs MUCH improvement, some help would be nice to fix this script. :/

Code:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          znc
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: znc initscript
# Description:      This is the init-Script for znc.
### END INIT INFO

# Author: Bombshell
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# Read configuration variable file if it is present
if [ -r /etc/sysconfig/znc ]; then
  . /etc/sysconfig/znc
else
  log_failure_msg 'Error: Missing ZNC sysconfig: Unable to start'
  exit 3
fi

# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then
  log_failure_msg 'Error: Missing ZNC binary: Unable to start'
  exit 3
fi

#
# Function that starts the daemon/service
#
do_start()
{
        # Start ZNC
        su - znc -c "$DAEMON $DAEMON_ARGS"
        #start_daemon -u $ZNC_USER -p $ZNC_PID_FILE "$DAEMON $DAEMON_ARGS"
        PID=$!
        echo $PID
        if [ -n "$PID" ]; then
          if kill -0 $PID
          then
            echo $PID > $ZNC_PID_FILE
          fi
        fi
        RETVAL=$?
}

#
# Function that stops the daemon/service
#
do_stop()
{
        # Kill znc
        killproc -p $ZNC_PID_FILE "$DAEMON $DAEMON_ARGS"
        RETVAL=$?
        #rm -f $ZNC_PID_FILE
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
        #
        # If the daemon can reload its configuration without
        # restarting (for example, when it is sent a SIGHUP),
        # then implement that here.
        #
        killproc -p $ZNC_PID_FILE "$DAEMON $DAEMON_ARGS" -signal 1
        RETVAL=$?
}

case "$1" in
  start)
        echo -n $"Starting $NAME....."
        do_start
        if [ $? -eq 0 ]; then
          echo -e "OK\n";
        else
          echo -e "Failed\n";
        fi
          ;;
  stop)
        echo -n $"Stoping $NAME....."
        do_stop
        if [ $? -eq 0 ]; then
          echo -e "OK\n";
        else
          echo -e "Failed\n";
        fi
        ;;
  reload)
        echo -n $"Reloading $NAME....."
        do_reload
        if [ $? -eq 0 ]; then
          echo -e "OK\n";
        else
          echo -e "Failed\n";
        fi
        ;;
  #reload|force-reload)
        #
        # If do_reload() is not implemented then leave this commented out
        # and leave 'force-reload' as an alias for 'restart'.
        #
        #log_daemon_msg "Reloading $DESC" "$NAME"
        #do_reload
        #log_end_msg $?
        #;;
  restart|force-reload)
        #
        # If the "reload" option is implemented then remove the
        # 'force-reload' alias
        #
        echo -n $"Restarting $NAME: "
        echo -n "Stoping....."       
        do_stop
        if [ $? -eq 0 ]; then
          echo -e "OK\n";
        else
          echo -e "Failed\n";
        fi
        echo -n "Starting....."
        do_start
        if [ $? -eq 0 ]; then
          echo -e "OK\n";
        else
          echo -e "Failed\n";
        fi
        ;;
  *)
        #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        exit 3
        ;;
esac


bombshell 02-23-2011 02:02 PM

Here's my second attempt

Code:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          znc
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: znc initscript
# Description:      This is the init-Script for znc.
### END INIT INFO

# Author: Bombshell
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# Read configuration variable file if it is present
if [ -r /etc/sysconfig/znc ]; then
  . /etc/sysconfig/znc
else
  log_failure_msg 'Error: Missing ZNC sysconfig: Unable to start'
  exit 3
fi

# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then
  log_failure_msg 'Error: Missing ZNC binary: Unable to start'
  exit 3
fi

#
# Function that starts the daemon/service
#
do_start()
{
        # Start ZNC
        PID=`su - znc -c "$DAEMON $DAEMON_ARGS"`
        #start_daemon -u $ZNC_USER -p $ZNC_PID_FILE "$DAEMON $DAEMON_ARGS"
        #PID=$!
        echo $PID
        if [ -n "$PID" ]; then
          if kill -0 $PID
          then
            echo $PID > $ZNC_PID_FILE
          fi
        fi
        RETVAL=$?
}

#
# Function that stops the daemon/service
#
do_stop()
{
        # Kill znc
        killproc -p $ZNC_PID_FILE "$DAEMON $DAEMON_ARGS"
        RETVAL=$?
        #rm -f $ZNC_PID_FILE
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
        #
        # If the daemon can reload its configuration without
        # restarting (for example, when it is sent a SIGHUP),
        # then implement that here.
        #
        killproc -p $ZNC_PID_FILE "$DAEMON $DAEMON_ARGS" -signal 1
        RETVAL=$?
}

case "$1" in
  start)
        echo -n $"Starting $NAME....."
        do_start
        if [ $? -eq 0 ]; then
          echo -e "OK\n";
        else
          echo -e "Failed\n";
        fi
          ;;
  stop)
        echo -n $"Stoping $NAME....."
        do_stop
        if [ $? -eq 0 ]; then
          echo -e "OK\n";
        else
          echo -e "Failed\n";
        fi
        ;;
  reload)
        echo -n $"Reloading $NAME....."
        do_reload
        if [ $? -eq 0 ]; then
          echo -e "OK\n";
        else
          echo -e "Failed\n";
        fi
        ;;
  #reload|force-reload)
        #
        # If do_reload() is not implemented then leave this commented out
        # and leave 'force-reload' as an alias for 'restart'.
        #
        #log_daemon_msg "Reloading $DESC" "$NAME"
        #do_reload
        #log_end_msg $?
        #;;
  restart|force-reload)
        #
        # If the "reload" option is implemented then remove the
        # 'force-reload' alias
        #
        echo -n $"Restarting $NAME: "
        echo -n "Stoping....."       
        do_stop
        if [ $? -eq 0 ]; then
          echo -e "OK\n";
        else
          echo -e "Failed\n";
        fi
        echo -n "Starting....."
        do_start
        if [ $? -eq 0 ]; then
          echo -e "OK\n";
        else
          echo -e "Failed\n";
        fi
        ;;
  *)
        #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        exit 3
        ;;
esac

Quote:

15:00:56 root@nexus /etc/init.d $ service znc start
Starting znc.....-bash: -c: line 0: syntax error near unexpected token `;'
-bash: -c: line 0: `/usr/bin/znc -f 1> /etc/znc/znc.log &; echo ""'

OK

15:02:05 root@nexus /etc/init.d $

gnashley 02-24-2011 12:55 AM

You were closer with the first one. This:
PID=$!
gets the pid of the last *backgrounded* process, so you need to use the '&':
Code:

znc -c "$DAEMON $DAEMON_ARGS" &
PID=$!
echo $PID

Note that there is no need to use 'su' -init scripts are being run by root already.


All times are GMT -5. The time now is 04:14 AM.