Syntax error on simple init script for debian [duplicate] - bash

This question already has an answer here:
Syntax error of ";; unexpected" on simple init script for Debian
(1 answer)
Closed 4 years ago.
It seems like the colons should be there from the tutorials I am reading, and yet it is telling me to remove them?
/etc/init.d/uwsgi: 27: /etc/init.d/uwsgi: Syntax error: ";;" unexpected
Here is my code:
#!/bin/sh
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: This script manages uWSGI.
### END INIT INFO
DAEMON=/var/www/app/venv/bin/uwsgi
PIDFILE=/var/run/uwsgi.pid
DAEMON_ARGS="--ini /var/www/app/conf/uwsgi/app.ini --pidfile /var/run/uwsgi.pid"
. /lib/init/vars.sh
. /lib/lsb/init-functions
case "$1" in
start)
echo "Starting"
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS 1> /dev/null 2>&1 \
|| return 2
esac
;;
stop)
echo "Stopping"
start-stop-daemon --stop --quiet --retry=QUIT/30/KILL/5 --pidfile $PIDFILE --name uwsgi
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
rm -f $PIDFILE
return "$RETVAL"
esac
;;
status)
status_of_proc "$DAEMON" "uwsgi" && exit 0 || exit $?
;;
reload)
echo "Reloading"
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name uwsgi
return 0
;;
*)
echo "Usage: /etc/init.d/uwsgi {start|stop|status|reload|force-reload}" >&2
exit 3
;;
esac

The proper syntax for the case statement is:
case expression in
pattern1)
statements
;;
pattern2)
statements
;;
esac
i.e. just closed with a single esac at the end. Anything else is just going to cause a syntax error.
You can feed scripts into ShellCheck, which does on-line script checking and will indicate where the syntax error is (which you can highlight in the input that you're pasting in here).

Related

Elasticsearch Service Running but Status "Not Running"

I'm running Elasticsearch 1.4.4 on Ubuntu. When I attempt to start Elasticsearch as a service then check its status via service elasticsearch status, it appears that the service is "not running". However, when I check the cluster and various running processes, Elasticsearch appears to be up and running:
root 13353 36.7 1.9 3241608 315276 pts/1 Sl 10:30 0:09 /usr/bin/java -Xms256m -Xmx1g -Xss256k -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Delasticsearch -Des.pidfile=-Des.config=/elasticsearch/config/elasticsearch.yml -Des.path.home=/elasticsearch -cp :/elasticsearch/lib/elasticsearch.jar:/elasticsearch/lib/*:/elasticsearch/lib/sigar/* -Des.path.home=/elasticsearch -Des.path.logs=/elasticsearch/logs -Des.path.data=/elasticsearch/data -Des.path.work= org.elasticsearch.bootstrap.Elasticsearch
My init.d looks like this:
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/elasticsearch/bin
DESC="Elasticsearch"
NAME=elasticsearch
ES_HOME=/elasticsearch
ES_MIN_MEM=256m
ES_MAX_MEM=2g
DAEMON=$ES_HOME/bin/$NAME
LOG_DIR=$ES_HOME/logs
DATA_DIR=$ES_HOME/data
CONFIG_FILE=$ES_HOME/config/elasticsearch.yml
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
DAEMON_ARGS="-p $PID_FILE -Des.config=$CONFIG_FILE -Des.path.home=$ES_HOME -Des.path.logs=$LOG_DIR -Des.path.data=$DATA_DIR -Des.path.work=$WORK_DIR -d"
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
-- $DAEMON_ARGS \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
}
...
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
#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
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:
What am I doing wrong?

Aria2 working init script

I want that aria2 starts when the system is booting. I found different init.d scripts but non of them is working for me...
Can you tell me whats wrong with that init.d script?
#!/bin/sh
### BEGIN INIT INFO
# Provides: Aria2
# Required-Start: $network $local_fs $remote_fs
# Required-Stop:: $network $local_fs $remote_fs
# Should-Start: $all
# Should-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Aria2 - Download Manager
### END INIT INFO
NAME=aria2c
ARIA2C=/usr/local/bin/$NAME
PIDFILE=/var/run/$NAME.pid
CONF=/etc/aria2/aria2.conf
ARGS="--conf-path=${CONF} --enable-rpc --rpc-listen-all --daemon"
USER="aria2"
test -f $ARIA2C || exit 0
. /lib/lsb/init-functions
case "$1" in
start) log_daemon_msg "Starting aria2c" "aria2c"
start-stop-daemon --start --quiet -b -m --pidfile $PIDFILE --chuid $USER --startas $ARIA2C -- $ARGS
log_end_msg $?
;;
stop) log_daemon_msg "Stopping aria2c" "aria2c"
start-stop-daemon --stop --quiet --pidfile $PIDFILE
log_end_msg $?
;;
restart|reload|force-reload)
log_daemon_msg "Restarting aria2c" "aria2c"
start-stop-daemon --stop --retry 5 --quiet --pidfile $PIDFILE
start-stop-daemon --start --quiet -b -m --pidfile $PIDFILE --chuid $USER --startas $ARIA2C -- $ARGS
log_end_msg $?
;;
status)
status_of_proc -p $PIDFILE $ARIA2C aria2c && exit 0 || exit $?
;;
*) log_action_msg "Usage: /etc/init.d/aria2c {start|stop|restart|reload|force-reload|status}"
exit 2
;;
esac
exit 0
When I start, it says
[ ok ] Starting aria2c: aria2c.
But when i take a look with
/etc/init.d/aria2c status
it says
[FAIL] aria2c is not running ... failed!
Appreciate your help!
Try this:
#!/bin/sh
### BEGIN INIT INFO
# Provides: aria2
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: aria2c init script.
# Description: Starts and stops aria2 daemon.
### END INIT INFO
USER="root"
DAEMON=/usr/bin/aria2c
CONF=/etc/aria2/aria2.conf
start() {
if [ -f $CONF ]; then
logger -st ARIA2C "Starting aria2c daemon..."
nohup start-stop-daemon -S -c $USER -x $DAEMON -- -D --enable-rpc --conf-path=$CONF
else
logger -st ARIA2C "Couldn't start aria2 daemon (no $CONF found)"
fi
}
stop() {
logger -st ARIA2C "Stoping aria2c daemon..."
start-stop-daemon -o -c $USER -K -u $USER -x $DAEMON
}
status() {
dbpid=`pgrep -fu $USER $DAEMON`
if [ -z "$dbpid" ]; then
logger -st ARIA2C "aria2c daemon not running."
else
logger -st ARIA2C "aria2c daemon running..."
fi
}
case "$1" in
start)
start
sleep 1
;;
stop)
stop
sleep 1
;;
restart)
stop
sleep 2
start
;;
status)
status
;;
*)
echo "Usage: {start|stop|restart|status}"
exit 1
esac
exit 0
You must edit your config file in /etc/aria2/aria2.conf
In this file paste this:
daemon=true
enable-rpc=true
rpc-listen-port=6800
rpc-listen-all=true
####### your download folder, ensure that this folder exist! ##########
dir=/tmp
where is your logfile located
log=/var/log/aria2.log
log-level=warn
dht-listen-port=6801
auto-save-interval=30
#seed ratio and seed time in minutes
seed-ratio=1.0
seed-time=1460
max-upload-limit=20K
event-poll=select
With these files (/etc/init.d/aria2d and /etc/aria2/aria2.conf) aria work, when i run manually -> sudo /etc/init.d/aria2d start
Unfortunately aria don't start automatically when i reboot system.

create service from shell script on ubuntu 14

I'm trying to create a service script in /etc/init.d to run my shell script wich is under /home/user !!
here a part of my script :
#! /bin/sh
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/home/user/workspace/eattogether/app-server/app.server.core/target/wisdom
DESC="chameleon service"
NAME=chameleon.sh
DAEMON=/home/user/workspace/eattogether/app-server/app.server.core/target/wisdom/$NAME
DAEMON_ARGS="start"
PIDFILE=/home/user/workspace/eattogether/app-server/app.server.core/target/wisdom/CHAMELEON.pid
SCRIPTNAME=/etc/init.d/spheros
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
do_start()
{
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2
}
do_stop()
{
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
rm -f $PIDFILE
return "$RETVAL"
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
when I tr to execute this script i got an error : cannot access ./bin/*.jar: No such file or directory
here my other script :
# Check if the RUNNING_PID file is not there already
if [ -f RUNNING_PID ]; then
echo "[error] RUNNING_PID existing. Is this chameleon already running?"
exit 1
fi
#CLASSPATH=$(JARS=("bin"/*.jar); IFS=:; echo "${JARS[*]}")
for i in `ls ./bin/*.jar`
do
CLASSPATH=${CLASSPATH}:${i}
done
if test "$1" = "--interactive"; then
"$JAVA" -cp ${CLASSPATH} ${JVM_ARGS} -Dchameleon.home=$dir org.ow2.chameleon.core.Main "$#"
else
"$JAVA" -cp ${CLASSPATH} ${JVM_ARGS} -Dchameleon.home=$dir org.ow2.chameleon.core.Main "$#" &
echo $! > RUNNING_PID
fi
any idea how to resolve this ?? tell the service to use the directory where the script is located ???
thanks jackman for the reply,
I did found a solution for this issue. I decided to use :
start-stop-daemon --chdir /path/to/my/script
this solved my problem.
You could add this to the daemon script:
cd -P "$(dirname "$0")"
But don't do this:
for i in `ls ./bin/*.jar`
do this instead
for i in ./bin/*.jar
and let the shell expand the filenames for you.
You should quote variables like "$this" not like ${this}

mumudvb starting script is not working

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

Syntax error: EOF in backquote substitution

I'm getting an error Syntax error: EOF in backquote substitution and I don't have the faintest idea why. Would anyone mind taking a quick look?
#! /bin/sh
# chkconfig 345 85 60
# description: startup script for produtcrawler-router
# processname: producrawler-router
NAME=productcrawler-router
DIR=/etc/productcrawler/services
EXEC=router.py
PID_FILE=/var/run/productcrawler.pid
IEXE=/etc/init.d/productcrawler-router
RUN_AS=root
### BEGIN INIT INFO
# Provides: productcrawler-router
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 5
# Default-Stop: 0 1 2 3 6
# Description: Starts the productcrawler-router service
### END INIT INFO
if [ ! -f $DIR/$EXEC ]
then
echo "$DIR/$EXEC not found."
exit
fi
case "$1" in
start)
echo -n "Starting $NAME"
cd $LDIR
start-stop-daemon -d $DIR --start --background --pidfile $PID_FILE --make-pidfile --exec $EXEC --quiet
echo "$NAME are now running."
;;
stop)
echo -n "Stopping $NAME"
kill -TERM `cat $PID_FILE
rm $PID_FILE
echo "$NAME."
;;
force-reload|restart)
$0 stop
$0 start
;;
*)
echo "Use: /etc/init.d/$NAME {start|stop|restart|force-reload}"
exit 1
;;
esac
The syntax coloring didn't give it away?
stop)
echo -n "Stopping $NAME"
kill -TERM `cat $PID_FILE
It's the backtick and the end of the kill line.

Resources