EDIT: I am following this example.
Trying to write an archlinux rc.d script for mongod. I put my binaries in /usr/bin. Here is what I got so far:
#!/bin/bash
# import predefined functions
. /etc/rc.conf
. /etc/rc.d/functions
# Point to the binary
DAEMON=/usr/bin/mongod
# Get the ARGS from the conf
. /etc/conf.d/crond
# Function to get the process id
PID=$(get_pid $DAEMON)
case "$1" in
start)
stat_busy "Starting $DAEMON"
# Check the PID exists - and if it does (returns 0) - do no run
[ -z "$PID" ] && $DAEMON $ARGS $> /dev/null
if [ $? = 0 ]; then
add_daemon $DAEMON
stat_done
else
stat_fail
exit 1
fi
;;
stop)
stat_busy "Stopping $DAEMON"
kill -HUP $PID &>/dev/null
rm_daemon $DAEMON
stat_done
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac
The problem is that when I do sudo rc.d start mongod, I get the following error:
:: Starting /usr/bin/mongod
[BUSY] /etc/rc.d/functions: line 203: /run/daemons//usr/bin/mongod: No such file or directory
[DONE]
Syntax error.
I used $> instead of &> on the line:
[ -z "$PID" ] && $DAEMON $ARGS $> /dev/null
Related
I have install elasticsearch 2.3.3 in centos 7 but after closing terminal elasticsearch plugin head automatically close but I want keep running in background. please give me helpful answer.
You can run it in background as two ways,
1. Nohup
2. Creating service script and put it in init.d folder
Nohup
Eg: nohup ./bin/elasticsearch
Service script
Use the following script,
#!/bin/bash
### BEGIN INIT INFO
# Provides: Elasticsearch
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Runs elasticsearch daemon
# Description: Runs the elasticsearch daemon as a non-root user
### END INIT INFO
# Process name
NAME=elasticsearch
DESC="Elasticsearch"
PROG="/etc/init.d/elasticsearch"
# Configure location of Elasticsearch bin
ELASTICSEARCH_BIN=/opt/elasticsearch-2.3.0/bin
# PID Info
PID_FOLDER=/var/run/elasticsearch/
PID_FILE=/var/run/elasticsearch/$NAME.pid
LOCK_FILE=/var/lock/subsys/$NAME
PATH=/bin:/usr/bin:/sbin:/usr/sbin:$ELASTICSEARCH_BIN
DAEMON=$ELASTICSEARCH_BIN/$NAME
# Configure logging location
ELASTICSEARCH_LOG=/var/log/elasticsearch.log
# Begin Script
RETVAL=0
if [ `id -u` -ne 0 ]; then
echo "You need root privileges to run this script"
exit 1
fi
# Function library
. /etc/init.d/functions
start() {
echo -n "Starting $DESC : "
pid=`pidofproc -p $PID_FILE elasticsearch`
if [ -n "$pid" ] ; then
echo "Already running."
exit 0
else
# Start Daemon
if [ ! -d "$PID_FOLDER" ] ; then
mkdir $PID_FOLDER
fi
daemon --user=$DAEMON_USER --pidfile=$PID_FILE $DAEMON 1>"$ELASTICSEARCH_LOG" 2>&1 &
sleep 2
pidofproc node > $PID_FILE
RETVAL=$?
[[ $? -eq 0 ]] && success || failure
echo
[ $RETVAL = 0 ] && touch $LOCK_FILE
return $RETVAL
fi
}
reload()
{
echo "Reload command is not implemented for this service."
return $RETVAL
}
stop() {
echo -n "Stopping $DESC : "
killproc -p $PID_FILE $DAEMON
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $PID_FILE $LOCK_FILE
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $PID_FILE $DAEMON
RETVAL=$?
;;
restart)
stop
start
;;
reload)
reload
;;
*)
# Invalid Arguments, print the following message.
echo "Usage: $0 {start|stop|status|restart}" >&2
exit 2
;;
esac
sudo chmod +x /etc/init.d/elasticsearch
sudo update-rc.d elasticsearch defaults 96 9
sudo /etc/init.d/elasticsearch restart
I have installed mumudvb manually based on instructions in here
https://github.com/braice/MuMuDVB
to get the starting scripts working i executed these commands
cp scripts/debian/etc/default/mumudvb /etc/default/mumudvb
cp scripts/debian/etc/init.d/mumudvb /etc/init.d/mumudvb
/etc/default/mumudvb :
#Mumudvb init config file
#
# This file is used to specify the locations of mumudvb config files for each card
#
#If you don't want to automatically start mumudvb, uncomment this line
#DONTSTARTMUMU=true
#If you want to launch a command before mumudvb (for example for automatic configuration generation)
#LAUNCH_BEFORE_MUMU=""
#Options for mumudvb
DAEMON_OPTS=""
#The user to launch mumudvb
DAEMONUSER="username"
#Change this line to reflect your configuration
#Ex : ADAPTERS="0 1 2 4"
ADAPTERS="0"
#Location of the config files
#Ex : MUMUDVB_CONF_1="/etc/mumudvb/card1.conf"
MUMUDVB_CONF_0="/etc/mumudvb/card0.conf"
this is the init.d/mumudvb script :
#!/bin/sh
### BEGIN INIT INFO
# Provides: mumudvb
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mumudvb
# Description: Digital television streaming program
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/mumudvb
PIDDIR=/var/run/mumudvb
DEFAULT_FILE=/etc/default/mumudvb
NAME=mumudvb
DESC="television streaming program"
#Reading of the config file
if [ -f "$DEFAULT_FILE" ] ; then
. "$DEFAULT_FILE"
fi
if [ "$DONTSTARTMUMU" = "true" ]; then exit 0; fi
. /lib/lsb/init-functions
test -x $DAEMON || exit 5
set -e
do_start() {
if [ ! -d $PIDDIR ]; then
mkdir -p $PIDDIR
fi
chown $DAEMONUSER $PIDDIR
if [ -x "$LAUNCH_BEFORE_MUMU" ]; then
log_daemon_msg "Launching pre script ..."
eval $LAUNCH_BEFORE_MUMU
log_daemon_msg "Done."
fi
for ADAPTER in $ADAPTERS; do
#Todo : fails if all card fails
log_daemon_msg " Starting card $ADAPTER"
eval CONFIG_FILE = MUMUDVB_CONF_0
if [ ! -f $CONFIG_FILE ]; then
log_warning_msg " Card $ADAPTER: Config file $CONFIG_FILE not found."
else
start-stop-daemon --start --oknodo --name mumudvb_$ADAPTER\
--make-pidfile --pidfile=$PIDDIR/mumudvb_init_$ADTAPTER.pid\
--chuid $DAEMONUSER --exec $DAEMON -- $DAEMON_OPTS --card $ADAPTER -c $CONFIG_FILE
fi
done
}
do_stop() {
for PIDFILE in `ls $PIDDIR/mumudvb_init_*.pid 2> /dev/null`; do
start-stop-daemon --stop --oknodo --pidfile "$PIDFILE" \
--exec $DAEMON
done
}
case "$1" in
start)
if [ ! -f "$DEFAULT_FILE" ]; then
log_failure_msg "$DEFAULT_FILE not found, Can't start $NAME"
exit 6
fi
log_daemon_msg "Starting $DESC: $NAME"
do_start
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping $DESC: $NAME"
do_stop
log_end_msg $?
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC: $NAME"
do_stop
sleep 1
do_start
log_end_msg $?
;;
status)
status_of_proc "$DAEMON" "$DESC: $NAME" && exit 0 || exit $?
;;
*)
log_success_msg "Usage: $0 {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0
when i run :
sudo service mumudvb status
or
sudo service mumudvb start
nothing returns ...
shouldn't it at least return an error or something ?
**note: runing
mumudvb -c /etc/mumudvb/card0.conf
works fine**
in the file /etc/default/mumudvb set DONTSTARTMUMU to false
DONTSTARTMUMU=false
I'm installing Tomcat6 and using the following for /etc/init.d/tomcat6:
#!/bin/bash
# description: Tomcat6 service
# processname: java
# chkconfig: - 99 1
## Note: CATALINA_HOME and CATALINA_PID are set elsewhere.##
# Source function library.
. /etc/init.d/functions
# Source sysconfig for tomcat6
if [ -f /etc/sysconfig/tomcat6 ]; then
. /etc/sysconfig/tomcat6
fi
[ -d "$CATALINA_HOME" ] || { echo "Tomcat requires $CATALINA_HOME."; exit 1; }
case $1 in
start|stop|run)
if su $TOMCAT_USER bash -c "cd $CATALINA_HOME/logs; $CATALINA_HOME/bin/catalina.sh $1"; then
echo -n "Tomcat $1 successful"
[ $1 == "stop" ] && rm -f $CATALINA_PID
else
echo -n "Error in Tomcat $1: $?"
fi
;;
restart)
$0 start
$0 stop
;;
status)
if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
if ps --pid $kpid 2>&1 1>/dev/null; then
echo "$0 is already running at ${kpid}"
else
echo "$CATALINA_PID found, but $kpid is not running"
fi
unset kpid
else
echo "$0 is stopped"
fi
;;
esac
exit 0
The problem, as noted in this related ticket, is that Chef checks the "status" of a service and will not start it if the "status" command returns an exit code of "0". Which it always does because the script itself completes successfully, regardless of whether the service is running or not.
I need to adapt my init script to return an exit code of 3 if the service is not running, per the guidelines for Init scripts posted here:
0 program is running or service is OK
1 program is dead and /var/run pid file exists
2 program is dead and /var/lock lock file exists
3 program is not running
4 program or service status is unknown
5-99 reserved for future LSB use
100-149 reserved for distribution use
150-199 reserved for application use
200-254 reserved
I modified my initial script to:
#!/bin/bash
# description: Tomcat6 service
# processname: java
# chkconfig: - 99 1
# Source function library.
. /etc/init.d/functions
# Source sysconfig for tomcat6
if [ -f /etc/sysconfig/tomcat6 ]; then
. /etc/sysconfig/tomcat6
fi
[ -d "$CATALINA_HOME" ] || { echo "Tomcat requires $CATALINA_HOME."; exit 1; }
exit_var=0
case $1 in
start|stop|run)
if su $TOMCAT_USER bash -c "cd $CATALINA_HOME/logs; $CATALINA_HOME/bin/catalina.sh $1"; then
echo -n "Tomcat $1 successful"
[ $1 == "stop" ] && rm -f $CATALINA_PID
else
echo -n "Error in Tomcat $1: $?"
exit_var=1
fi
;;
restart)
$0 start
$0 stop
;;
status)
if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
if ps --pid $kpid 2>&1 1>/dev/null; then
echo "$0 is already running at ${kpid}"
exit_var=0
else
echo "$CATALINA_PID found, but $kpid is not running"
exit_var=4
fi
unset kpid
else
echo "$0 is stopped"
exit_var=3 # Fixes issue with Chef not starting a stopped service.
fi
;;
esac
exit $exit_var
But those aren't ACTUALLY changing the exit codes for the script. How can I set different exit codes for different case scenarios?
Version Info:
OS: CentOS 6.5
Chef: 10.20
Tomcat: 6.0.39
You have the right idea, but you have exit_var=3 in the wrong place. I have placed it below to equal 3 for the status when it is already running:
status)
if [ -f "$CATALINA_PID" ]; then
read kpid < "$CATALINA_PID"
if ps --pid $kpid 2>&1 1>/dev/null; then
echo "$0 is already running at ${kpid}"
## Fixes issue with Chef not starting a stopped service.
exit_var=3 ## this is the condition of already running
else
echo "$CATALINA_PID found, but $kpid is not running"
exit_var=4
fi
unset kpid
else
echo "$0 is stopped"
exit_var=5 # (renumbered 5 set as you desire)
fi
;;
esac
exit $exit_var
supervise is one of the powerful tool in daemontools, I wonder how to implement it using bash script. Anyone has suggestions? I need help!
supervise performs a number of tasks, and interoperates with svscan, svcok, svstat.
A fully-featured implementation in bash would be non-trivial, but a daemon-restarting script is a fairly straight-forward task.
#!/bin/bash
DAEMON=/usr/sbin/whatever # "/bin/sleep" for demo
DAEMON_ARGS="xxx" # "15" for sleep demo
case "$1" in
start)
echo "Starting $DAEMON"
(
trap 'logger -i -p daemon.info "INFO: $DAEMON shutting down..."; exit 1' 1 2 3 15
logger -i -p daemon.info "INFO: Starting $DAEMON"
while : ; do
$DAEMON $DAEMON_ARGS &
pid=$!
echo $pid > /var/run/mydaemon.pid
wait $pid
rc=$?
logger -i -p daemon.warn "WARNING: $DAEMON exited, rc=$rc"
sleep 1 # adjust as required
logger -i -p daemon.warn "WARNING: Restarting $DAEMON"
done
) &
echo $! > /var/run/myscript.pid
;;
stop)
echo "Stopping $DAEMON"
[ -f /var/run/myscript.pid ] && kill $(</var/run/myscript.pid)
[ -f /var/run/mydaemon.pid ] && kill $(</var/run/mydaemon.pid)
;;
esac
The above has pretty much no error handling, doesn't properly do real daemon things like chdir() and close unused FDs, but it does log via logger/syslog so you can see what it's doing. It assumes $DAEMON does not fork into the background itself (as does supervise).
You haven't stated your platform, but if you want something really, really simple, inittab may do the trick, see how to use inittab to auto-restart a PHP programme? for some tips. Otherwise we're veering out of Stack Overflow territory, so check out https://unix.stackexchange.com/ .
I changed the code provided by mr.spuratic around a little bit:
echo "true" > $STATUSFILE
case "$1" in
start)
echo "Starting $DAEMON_NAME"
(
trap 'logger -t italoService "INFO: $DAEMON_NAME shutting down..."; exit 1' 1 2 3 15
logger -t italoService "INFO: Starting $DAEMON_NAME"
while : ; do
#do_start
read STATUS <$STATUSFILE
if [ "$STATUS" = "false" ] ; then
logger -t italoService "INFO: $DAEMON_NAME stopped by user"
break
fi
$DAEMON $DAEMON_OPTS &
pid=$!
echo $pid > $PIDFILE
logger -t italoService "WARNING: pid File: $pid"
wait $pid
rc=$?
logger -t italoService "WARNING: $DAEMON_NAME exited, rc=$rc"
sleep 1 # adjust as required
read STATUS <$STATUSFILE
if [ "$STATUS" = "true" ] ; then
logger -t italoService "WARNING: Restarting $DAEMON_NAME"
fi
done
) &
echo $! > $PIDFILE
;;
stop)
echo "Stopping $DAEMON_NAME"
echo "false" > $STATUSFILE
[ -f $PIDFILE ] && kill $(<$PIDFILE)
[ -f $PIDFILE ] && kill $(<$PIDFILE)
;;
I found this small shell script in a book... NGiNX works it just this script that just does not work. Because every time I do /etc/init.d/nginx start (that is where the file is) it sends me this message: Usage: /etc/init.d/nginx {start|stop|restart|reload}
For testing purposes I added a echo "$1" and it sends me -e when I do: /etc/init.d/nginx start Or anything else...
I am using Ubuntu 12.04.
#! /bin/sh
# Author: Ryan Norbauer http://norbauerinc.com
# Modified: Geoffrey Grosenbach http://topfunky.com
# Modified: Clement NEDELCU
# Reproduced with express authorization from its contributors
set –e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0
echo "0"
d_start() {
$DAEMON || echo -n " already running"
}
d_stop() {
$DAEMON –s quit || echo -n " not running"
}
d_reload() {
$DAEMON –s reload || echo -n " could not reload"
}
echo "$1"
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
# Sleep for two seconds before starting again, this should give the
# Nginx daemon some time to perform a graceful stop.
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
set –e is setting the list of arguments to the script to –e. Remove that line and the script will work.
Edit: The script posted contains set –e (that is EN DASH + e) as opposed to set -e (ASCII hyphen/minus sign + e). This caused sh to override the first argument passed with –e instead of setting the -e option in the shell. Replacing – with - should fix the problem.