LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-17-2015, 10:16 AM   #1
RetroSpock
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Rep: Reputation: Disabled
Issues creating an init script


Hey,

I'm struggling with this init script I've created.

The process runs fine, but I'm unable to get the PIDFILE to create, and I can't kill the process because of this.

Other than the issues with the PID/PIDFILE, the script works fine.

I even tried a

pid=`ps -A |grep java |cut -d" " -f1`
echo ${pid} > ${pidfile}

which works in bash, just not within the script

Code:
#!/bin/bash
#
# stock-management
#
# chkconfig: 2345 80 30
# description: stock-management

### BEGIN INIT INFO
# Provides: stock-management
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop stock-management
### END INIT INFO
# Author: Dave

# Source function library
. /etc/rc.d/init.d/functions

voyager_dir=${VOYAGER_DIR-/opt/voyager}
appname=${APPNAME-stock-management}
pidfile=${PIDFILE-/var/run/${appname}.pid}
lockfile=${LOCKFILE-/var/lock/subsys/${appname}}
RETVAL=0

start() {
        echo -n $"Starting stock-management: "
        daemon --user="voyager" --pidfile="${pidfile}" "${voyager_dir}/${appname}/bin/${appname} -Dconfig.file=${voyager_dir}/${appname}.conf > ${voyager_dir}/${appname}/${appname}-`date +%Y%m%d%H%M`.log "
        RETVAL=$?
        echo
                [ $RETVAL = 0 ] && touch ${lockfile}
                return $RETVAL
}

stop() {
        echo -n $"Stopping stock-management: "
        kill $(cat ${pidfile})
        REVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

restart() {
    stop
    start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  status)
        rh_status
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $RETVAL

Any help?

Thanks!
 
Old 03-17-2015, 12:33 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 3,997

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Could be PIDFILE="". Unless that's valid, change the test to:

Code:
pidfile=${PIDFILE:-/var/run/${appname}.pid}
 
Old 03-17-2015, 07:52 PM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,072

Rep: Reputation: 832Reputation: 832Reputation: 832Reputation: 832Reputation: 832Reputation: 832Reputation: 832
To start if you are setting default values in bash you need to use :- not just - like the post above, also upper and lower case variable names are not the same so for instance you should use
Code:
PIDFILE=${PIDFILE:-/var/run/${APPNAME}.pid}
Try to stick to a format ie I use all uppercase variable names for global variables and all lowercase names for local variables.
 
Old 03-18-2015, 02:48 AM   #4
RetroSpock
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you both for your replies.

I'm not overly familiar with Bash scripting as I struggle to find any decent learning material.

Everything I know has come from about 6 months of playing around with scripts.

I've gone for a different approach now, which appears to work; I just need to add some sanity checks.

I'll post the final draft once I've added some checks.

Thanks again!

Last edited by RetroSpock; 03-18-2015 at 02:50 AM. Reason: forgot to put 'scripting' after Bash
 
Old 03-18-2015, 03:06 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,413

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
Quote:
Originally Posted by RetroSpock View Post
I'm not overly familiar with Bash scripting as I struggle to find any decent learning material.
http://www.tldp.org/LDP/abs/html/
 
Old 03-18-2015, 04:51 AM   #6
RetroSpock
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks TenTenths! That site occasionally pops up when I'm searching for something specific, I'll give it a read start to finish.

Here is the script I went with which appears to work as I want it to:

Code:
#!/bin/bash
#
# stock-management
#
# chkconfig: 2345 80 30
# description: stock-management

### BEGIN INIT INFO
# Provides: stock-management
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop stock-management
### END INIT INFO
# Author: Dave

# Source function library
. /etc/rc.d/init.d/functions

VOYAGER_DIR=${VOYAGER_DIR:-/opt/voyager}
APP_NAME=${APP_NAME:-stock-management}
VOYAGERD=${VOYAGERD:-${VOYAGER_DIR}/${APP_NAME}/bin/${APP_NAME}}
DCONF=${DCONF:-${VOYAGER_DIR}/${APP_NAME}.conf}
LOG_FILE=${LOG_FILE:-${VOYAGER_DIR}/${APP_NAME}/logs/${APP_NAME}-`date +%Y%m%d%H%M`.log}
PID_FILE=${PID_FILE:-/var/run/${APP_NAME}.pid}
LOCK_FILE=${LOCK_FILE:-/var/lock/subsys/${APP_NAME}}
IS_RUNNING=${IS_RUNNING:-"${APP_NAME} is already running! "}
NOT_RUNNING=${NOT_RUNNING:-"${APP_NAME} is not running! "}
NAME=java
RETVAL=0

start() {
        if [ -s ${PID_FILE} ] ; then
                echo $"${IS_RUNNING}"
                exit 1
        else
                echo -n $"Starting ${APP_NAME}: "
                touch ${PID_FILE}
                su - voyager -c "nohup ${VOYAGERD} -Dconfig.file=${DCONF} > ${LOG_FILE} 2>&1 & echo \$!" > ${PID_FILE}
                RETVAL=$?
                if [ $RETVAL = 0 ] ; then
                        echo "OK!"
                        touch ${LOCK_FILE}
                        return $RETVAL
                fi
        fi
}

stop() {
        if [ ! -s ${PID_FILE} ] ; then
                echo $"${NOT_RUNNING}"
                exit 1
        else
                echo -n $"Stopping ${APP_NAME}: "
                kill $(cat ${PID_FILE})
                #kill $(ps -A |grep $NAME |cut -d " " -f1)
                REVAL=$?
                if [ $RETVAL = 0 ] ; then
                        echo "OK!"
                        rm -f ${LOCK_FILE} ${PID_FILE}
                fi
        fi
}

restart() {
        if [ -s ${PID_FILE} ] ; then
                stop
                start
        elif [ ! -s ${PID_FILE} ] ; then
                echo -n "${NOT_RUNNING} Attempting to start: "
                start
                echo -n "OK!"
        fi
}

status() {
        if [ -s ${PID_FILE} ] ; then
                echo "${APP_NAME} is running!"
                exit 1
        elif [ ! -s ${PID_FILE} ] ; then
                echo "${NOT_RUNNING}"
                exit 1
        fi
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  status)
        status
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit $RETVAL
 
  


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
Creating an init script for uwsgi using a deployment script spadez Programming 1 02-23-2015 01:11 PM
[SOLVED] why init script cannot source init tools unless session root linuxecho Linux - Newbie 8 02-17-2014 05:17 PM
Issues creating partitions in %pre script PeterSteele Linux - Virtualization and Cloud 1 10-14-2012 11:13 PM
Creating links to an init script. slash1981 Linux - Newbie 4 12-20-2009 05:26 PM
Init script in /etc/init.d does not start at boot tdnnash25 Linux - Server 4 12-18-2009 04:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:41 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration