bash script Syntax error: word unexpected (expecting "do") - bash

I'm trying to run this dropbox script on my nginx server , but im getting:
Syntax error: word unexpected (expecting "do")
I copy pasted the script for a website, and I tried removing special characters, but im still getting the same error.
script:
#!/bin/sh
# /etc/init.d/dropbox
### BEGIN INIT INFO
# Provides: dropbox
# Required-Start: $network $syslog $remote_fs
# Required-Stop: $network $syslog $remote_fs
# Should-Start: $named $time
# Should-Stop: $named $time
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop the dropbox daemon for debian/ubuntu
# Description: Dropbox daemon for linux
### END INIT INFO
DROPBOX_USERS="root"
start() {
echo "Starting dropbox..."
for dbuser in $DROPBOX_USERS; do
start-stop-daemon -b -o -c $dbuser -S -x /home/$dbuser/.dropbox-dist/dropboxd
done
}
stop() {
echo "Stopping dropbox..."
for dbuser in $DROPBOX_USERS; do
start-stop-daemon -o -c $dbuser -K -x /home/$dbuser/.dropbox-dist/dropboxd
done
}
status() {
for dbuser in $DROPBOX_USERS; do
dbpid=`pgrep -u $dbuser dropbox`
if [ -z $dbpid ] ; then
echo "dropboxd for USER $dbuser: not running."
else
echo "dropboxd for USER $dbuser: running."
fi
done
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
stop
start
;;
status)
status
;;
*)
echo "Usage: /etc/init.d/dropbox {start|stop|reload|force-reload|restart|status}"
exit 1
esac
exit 0

Probably you managed to insert windows line endings when you copy and pasted. If you have dos2unix, use it. (dos2unix scriptfile) Otherwise, there are a number of similar utilities.

Why do you have
/home/$dbuser/.dropbox-dist/dropboxd
or replacing variables.
/home/root/.dropbox-dist/dropboxd
Shouldn't it be
/root/.dropbox-dist/dropboxd
eg
/$dbuser/.dropbox-dist/dropboxd
Are you seriously allowing dropboxes in /root ?
Also why a for dbuser in DROPBOX_USERS; do reassign? rather than
dbuser=$DROPBOX_USERS

Related

graceful stop inotifywait pipeline inside a bash script

I am using a docker to watch and sync data in a folder with inotify and aws-cli but when I try to kill the docker with SIGTERM it exit with code 143 but I want to get a zero exit code. And if i kill the inotify process inside the docker it do return a zero code.
So how can I kill the entrypoint.sh with TERM signal and return a 0 code?
The docker is here. I put the bash script below:
#!/usr/bin/env bash
# S3Sync Entry Point
# Bash strict mode
set -euo pipefail
IFS=$'\n\t'
# VARs
S3PATH=${S3PATH:-}
SYNCDIR="${SYNCDIR:-/sync}"
CRON_TIME="${CRON_TIME:-10 * * * *}"
INITIAL_DOWNLOAD="${INITIAL_DOWNLOAD:-true}"
# Log message
log(){
echo "[$(date "+%Y-%m-%dT%H:%M:%S%z") - $(hostname)] ${*}"
}
# Sync files
sync_files(){
local src="${1:-}"
local dst="${2:-}"
mkdir -p "$dst" # Make sure directory exists
log "Sync '${src}' to '${dst}'"
if ! aws s3 sync --no-progress --delete --exact-timestamps "$src" "$dst"; then
log "Could not sync '${src}' to '${dst}'" >&2; exit 1
fi
}
# Download files
download_files(){
sync_files "$S3PATH" "$SYNCDIR"
}
# Upload files
upload_files(){
sync_files "$SYNCDIR" "$S3PATH"
}
# Run initial download
initial_download(){
if [[ "$INITIAL_DOWNLOAD" == 'true' ]]; then
if [[ -d "$SYNCDIR" ]]; then
# directory exists
if [[ $(ls -A "$SYNCDIR" 2>/dev/null) ]]; then
# directory is not empty
log "${SYNCDIR} is not empty; skipping initial download"
else
# directory is empty
download_files
fi
else
# directory does not exist
download_files
fi
elif [[ "$INITIAL_DOWNLOAD" == 'force' ]]; then
download_files
fi
}
# Watch directory using inotify
watch_directory(){
initial_download # Run initial download
log "Watching directory '${SYNCDIR}' for changes"
inotifywait \
--event create \
--event delete \
--event modify \
--event move \
--format "%e %w%f" \
--monitor \
--quiet \
--recursive \
"$SYNCDIR" |
while read -r changed
do
log "$changed"
upload_files
done
}
# Install cron job
run_cron(){
local action="${1:-upload}"
# Run initial download
initial_download
log "Setup the cron job (${CRON_TIME})"
echo "${CRON_TIME} /entrypoint.sh ${action}" > /etc/crontabs/root
exec crond -f -l 6
}
# Main function
main(){
if [[ ! "$S3PATH" =~ s3:// ]]; then
log 'No S3PATH specified' >&2; exit 1
fi
mkdir -p "$SYNCDIR" # Make sure directory exists
# Parse command line arguments
cmd="${1:-download}"
case "$cmd" in
download)
download_files
;;
upload)
upload_files
;;
sync)
watch_directory
;;
periodic_upload)
run_cron upload
;;
periodic_download)
run_cron download
;;
*)
log "Unknown command: ${cmd}"; exit 1
;;
esac
}
main "$#"
Trying trap like this but failed:
trap "exit" INT TERM
trap "kill 0" EXIT
Answered by the contributor of the docker image.
https://github.com/vladgh/docker_base_images/issues/62
This image uses Tini, which does not make any assumptions about the meaning of the signal it receives and simply forwards it to its child.
In order for your traps to work you need to add the -g flag to Tini in the Dockerfile (krallin/tini#process-group-killing):
ENTRYPOINT ["/sbin/tini", "-g", "--", "/entrypoint.sh"]
An only then you can set a trap at the top of the entrypoint.sh:
trap "exit 0" INT TERM EXIT

Change max-job size of beanstalkd

Does somebody how to change the max-job size of beanstalkd ?
I have the problem that I get the message JOB_TOO_BIG and at Adding Job to beanstalkd, it says that the default size is 65k.
Does somebody know how to change that ?
EDIT:
my beanstalkd init-script in the folder /etc/init.d looks like the following (I added the -z option to increase the job size):
#!/bin/sh
#
# Copyright (c) 2007 Javier Fernandez-Sanguino <jfs#debian.org>
#
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2,
# or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License with
# the Debian operating system, in /usr/share/common-licenses/GPL; if
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
#
### BEGIN INIT INFO
# Provides: beanstalkd
# Required-Start: $remote_fs $network $local_fs
# Required-Stop: $remote_fs $network $local_fs
# Should-Start: $named
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: in-memory workqueue service
# Description: beanstalk is a simple, fast, queueing server. Its
# interface is generic, but was originally designed
# for reducing the latency of page views in high-volume
# web applications by running time-consuming tasks
# asynchronously.
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/beanstalkd # Introduce the server's location here
NAME=beanstalkd # Introduce the short server's name here
DESC="in-memory queueing server" # Introduce a short description here
LOGDIR=/var/log/beanstalkd # Log directory to use
BEANSTALKD_LISTEN_ADDR=0.0.0.0
BEANSTALKD_LISTEN_PORT=11300
PIDFILE=/var/run/$NAME.pid
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
# Default options, these can be overriden by the information
# at /etc/default/$NAME
DAEMON_OPTS="-l $BEANSTALKD_LISTEN_ADDR -p $BEANSTALKD_LISTEN_PORT -z 550000000000" # Additional options given to the server
#DIETIME=10 # Time to wait for the server to die, in seconds
# If this value is set too low you might not
# let some servers to die gracefully and
# 'restart' will not work
STARTTIME=1 # Time to wait for the server to start, in seconds
# If this value is set each time the server is
# started (on start or restart) the script will
# stall to try to determine if it is running
# If it is not set and the server takes time
# to setup a pid file the log message might
# be a false positive (says it did not start
# when it actually did)
#LOGFILE=$LOGDIR/$NAME.log # Server logfile
DAEMONUSER=root #beanstalkd # Users to run the daemons as. If this value
# is set start-stop-daemon will chuid the server
# Include defaults if available
if [ -f /etc/default/$NAME ] ; then
. /etc/default/$NAME
fi
# Check that the user exists (if we set a user)
# Does the user exist?
if [ -n "$DAEMONUSER" ] ; then
if getent passwd | grep -q "^$DAEMONUSER:"; then
# Obtain the uid and gid
DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
else
log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist."
exit 0
fi
fi
set -e
running_pid() {
# Check if a given process pid's cmdline matches a given name
pid=$1
name=$2
[ -z "$pid" ] && return 1
[ ! -d /proc/$pid ] && return 1
cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
# Is this the expected server
[ "$cmd" != "$name" ] && return 1
return 0
}
running() {
# Check if the process is running looking at /proc
# (works for all users)
# No pidfile, probably no daemon present
[ ! -f "$PIDFILE" ] && return 1
pid=`cat $PIDFILE`
running_pid $pid $DAEMON || return 1
return 0
}
start_server() {
# Start the process using the wrapper
if [ "x$START" != "xyes" -a "x$START" != "xtrue" ]; then
echo ""
echo "beanstalkd not configured to start, please edit /etc/default/beanstalkd to enable"
exit 0
fi
if [ -z "$DAEMONUSER" ] ; then
start_daemon -p $PIDFILE $DAEMON $DAEMON_OPTS
errcode=$?
else
# if we are using a daemonuser then change the user id
start-stop-daemon --start --quiet --pidfile $PIDFILE \
--chuid $DAEMONUSER --make-pidfile --oknodo \
--background --exec $DAEMON -- $DAEMON_OPTS
errcode=$?
fi
return $errcode
}
stop_server() {
# Stop the process using the wrapper
if [ -z "$DAEMONUSER" ] ; then
killproc -p $PIDFILE $DAEMON
errcode=$?
else
# if we are using a daemonuser then look for process that match
start-stop-daemon --stop --quiet --pidfile $PIDFILE \
--user $DAEMONUSER \
--exec $DAEMON
errcode=$?
fi
rm -f $PIDFILE
return $errcode
}
reload_server() {
[ ! -f "$PIDFILE" ] && return 1
pid=pidofproc $PIDFILE # This is the daemon's pid
# Send a SIGHUP
kill -1 $pid
return $?
}
force_stop() {
# Force the process to die killing it manually
[ ! -e "$PIDFILE" ] && return
if running ; then
kill -15 $pid
# Is it really dead?
sleep "$DIETIME"s
if running ; then
kill -9 $pid
sleep "$DIETIME"s
if running ; then
echo "Cannot kill $NAME (pid=$pid)!"
exit 0
fi
fi
fi
rm -f $PIDFILE
}
case "$1" in
start)
log_daemon_msg "Starting $DESC " "$NAME"
# Check if it's running first
if running ; then
log_progress_msg "apparently already running"
log_end_msg 0
exit 0
fi
if start_server ; then
# NOTE: Some servers might die some time after they start,
# this code will detect this issue if STARTTIME is set
# to a reasonable value
[ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
if running ; then
# It's ok, the server started and is running
log_end_msg 0
else
# It is not running after we did start
log_end_msg 1
fi
else
# Either we could not start it
log_end_msg 1
fi
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
if running ; then
# Only stop the server if we see it running
errcode=0
stop_server || errcode=$?
log_end_msg $errcode
else
# If it's not running don't do anything
log_progress_msg "apparently not running"
log_end_msg 0
exit 0
fi
;;
force-stop)
# First try to stop gracefully the program
$0 stop
if running; then
# If it's still running try to kill it more forcefully
log_daemon_msg "Stopping (force) $DESC" "$NAME"
errcode=0
force_stop || errcode=$?
log_end_msg $errcode
fi
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
errcode=0
stop_server || errcode=$?
# Wait some sensible amount, some server need this
[ -n "$DIETIME" ] && sleep $DIETIME
start_server || errcode=$?
[ -n "$STARTTIME" ] && sleep $STARTTIME
running || errcode=$?
log_end_msg $errcode
;;
status)
log_daemon_msg "Checking status of $DESC" "$NAME"
if running ; then
log_progress_msg "running"
log_end_msg 0
else
log_progress_msg "apparently not running ... "
log_end_msg 1
exit 0
fi
;;
reload)
log_warning_msg "Reloading $NAME daemon: not implemented (use restart)."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0
And my beanstalkd.conf file which is in the folder /etc/default looks like the following:
## Defaults for the beanstalkd init script, /etc/init.d/beanstalkd on
## Debian systems. Append "-b /var/lib/beanstalkd" for persistent
## storage.
BEANSTALKD_LISTEN_ADDR=0.0.0.0
BEANSTALKD_LISTEN_PORT=11300
# create the journal path before use !!!
BEANSTALKD_JOURNAL_PATH="/var/lib/beanstalkd"
DAEMON_OPTS="-l $BEANSTALKD_LISTEN_ADDR -p $BEANSTALKD_LISTEN_PORT -z 550000000000 -b $BEANSTALKD_JOURNAL_PATH -V"
## Uncomment to enable startup during boot.
START=yes
best regards,
The answer by slickorange in the following link might help you:
JOB_TOO_BIG Pheanstalk - what can be done?
The size of beanstalkd job can be increased by adding the following line (or uncomment the existing BEANSTALKD_EXTRA line and edit it):
BEANSTALKD_EXTRA="-z 524280"
The size is specified in bytes, default size being 65535 bytes.
Restart beanstalkd after making the change:
sudo service beanstalkd restart
On Debian 8.7 x64 editing the settings in /etc/default/beanstalkd works. A bug probably.
I have systemctl in Debian 8 and my config looks slightly different from what is suggested here in other answers to increase the limit. This is what I did:
Edit the beanstalkd file in:
nano /etc/sysconfig/beanstalkd
Then increase the limit for MAX_JOB_SIZE from default 65535 to 524280
MAX_JOB_SIZE=-z 524280
Restart beanstalkd and check the status:
service beanstalkd restart
systemctl status beanstalkd
There are settings for the daemon
-b DIR wal directory
-f MS fsync at most once every MS milliseconds (use -f0 for "always fsync")
-F never fsync (default)
-l ADDR listen on address (default is 0.0.0.0)
-p PORT listen on port (default is 11300)
-u USER become user and group
-z BYTES set the maximum job size in bytes (default is 65535)
-s BYTES set the size of each wal file (default is 10485760)
(will be rounded up to a multiple of 512 bytes)
-c compact the binlog (default)
-n do not compact the binlog
-v show version information
-V increase verbosity
-h show this help
So based on your Linux, you should find out where this is kept and change it. Usually it's under beanstalkd.conf

Startup Script to Run a Jar

I have a fat jar file that that I want to control by running it as a service. I'm not a shell script expert, but took some inspiration from this blog post:
http://vertx.io/blog/vert-x-3-init-d-script/
I then modified it a bit to my needs as below:
#!/bin/bash
###
# chkconfig: 345 20 80
# description: Vert.x application service script
# processname: java
#
# Installation (CentOS):
# copy file to /etc/init.d
# chmod +x /etc/init.d/my-vertx-application
# chkconfig --add /etc/init.d/my-vertx-application
# chkconfig my-vertx-application on
#
# Installation (Ubuntu):
# copy file to /etc/init.d
# chmod +x /etc/init.d/my-vertx-application
# update-rc.d my-vertx-application defaults
#
#
# Usage: (as root)
# service my-vertx-application start
# service my-vertx-application stop
# service my-vertx-application status
#
###
# The directory in which your application is installed
APPLICATION_DIR="/Users/joe/Desktop/temp/tsdb-kafka-consumer"
# The fat jar containing your application
APPLICATION_JAR="tsdb-kafka-consumer-0.1.0-SNAPAHOT.jar"
# The application argument such as -Dfoo=bar ...
APPLICATION_ARGS="-Dlogback.configurationFile=prod-logger.xml -Dconfig.resource=application.integration.conf"
# The Java command to use to launch the application (must be java 8+)
#JAVA=/opt/java/java/bin/java
# ***********************************************
LOG_FILE="${APPLICATION_DIR}"
RUNNING_PID="${APPLICATION_DIR}"/RUNNING_PID
# ***********************************************
# colors
red='\e[0;31m'
green='\e[0;32m'
yellow='\e[0;33m'
reset='\e[0m'
echoRed() { echo -e "${red}$1${reset}"; }
echoGreen() { echo -e "${green}$1${reset}"; }
echoYellow() { echo -e "${yellow}$1${reset}"; }
# Check whether the application is running.
# The check is pretty simple: open a running pid file and check that the process
# is alive.
isrunning() {
# Check for running app
if [ -f "$RUNNING_PID" ]; then
proc=$(cat $RUNNING_PID);
if /bin/ps --pid $proc 1>&2 >/dev/null;
then
return 0
fi
fi
return 1
}
start() {
if isrunning; then
echoYellow "The Vert.x application is already running"
return 0
fi
pushd $APPLICATION_DIR > /dev/null
nohup java $APPLICATION_ARGS -jar $APPLICATION_DIR/$APPLICATION_JAR > $LOG_FILE 2>&1 &
echo $! > ${RUNNING_PID}
popd > /dev/null
if isrunning; then
echoGreen "Kafka Consumer Application started"
exit 0
else
echoRed "Kafka Consumer Application has not started - check log"
exit 3
fi
}
restart() {
echo "Restarting Kafka Consumer Application"
stop
start
}
stop() {
echoYellow "Stopping Kafka Consumer Application"
if isrunning; then
kill `cat $RUNNING_PID`
rm $RUNNING_PID
fi
}
status() {
if isrunning; then
echoGreen "Kafka Consumer Application is running"
else
echoRed "Kafka Consumer Application is either stopped or inaccessible"
fi
}
case "$1" in
start)
start
;;
status)
status
exit 0
;;
stop)
if isrunning; then
stop
exit 0
else
echoRed "Application not running"
exit 3
fi
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {status|start|stop|restart}"
exit 1
esac
When I tried running this script, I got the following error:
Joes-MacBook-Pro:script joe$ sh ./run.sh start
/bin/ps: illegal option -- -
usage: ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]]
[-g grp[,grp...]] [-u [uid,uid...]]
[-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]
ps [-L]
./run.sh: line 73: /Users/joe/Desktop/temp/tsdb-kafka-consumer: Is a directory
/bin/ps: illegal option -- -
usage: ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]]
[-g grp[,grp...]] [-u [uid,uid...]]
[-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]
ps [-L]
-e \e[0;31mKafka Consumer Application has not started - check log\e[0m
What is here the problem? Any suggestions?
The --pid parameter is invalid on your OSX. You should change ps --pid on isrunning() to ps -p. This will work on Linux and OSX.

Laravel 5 running queue

I have a setup where the email sending service is queued to redis driver on my laravel application.
But I need to run the following code on my local host php artisan queue:work --daemon where the queue will be executed.
How can I run the daemon once I pushed my code into the server? I am currently using AWS Elastic Beanstalk
Thanks!!
Thanks #davidlee for posting comment of this question... :)
Finally I found a solution for running queue in elasticbeanstalk amazon. I'm using supervisord. I put the file on my laravel root as supervise.sh. Content of supervise.sh is like this :
#!/bin/bash
#
#
# Author: Günter Grodotzki (gunter#grodotzki.co.za)
# Version: 2015-04-25
#
# install supervisord
#
# See:
# - https://github.com/Supervisor/initscripts
# - http://supervisord.org/
if [ "${SUPERVISE}" == "enable" ]; then
export HOME="/root"
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin"
easy_install supervisor
cat <<'EOB' > /etc/init.d/supervisord
#!/bin/bash
#
# supervisord Startup script for the Supervisor process control system
#
# Author: Mike McGrath <mmcgrath#redhat.com> (based off yumupdatesd)
# Jason Koppe <jkoppe#indeed.com> adjusted to read sysconfig,
# use supervisord tools to start/stop, conditionally wait
# for child processes to shutdown, and startup later
# Erwan Queffelec <erwan.queffelec#gmail.com>
# make script LSB-compliant
#
# chkconfig: 345 83 04
# description: Supervisor is a client/server system that allows \
# its users to monitor and control a number of processes on \
# UNIX-like operating systems.
# processname: supervisord
# config: /etc/supervisord.conf
# config: /etc/sysconfig/supervisord
# pidfile: /var/run/supervisord.pid
#
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $all
# Required-Stop: $all
# Short-Description: start and stop Supervisor process control system
# Description: Supervisor is a client/server system that allows
# its users to monitor and control a number of processes on
# UNIX-like operating systems.
### END INIT INFO
# Source function library
. /etc/rc.d/init.d/functions
# Source system settings
if [ -f /etc/sysconfig/supervisord ]; then
. /etc/sysconfig/supervisord
fi
# Path to the supervisorctl script, server binary,
# and short-form for messages.
supervisorctl=${SUPERVISORCTL-/usr/bin/supervisorctl}
supervisord=${SUPERVISORD-/usr/bin/supervisord}
prog=supervisord
pidfile=${PIDFILE-/var/run/supervisord.pid}
lockfile=${LOCKFILE-/var/lock/subsys/supervisord}
STOP_TIMEOUT=${STOP_TIMEOUT-60}
OPTIONS="${OPTIONS--c /etc/supervisord.conf}"
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon --pidfile=${pidfile} $supervisord $OPTIONS
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
touch ${lockfile}
$supervisorctl $OPTIONS status
fi
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $supervisord
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -rf ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
LSB=1 killproc -p $pidfile $supervisord -HUP
RETVAL=$?
echo
if [ $RETVAL -eq 7 ]; then
failure $"$prog reload"
else
$supervisorctl $OPTIONS status
fi
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $supervisord
RETVAL=$?
[ $RETVAL -eq 0 ] && $supervisorctl $OPTIONS status
;;
restart)
restart
;;
condrestart|try-restart)
if status -p ${pidfile} $supervisord >&/dev/null; then
stop
start
fi
;;
force-reload|reload)
reload
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload}"
RETVAL=2
esac
exit $RETVAL
EOB
chmod +x /etc/init.d/supervisord
cat <<'EOB' > /etc/sysconfig/supervisord
# Configuration file for the supervisord service
#
# Author: Jason Koppe <jkoppe#indeed.com>
# orginal work
# Erwan Queffelec <erwan.queffelec#gmail.com>
# adjusted to new LSB-compliant init script
# make sure elasticbeanstalk PARAMS are being passed through to supervisord
. /opt/elasticbeanstalk/support/envvars
# WARNING: change these wisely! for instance, adding -d, --nodaemon
# here will lead to a very undesirable (blocking) behavior
#OPTIONS="-c /etc/supervisord.conf"
PIDFILE=/var/run/supervisord/supervisord.pid
#LOCKFILE=/var/lock/subsys/supervisord.pid
# Path to the supervisord binary
SUPERVISORD=/usr/local/bin/supervisord
# Path to the supervisorctl binary
SUPERVISORCTL=/usr/local/bin/supervisorctl
# How long should we wait before forcefully killing the supervisord process ?
#STOP_TIMEOUT=60
# Remove this if you manage number of open files in some other fashion
#ulimit -n 96000
EOB
mkdir -p /var/run/supervisord/
chown webapp: /var/run/supervisord/
cat <<'EOB' > /etc/supervisord.conf
[unix_http_server]
file=/tmp/supervisor.sock
chmod=0777
[supervisord]
logfile=/var/app/support/logs/supervisord.log
logfile_maxbytes=0
logfile_backups=0
loglevel=warn
pidfile=/var/run/supervisord/supervisord.pid
nodaemon=false
nocleanup=true
user=webapp
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
[program:laravel_queue]
command=php artisan queue:listen
directory=/var/www/html
stdout_logfile=/var/www/html/storage/logs/laravel-queue.log
logfile_maxbytes=0
logfile_backups=0
redirect_stderr=true
autostart=true
autorestart=true
startretries=86400
EOB
# this is now a little tricky, not officially documented, so might break but it is the cleanest solution
# first before the "flip" is done (e.g. switch between ondeck vs current) lets stop supervisord
echo -e '#!/usr/bin/env bash\nservice supervisord stop' > /opt/elasticbeanstalk/hooks/appdeploy/enact/00_stop_supervisord.sh
chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/00_stop_supervisord.sh
# then right after the webserver is reloaded, we can start supervisord again
echo -e '#!/usr/bin/env bash\nservice supervisord start' > /opt/elasticbeanstalk/hooks/appdeploy/enact/99_z_start_supervisord.sh
chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/99_z_start_supervisord.sh
fi
Honestly... I don't really understand the meaning of above code ahaha... I just copied it from someone's blog... :D
And then we need to a new supervise.config inside .ebextensions folder like this:
packages:
yum:
python27-setuptools: []
files:
"/usr/bin/supervise.sh" :
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
#
# Author: Günter Grodotzki (gunter#grodotzki.co.za)
# Version: 2015-04-25
#
# install supervisord
#
# See:
# - https://github.com/Supervisor/initscripts
# - http://supervisord.org/
if [ "${SUPERVISE}" == "enable" ]; then
export HOME="/root"
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin"
easy_install supervisor
cat <<'EOB' > /etc/init.d/supervisord
#!/bin/bash
#
# supervisord Startup script for the Supervisor process control system
#
# Author: Mike McGrath <mmcgrath#redhat.com> (based off yumupdatesd)
# Jason Koppe <jkoppe#indeed.com> adjusted to read sysconfig,
# use supervisord tools to start/stop, conditionally wait
# for child processes to shutdown, and startup later
# Erwan Queffelec <erwan.queffelec#gmail.com>
# make script LSB-compliant
#
# chkconfig: 345 83 04
# description: Supervisor is a client/server system that allows \
# its users to monitor and control a number of processes on \
# UNIX-like operating systems.
# processname: supervisord
# config: /etc/supervisord.conf
# config: /etc/sysconfig/supervisord
# pidfile: /var/run/supervisord.pid
#
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $all
# Required-Stop: $all
# Short-Description: start and stop Supervisor process control system
# Description: Supervisor is a client/server system that allows
# its users to monitor and control a number of processes on
# UNIX-like operating systems.
### END INIT INFO
# Source function library
. /etc/rc.d/init.d/functions
# Source system settings
if [ -f /etc/sysconfig/supervisord ]; then
. /etc/sysconfig/supervisord
fi
# Path to the supervisorctl script, server binary,
# and short-form for messages.
supervisorctl=${SUPERVISORCTL-/usr/bin/supervisorctl}
supervisord=${SUPERVISORD-/usr/bin/supervisord}
prog=supervisord
pidfile=${PIDFILE-/var/run/supervisord.pid}
lockfile=${LOCKFILE-/var/lock/subsys/supervisord}
STOP_TIMEOUT=${STOP_TIMEOUT-60}
OPTIONS="${OPTIONS--c /etc/supervisord.conf}"
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon --pidfile=${pidfile} $supervisord $OPTIONS
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
touch ${lockfile}
$supervisorctl $OPTIONS status
fi
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $supervisord
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -rf ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
LSB=1 killproc -p $pidfile $supervisord -HUP
RETVAL=$?
echo
if [ $RETVAL -eq 7 ]; then
failure $"$prog reload"
else
$supervisorctl $OPTIONS status
fi
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $supervisord
RETVAL=$?
[ $RETVAL -eq 0 ] && $supervisorctl $OPTIONS status
;;
restart)
restart
;;
condrestart|try-restart)
if status -p ${pidfile} $supervisord >&/dev/null; then
stop
start
fi
;;
force-reload|reload)
reload
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload}"
RETVAL=2
esac
exit $RETVAL
EOB
chmod +x /etc/init.d/supervisord
cat <<'EOB' > /etc/sysconfig/supervisord
# Configuration file for the supervisord service
#
# Author: Jason Koppe <jkoppe#indeed.com>
# orginal work
# Erwan Queffelec <erwan.queffelec#gmail.com>
# adjusted to new LSB-compliant init script
# make sure elasticbeanstalk PARAMS are being passed through to supervisord
. /opt/elasticbeanstalk/support/envvars
# WARNING: change these wisely! for instance, adding -d, --nodaemon
# here will lead to a very undesirable (blocking) behavior
#OPTIONS="-c /etc/supervisord.conf"
PIDFILE=/var/run/supervisord/supervisord.pid
#LOCKFILE=/var/lock/subsys/supervisord.pid
# Path to the supervisord binary
SUPERVISORD=/usr/local/bin/supervisord
# Path to the supervisorctl binary
SUPERVISORCTL=/usr/local/bin/supervisorctl
# How long should we wait before forcefully killing the supervisord process ?
#STOP_TIMEOUT=60
# Remove this if you manage number of open files in some other fashion
#ulimit -n 96000
EOB
mkdir -p /var/run/supervisord/
chown webapp: /var/run/supervisord/
cat <<'EOB' > /etc/supervisord.conf
[unix_http_server]
file=/tmp/supervisor.sock
chmod=0777
[supervisord]
logfile=/var/app/support/logs/supervisord.log
logfile_maxbytes=0
logfile_backups=0
loglevel=warn
pidfile=/var/run/supervisord/supervisord.pid
nodaemon=false
nocleanup=true
user=webapp
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
[program:laravel_queue_general]
command=php /var/www/html/artisan queue:listen --timeout=600
directory=/var/www/html
stdout_logfile=/var/www/html/storage/logs/laravel-queue.log
logfile_maxbytes=0
logfile_backups=0
redirect_stderr=true
autostart=true
autorestart=true
startretries=86400
process_name=%(program_name)s_%(process_num)02d
numprocs=2
[program:laravel_queue_data]
command=php /var/www/html/artisan queue:listen --timeout=600 --queue=https://sqs.ap-southeast-1.amazonaws.com/333973004348/data-processing-queue
directory=/var/www/html
stdout_logfile=/var/www/html/storage/logs/laravel-queue.log
logfile_maxbytes=0
logfile_backups=0
redirect_stderr=true
autostart=true
autorestart=true
startretries=86400
process_name=%(program_name)s_%(process_num)02d
numprocs=30
[program:laravel_queue_notif]
command=php /var/www/html/artisan queue:listen --timeout=600 --queue=https://sqs.ap-southeast-1.amazonaws.com/333973004348/notifications-queue
directory=/var/www/html
stdout_logfile=/var/www/html/storage/logs/laravel-queue.log
logfile_maxbytes=0
logfile_backups=0
redirect_stderr=true
autostart=true
autorestart=true
startretries=86400
process_name=%(program_name)s_%(process_num)02d
numprocs=2
EOB
# this is now a little tricky, not officially documented, so might break but it is the cleanest solution
# first before the "flip" is done (e.g. switch between ondeck vs current) lets stop supervisord
echo -e '#!/usr/bin/env bash\nservice supervisord stop' > /opt/elasticbeanstalk/hooks/appdeploy/enact/00_stop_supervisord.sh
chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/00_stop_supervisord.sh
# then right after the webserver is reloaded, we can start supervisord again
echo -e '#!/usr/bin/env bash\nservice supervisord start' > /opt/elasticbeanstalk/hooks/appdeploy/enact/99_z_start_supervisord.sh
chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/99_z_start_supervisord.sh
fi
Sometime's the queue failed to start. So we need to run it manually by login to ec2 instance of amazon using putty or mobaextrem (this is my favorite ssh terminal). And then after login, we just need to execute this command :
sudo -i
cd /usr/bin
./supervise.sh
/opt/elasticbeanstalk/hooks/appdeploy/enact/99_z_start_supervisord.sh
./99_z_start_supervisord.sh
yaaapsss... that's all... :)
Note :
for checking whether the queue is running or not, we can use these :
ps ax|grep supervise
ps aux|grep sord

What RETVAL means?

I'm trying to create a template for scripts of start and stop services.
I was checking the template for tomcat start and stop and see the command RETVAL=$?.
What this does ? Should I keep it ?
By the way, my script it's below in case you guys wanna see it.
#!/bin/bash
#===================================================================================
#
#
FILE: <script-file-name>.sh
#
#
USAGE: <script-file-name>.sh [-d] [-l] [-oD logfile] [-h] [starting directories]
#
# DESCRIPTION: List and/or delete all stale links in directory trees.
#
The default starting directory is the current directory.
#
Don’t descend directories on other filesystems.
#
#
OPTIONS: see function ’usage’ below
# REQUIREMENTS: ---
#
BUGS: ---
#
NOTES: ---
#
AUTHOR: Valter Henrique, valter.henrique#<company>.com
#
COMPANY: <company>
#
VERSION: 1.0
#
CREATED: 03.14.13
#
REVISION: 03.14.13
#===================================================================================
#
# chkconfig: 345 90 12
# description: <service-name> start, stop, restart service
#
# Get function from functions library
. /etc/init.d/functions
folder=/<company>/<service-folder> #folder to the application
service="<service-name>" #name of the service
startup=$folder/start.sh
shutdown=$folder/stop.sh
#=== FUNCTION ================================================================
#
NAME: start
# DESCRIPTION: Start the service <service-name>
# PARAMETER 1: ---
#===============================================================================
start() {
#----------------------------------------------------------------------
# logging the start
#----------------------------------------------------------------------
initlog -c "echo -n Starting $service:"
#----------------------------------------------------------------------
# getting the process PID
#----------------------------------------------------------------------
pid_process=`ps -ef | grep "$folder/<jar>.jar" | grep -v grep |awk -F' ' '{ print $2 }'`;
if [ $pid_process ]; then
echo "<service-name> is running!"
echo "Stop then first!"
else
action $"Starting <service-name> service: " su - <user_deployer> -c $startup
RETVAL=$?
fi
#----------------------------------------------------------------------
# create the lock file
#----------------------------------------------------------------------
touch /var/lock/subsys/$service
success $"Sucess $service startup"
echo
}
#=== FUNCTION ================================================================
#
NAME: stop
# DESCRIPTION: Stop the service <service-name>
# PARAMETER 1: ---
#===============================================================================
stop() {
#----------------------------------------------------------------------
# logging the stop
#----------------------------------------------------------------------
initlog -c "echo -n Stopping $service: "
killproc $service
#----------------------------------------------------------------------
# now, delete the lock file
#----------------------------------------------------------------------
rm -f /var/lock/subsys/$service
echo
}
#----------------------------------------------------------------------
# Main Logic
#----------------------------------------------------------------------
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $service
;;
restart|reload|condrestart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0
$? gives the status of the last command executed. In your case, the last command executed was action.... . The exit status of this command will be present in $? which is later captured in the RETVAL variable.
If the command was successful, $? will contain 0, else a non-zero value.
RETVAL is a variable. $? is assigning the status of the last executed command to the RETVAL variable.
As others stated, $? is the exit status of the last command.
Now, regarding your question...
RETVAL is not used anywhere else in a script, but remember, in bash, regular variables are global so they can be used by other functions. As you can see, there's a success call that might use it. In the distribution I checked /etc/init.d/functions doesn't use this variable, so the line is just noise and can be removed.. check your distribution to see what it does.

Resources