I am writing a script, for the first time. I followed few examples and I have my init script and then a control script that uses the loop option, to create a copy if itself and start the final application.
My questions are:
1. What is the difference between an init script and a control script? Why is better to implement them separately?
2. Why do I need to create a copy of the control script to start the application (loop), why not do it directly with the control script? What are the benefits of doing it like this?
3. How could I improve my scripts? For example I don't know if using directly kill -9 is the best option, I have read few comments against it..
My init script is very basic:
#!/bin/bash
APPCTL="/home/app/appctl"
APPBIN="/home/app/app.cpp"
if [ ! -x "$APPCTL" -o ! -f "$APPBIN" ]; then
echo "ERROR: app is missing or is not executable" >&2
exit 1
fi
if [ "$1" = "stop" -o "$1" = "restart" ]; then
echo "Stopping app"
"$APPCTL" stop
fi
if [ "$1" = "start" -o "$1" = "restart" ]; then
echo "Starting app"
"$APPCTL" start
fi
My control script is the following:
#!/bin/sh
APP_DIR="/home/app"
APP_CTL="$APP_DIR/appctl"
APP_BIN="$APP_DIR/app.cpp"
APPCLT_PID="/var/run/appctl.pid"
OWN_PID=$$
case "$1" in
start)
if [ -f "$APPCLT_PID" ]; then
PID=$(cat "$APPCLT_PID")
fi
if [ "$PID" != "" ]; then
COMM=$(ps --pid "$PID" ho comm)
if [ "$COMM" == "appctl" ]; then
echo "Already runnning..."
exit 0
fi
fi
"$APP_CTL" loop 2>&1 &
;;
stop)
if [ -f "$APPCLT_PID" ]; then
PID=$(cat $APPCLT_PID)
if [ "$PID" != "" ]; then
kill -9 "$PID"
fi
rm -f "$APPCLT_PID"
fi
for pid in $(pgrep -f $APP_BIN); do
kill $pid
done
;;
loop)
echo $OWN_PID > "$APPCLT_PID"
cd "$APP_DIR"
while true; do
nice -n 10 "$APP_BIN" > /dev/null 2>&1
sleep 5
done
;;
esac
exit 0
Thanks.
Related
I've been following a tutorial on connecting a raspberry pi to the AWS greengrass and I keep getting a segmentation fault on the final step. AWS provided me with this greengrassd shell script however when i run it I'm getting a segmentation fault. I have no idea why its throwing this error so any help would be appreciated.
AWS Greengrass Tutorial / RaspberryPi
Error
pi#raspberrypi:/greengrass/ggc/packages/1.1.0 $ sudo ./greengrassd start
Setting up greengrass daemon
Validating execution environment
Found cgroup subsystem: cpu
Found cgroup subsystem: cpuacct
Found cgroup subsystem: blkio
Found cgroup subsystem: memory
Found cgroup subsystem: devices
Found cgroup subsystem: freezer
Found cgroup subsystem: net_cls
Starting greengrass daemon./greengrassd: line 158: 2254 Segmentation fault nohup $COMMAND > /dev/null 2> $CRASH_LOG < /dev/null
Greengrass daemon 2254 failed to start
greengrassd script
#!/usr/bin/env bash
##########Environment Requirement for Greengrass Daemon##########
# by default, the daemon assumes it's going to be launched from a directory
# that has the following structure:
# GREENGRASS_ROOT/
# greengrassd
# bin/daemon
# configuration/
# group/group.json
# certs/server.crt
# lambda/
# system_lambda1/...
# system_lambda2/...
# root cgroup has to be mounted separately, this script doesn't do that for you.
#################################################################
set -e
PWD=$(cd $(dirname "$0"); pwd)
GGC_PKG_HOME=$(readlink -f $PWD)
GG_HOME=$(cd $GGC_PKG_HOME/../../; pwd)
CRASH_LOG=$GG_HOME/var/log/crash.log
GGC_ROOT_FS=$GGC_PKG_HOME/ggc_root
PID_FILE=/var/run/greengrassd.pid
FS_SETTINGS=/proc/sys/fs
GGC_GROUP=ggc_group
GGC_USER=ggc_user
MAX_DAEMON_KILL_WAIT_SECONDS=60
RETRY_SIGTERM_INTERVAL_SECONDS=20
if [ -z "$COMMAND" ]; then
COMMAND="$GGC_PKG_HOME/bin/daemon -core-dir=$GGC_PKG_HOME -greengrassdPid=$$"
fi
# Function ran as part of initial setup
setup() {
echo "Setting up greengrass daemon"
mkdir -p $GGC_ROOT_FS
# Mask greengrass directory for containers
mknod $GGC_ROOT_FS/greengrass c 1 3 &>/dev/null || true
mkdir -p $(dirname "$CRASH_LOG")
}
validatePlatformSecurity() {
if [[ -f $FS_SETTINGS/protected_hardlinks &&
-f $FS_SETTINGS/protected_symlinks ]]; then
PROT_HARDLINK_VAL=$(cat $FS_SETTINGS/protected_hardlinks)
PROT_SOFTLINK_VAL=$(cat $FS_SETTINGS/protected_symlinks)
if [[ "$PROT_HARDLINK_VAL" -ne 1 || "$PROT_SOFTLINK_VAL" -ne 1 ]]; then
echo "AWS Greengrass detected insecure OS configuration: No hardlink/softlink protection enabled." | tee -a $CRASH_LOG
exit 1
fi
fi
}
validateEnvironment() {
echo "Validating execution environment"
# ensure all commands that the installation script is going to use are available
if ! type grep >/dev/null ; then
echo "grep command is NOT on the path or is NOT installed on the system"
exit 1
fi
if ! type cat >/dev/null ; then
echo "cat command is NOT on the path or is NOT installed on the system"
exit 1
fi
if ! type awk >/dev/null ; then
echo "awk command is NOT on the path or is NOT installed on the system"
exit 1
fi
if ! type id >/dev/null ; then
echo "id command is NOT on the path or is NOT installed on the system"
exit 1
fi
if ! type ps >/dev/null ; then
echo "ps command is NOT on the path or is NOT installed on the system"
exit 1
fi
if ! type sqlite3 >/dev/null ; then
echo "sqlite3 command is NOT on the path or is NOT installed on the system"
exit 1
fi
# the script needs to be run as root
if [ ! $(id -u) = 0 ]; then
echo "The script needs to be run using sudo"
exit 1
fi
if ! id $GGC_USER >/dev/null ; then
echo "${GGC_USER} doesn't exist. Please add a user ${GGC_USER} on the system"
exit 1
fi
if ! grep -q $GGC_GROUP /etc/group ; then
echo "${GGC_GROUP} doesn't exist. Please add a group ${GGC_GROUP} on the system"
exit 1
fi
# ensure that kernel supports cgroup
if [ ! -e /proc/cgroups ]; then
echo "The kernel in use does NOT support cgroup."
exit 1
fi
# assume that all kernel supported subsystems, which are listed in /proc/cgroups, are going to be used
# so check whether all of them are mounted.
for d in `awk '$4 == 1 {print $1}' /proc/cgroups`; do
if cat /proc/self/cgroup | grep -q $d; then
echo "Found cgroup subsystem: $d"
else
# exit with error if can't find cgroup
echo "The cgroup subsystem is not mounted: $d"
exit 1
fi
done
}
finish() {
pid=$1
echo "$pid" > $PID_FILE
echo ""
echo -e "\e[0;32mGreengrass successfully started with PID: $pid\e[0m"
exit 0
}
start() {
setup
if [[ $INSECURE -ne 1 ]]; then
validatePlatformSecurity
fi
validateEnvironment
trap 'finish $pid' SIGUSR1
echo ""
echo -n "Starting greengrass daemon"
if nohup $COMMAND >/dev/null 2>$CRASH_LOG < /dev/null &
then
pid=$!
# sleep 10 seconds to wait for daemon to start or exit
sleep 10 &
wait $!
echo ""
echo "Greengrass daemon $pid failed to start"
echo -e "\e[0;31m$(cat $CRASH_LOG)\e[0m"
exit 1
else
echo "Failed to start Greengrass daemon"
exit 1
fi
}
version() {
$GGC_PKG_HOME/bin/daemon --version
}
stop() {
if [ -f $PID_FILE ]; then
PID=$(cat $PID_FILE)
echo "Stopping greengrass daemon of PID: $PID"
if [ ! -e "/proc/$PID" ]; then
rm $PID_FILE
echo "Process with pid $PID does not exist already"
return 0
fi
echo -n "Waiting"
kill "$PID" > /dev/null 2>&1
total_sleep_seconds=0
until [ "$total_sleep_seconds" -ge "$MAX_DAEMON_KILL_WAIT_SECONDS" ]; do
sleep 1
# If the pid no longer exists, we're done, remove the pid file and exit. Otherwise, just increment the loop counter
if [ ! -e "/proc/$PID" ]; then
rm $PID_FILE
echo -e "\nStopped greengrass daemon, exiting with success"
break
else
total_sleep_seconds=$(($total_sleep_seconds+1))
echo -n "."
fi
# If it has been $RETRY_SIGTERM_INTERVAL_SECONDS since the last SIGTERM, send SIGTERM
if [ $(($total_sleep_seconds % $RETRY_SIGTERM_INTERVAL_SECONDS)) -eq "0" ]; then
kill "$PID" > /dev/null 2>&1
fi
done
if [ $total_sleep_seconds -ge $MAX_DAEMON_KILL_WAIT_SECONDS ] && [ -e "/proc/$PID" ]; then
# If we are here, we never exited in the previous loop and the pid still exists. Exit with failure.
kill -9 "$PID" > /dev/null 2>&1
echo -e "\nProcess with pid $PID still alive after timeout of $MAX_DAEMON_KILL_WAIT_SECONDS seconds. Forced kill process, exiting with failure."
exit 1
fi
fi
}
usage() {
echo ""
echo "Usage: $0 [FLAGS] {start|stop|restart}"
echo ""
echo -e "[FLAGS]: \n -i, --insecure \t Run GGC in insecure mode without hardlink/softlink protection, (highly discouraged for production use) \n -v, --version \t\t Outputs the version of GGC."
echo ""
exit 1
}
if [[ $# -eq 0 ]]; then
usage
fi
for var in "$#"
do
case "$var" in
-v|--version)
version
exit 0
;;
esac
done
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-i|--insecure)
mkdir -p $(dirname "$CRASH_LOG")
echo "Warning! You are running in insecure mode, this is highly discouraged!" | tee -a $CRASH_LOG
INSECURE=1
;;
-h|--help)
usage
;;
start)
stop
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
usage
esac
shift
done
#Jim Maybe check the model of Pi you are using?
It seems that the Pi version of Greengrass is for ARMv7-A. I got this problem too and I'm using an older Model 1 B+ which is ARMv6Z (https://en.wikipedia.org/wiki/Raspberry_Pi#Specifications).
The error we're seeing for line 158 is the ./greengrassd script waiting for the actual process to run:
sudo /greengrass/ggc/packages/1.1.0/bin/daemon -core-dir=/greengrass/ggc/packages/1.1.0 -greengrassdPid=641
/greengrass/ggc/packages/1.1.0/bin/daemon is the binary. If you run the above command directly in the console it exits with the same segmentation fault error.
AWS do recommend using the Pi 3 so I'm guessing it will work on that.
I have written the below code to create a pid file for my spring boot application.
File pid = new File("service.pid");
pid.deleteOnExit();
SpringApplication springApplication = new SpringApplication(BusRouteServiceApplication.class);
springApplication.addListeners(new ApplicationPidFileWriter(pid));
springApplication.run(args);
Still the pid file gets created in the name "app.pid". And I am using the below script to start and stop the spring app.
#!/bin/bash
RUN="java -jar service-0.0.1-SNAPSHOT-exec.jar --filename=$2"
NAME=app
DATA_FILE=$2
PIDFILE=$NAME.pid
LOGFILE=/tmp/$NAME.log
start() {
if [ -f $PIDFILE ]; then
if kill -0 $(cat $PIDFILE); then
echo 'Service already running' >&2
return 1
else
rm -f $PIDFILE
fi
fi
local CMD="$RUN $DATA_FILE &> \"$LOGFILE\" & echo \$!"
sh -c "$CMD" > $PIDFILE
}
stop() {
if [ ! -f $PIDFILE ] || ! kill -0 $(cat $PIDFILE); then
echo 'Service not running' >&2
return 1
fi
kill $(cat $PIDFILE) && rm -f $PIDFILE
}
case $1 in
start)
start
;;
stop)
stop
;;
block)
start
sleep infinity
;;
*)
echo "Usage: $0 {start|stop|block} DATA_FILE"
esac
But service is not stopping. I have printed out the value of cat $PIDFILE. At times this is printing process ID + the date. At times it contains a list what looks like a lot of process ids. What am I doing wrong here? How can I correctly stop this spring boot app? I am new to spring. Any help would be much appreciated.
I am writing a init.d script for kibana
as of not script is running partially, but the issue is if I run run service kibana start even if service is running then second instance start which bothers me I want to add check before starting service, if service is running then dont start second instance. I tried to put if check on "/var/lock/subsys/kibana" but didn't work. Here is my script :
#!/bin/bash
KIBANA_PATH="/opt/kibana4"
DESC="Kibana Daemon"
NAME=kibana
DAEMON=bin/kibana
CONFIG_DIR=$KIBANA_PATH/config/kibana.yml
LOGFILE=/var/log/kibana/kibana.log
#ARGS="agent --config ${CONFIG_DIR} --log ${LOGFILE}"
SCRIPTNAME=/etc/init.d/kibana
PIDFILE=/var/run/kibana.pid
base=kibana
# Exit if the package is not installed
if [ ! -x "$KIBANA_PATH/$DAEMON" ]; then
{
echo "Couldn't find $DAEMON"
exit 99
}
fi
. /etc/init.d/functions
#
# Function that starts the daemon/service
#
do_start()
{
cd $KIBANA_PATH && \
($DAEMON >> $LOGFILE &) && \
success || failure;
}
set_pidfile()
{
pgrep -f "kibana.jar" > $PIDFILE
}
#
# Function that stops the daemon/service
#
do_stop()
{
pid=`cat $PIDFILE`
if checkpid $pid 2>&1; then
# TERM first, then KILL if not dead
kill -TERM $pid >/dev/null 2>&1
usleep 100000
if checkpid $pid && sleep 1 &&
checkpid $pid && sleep $delay &&
checkpid $pid ; then
kill -KILL $pid >/dev/null 2>&1
usleep 100000
fi
fi
checkpid $pid
RC=$?
[ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
}
case "$1" in
start)
echo -n "Starting $DESC: "
do_start
touch /var/lock/subsys/$NAME
set_pidfile
;;
stop)
echo -n "Stopping $DESC: "
do_stop
rm /var/lock/subsys/$NAME
rm $PIDFILE
;;
restart|reload)
echo -n "Restarting $DESC: "
do_stop
do_start
touch /var/lock/subsys/$NAME
set_pidfile
;;
status)
echo $DESC
status -p $PIDFILE
echo $!
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
exit 3
;;
esac
echo
exit 0
any help here ?
Thanks
use lockfile -r0 /path/to/lock/file.lck when you start the service. every new access then will retry zero times to create the file. so if that command fails do nothing or start the service otherwise.
lockfile -r0 /path/to/lock/file.lck
if [ "$?" == "0" ]; then
echo "lock does not exist. enter devils land :)"
fi
The following is a pretty standard implementation of this feature used by most init.d scripts.
start () {
[ -d /var/run/nscd ] || mkdir /var/run/nscd
[ -d /var/db/nscd ] || mkdir /var/db/nscd
echo -n $"Starting $prog: "
daemon /usr/sbin/nscd $NSCD_OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/nscd
return $RETVAL
}
...
# See how we were called.
case "$1" in
start)
[ -e /var/lock/subsys/nscd ] || start
RETVAL=$?
;;
...
I'm something of an amateur when it comes to Bash shell scripting and am baffled by this first line in the killproc() function in /etc/init.d/functions on a CentOS 5.10 system:
local RC killlevel= base pid pid_file= delay try
What is the meaning of that line? I believe it is the declaration of local variables perhaps, but what is the meaning of the = after a couple of the items? I have included the complete killproc() function below for reference:
# A function to stop a program.
killproc() {
local RC killlevel= base pid pid_file= delay try
RC=0; delay=3; try=0
# Test syntax.
if [ "$#" -eq 0 ]; then
echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
return 1
fi
if [ "$1" = "-p" ]; then
pid_file=$2
shift 2
fi
if [ "$1" = "-d" ]; then
delay=$(echo $2 | awk -v RS=' ' -v IGNORECASE=1 '{if($1!~/^[0-9.]+[smhd]?$/) exit 1;d=$1~/s$|^[0-9.]*$/?1:$1~/m$/?60:$1~/h$/?60*60:$1~/d$/?24*60*60:-1;if(d==-1) exit 1;delay+=d*$1} END {printf("%d",delay+0.5)}')
if [ "$?" -eq 1 ]; then
echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
return 1
fi
shift 2
fi
# check for second arg to be kill level
[ -n "${2:-}" ] && killlevel=$2
# Save basename.
base=${1##*/}
# Find pid.
__pids_var_run "$1" "$pid_file"
if [ -z "$pid_file" -a -z "$pid" ]; then
pid="$(__pids_pidof "$1")"
fi
# Kill it.
if [ -n "$pid" ] ; then
[ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
if [ -z "$killlevel" ] ; then
if checkpid $pid 2>&1; then
# TERM first, then KILL if not dead
kill -TERM $pid >/dev/null 2>&1
usleep 100000
if checkpid $pid ; then
try=0
while [ $try -lt $delay ] ; do
checkpid $pid || break
sleep 1
let try+=1
done
if checkpid $pid ; then
kill -KILL $pid >/dev/null 2>&1
usleep 100000
fi
fi
fi
checkpid $pid
RC=$?
[ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
RC=$((! $RC))
# use specified level only
else
if checkpid $pid; then
kill $killlevel $pid >/dev/null 2>&1
RC=$?
[ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
elif [ -n "${LSB:-}" ]; then
RC=7 # Program is not running
fi
fi
else
if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
RC=7 # Program is not running
else
failure $"$base shutdown"
RC=0
fi
fi
# Remove pid file if any.
if [ -z "$killlevel" ]; then
rm -f "${pid_file:-/var/run/$base.pid}"
fi
return $RC
}
It's just declaring a lot of function scope variables, where killlevel and pid_file are assigned the empty string and the others are not assigned anything.
There is a slight difference. local variables are also visible in functions called from the function. So if you happen to call killproc() recursively, the uninitialized ones are remembered from the caller.
On Debian the lib/lsb/init-functions file looks like this:
killproc() {
local pidfile sig status base name_param is_term_sig OPTIND
pidfile=
name_param=
is_term_sig=
I have a script to start and stop my node.js server.
When I stop the script, the forever process is killed however the node process is not terminated.
Is there any way to stop both forver and node process when I issue
Kill $FOREVER_PID
Here is the script -
#!/bin/bash
path="/Users/aayush/Desktop/node/rest-api"
action="forever errorLog_express.js "
logFile="$path/system.log"
pidFile="$path/pidFile.pid"
#messages
usage="Usage : node-script.sh start|stop"
panic="Panic! nothing to do, exiting"
unknown="Unrecognized parameter"
start="[starting node-forever]"
end="[stopping node-forever]"
notRunning="Process node-forever not running"
alreadyRunning="Process node-forever already running"
if [ -z $1 ]
then
echo $panic
echo $usage
exit 0;
fi
if [ $1 = "start" ]
then
# starting process
dummy="OK"
if [ -f $pidFile ];
then
exit 0
else
cd $path
echo "cd $path"
echo $start
echo $start >> $logFile
$action > /dev/null 2>&1 &
Process_Pid=$!
echo $Process_Pid > $pidFile
echo $dummy
exit 0
fi
elif [ $1 = "stop" ]
then
# stopping process by getting pid from pid file
dummy="OK"
echo $end
echo $end >> $logFile
if [ -f $pidFile ];
then
while IFS=: read -r pid
do
# reading line in variable pid
if [ -z $pid ]
then
dummy="FAILED"
echo "Could not parse pid PANIC ! do 'ps' and check manully"
else
echo "Process Pid : $pid"
kill $pid
fi
done <"$pidFile"
rm $pidFile
echo $dummy
exit 0
else
echo $notRunning
echo "FAILED"
exit 0
fi
else
echo $unknown
echo $usage
exit 0
fi
The final script working for me -
#!/bin/bash
#proccessname: node
USER=node
PWD=node
node=node
forever=forever
path="/Users/aayush/Desktop/node/rest-api"
action="forever start -l forever.log -a -o out.log -e err.log errorLog_express.js "
start(){
cd $path
$action
}
stop(){
/usr/local/bin/forever stopall
}
restart(){
stop
start
}
status(){
/usr/local/bin/forever list
}
#Options
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo $ "usage $0 {start | stop | status | restart}"
exit 1
esac
exit 0
Yes there is, use a signal handler in your script to catch the sigterm and kill the node process.
www.gnu.org/software/bash/manual/html_node/Signals.html
$ killall node
Will kill them.