LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-23-2011, 01:53 PM   #1
bombshell
LQ Newbie
 
Registered: Dec 2010
Distribution: Fedora 14, Ubuntu 10.10, OpenSUSE 11.3
Posts: 13

Rep: Reputation: 0
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
 
Old 02-23-2011, 02:02 PM   #2
bombshell
LQ Newbie
 
Registered: Dec 2010
Distribution: Fedora 14, Ubuntu 10.10, OpenSUSE 11.3
Posts: 13

Original Poster
Rep: Reputation: 0
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 $
 
Old 02-24-2011, 12:55 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,752

Rep: Reputation: 462Reputation: 462Reputation: 462Reputation: 462Reputation: 462
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Crond script doesn't work as expected Karimo Slackware 1 02-14-2010 06:32 PM
Init script in /etc/init.d does not start at boot tdnnash25 Linux - Server 4 12-18-2009 04:40 PM
Script cd not work as expected cdcshu Linux - Software 1 11-10-2007 04:38 AM
Why won’t this init.d startup script work? dholingw Linux - Software 2 06-08-2004 02:40 PM
Reboot init script doesn't work casey0999 Linux - Software 6 02-25-2004 05:15 PM


All times are GMT -5. The time now is 04:43 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration