Refactorization of bash scripts
This commit is contained in:
24
src/main/bash/codiki-init.d.sh
Executable file
24
src/main/bash/codiki-init.d.sh
Executable file
@@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
'start')
|
||||||
|
sudo -H -u wildfly bash -c '/opt/codiki/bin/codiki.sh start'
|
||||||
|
;;
|
||||||
|
'stop')
|
||||||
|
sudo -H -u wildfly bash -c '/opt/codiki/bin/codiki.sh stop'
|
||||||
|
;;
|
||||||
|
'status')
|
||||||
|
sudo -H -u wildfly bash -c '/opt/codiki/bin/codiki.sh status'
|
||||||
|
;;
|
||||||
|
'restart')
|
||||||
|
sudo -H -u wildfly bash -c '/opt/codiki/bin/codiki.sh restart'
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
# If no argument, we launch the app in case of server startup
|
||||||
|
sudo -H -u wildfly bash -c '/opt/codiki/bin/codiki.sh start &>/dev/null'
|
||||||
|
# And show the script usage
|
||||||
|
echo "Usage: /etc/init.d/codiki {start|stop|status|restart}\n" >&2
|
||||||
|
exit 3
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
exit 0
|
||||||
24
src/main/bash/codiki-integ-init.d.sh
Executable file
24
src/main/bash/codiki-integ-init.d.sh
Executable file
@@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
'start')
|
||||||
|
sudo -H -u wildfly bash -c '/opt/codiki-integ/bin/codiki-integ.sh start'
|
||||||
|
;;
|
||||||
|
'stop')
|
||||||
|
sudo -H -u wildfly bash -c '/opt/codiki-integ/bin/codiki-integ.sh stop'
|
||||||
|
;;
|
||||||
|
'status')
|
||||||
|
sudo -H -u wildfly bash -c '/opt/codiki-integ/bin/codiki-integ.sh status'
|
||||||
|
;;
|
||||||
|
'restart')
|
||||||
|
sudo -H -u wildfly bash -c '/opt/codiki-integ/bin/codiki-integ.sh restart'
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
# If no argument, we launch the app in case of server startup
|
||||||
|
sudo -H -u wildfly bash -c '/opt/codiki-integ/bin/codiki-integ.sh start &>/dev/null'
|
||||||
|
# And show the script usage
|
||||||
|
echo "Usage: /etc/init.d/codiki-integ {start|stop|status|restart}\n" >&2
|
||||||
|
exit 3
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
exit 0
|
||||||
77
src/main/bash/codiki-integ.sh
Executable file
77
src/main/bash/codiki-integ.sh
Executable file
@@ -0,0 +1,77 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
|
||||||
|
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
|
||||||
|
set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Colors
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
NC='\033[0m'
|
||||||
|
|
||||||
|
DESC="The Codiki web app server"
|
||||||
|
NAME='codiki'
|
||||||
|
DAEMON_HOME='/opt/codiki-integ/bin'
|
||||||
|
DAEMON="$DAEMON_HOME/$NAME.jar"
|
||||||
|
PIDFILE="$DAEMON_HOME/$NAME.pid"
|
||||||
|
SCRIPTNAME="$DAEMON_HOME/$0"
|
||||||
|
|
||||||
|
start()
|
||||||
|
{
|
||||||
|
# If PIDFILE exists and PID is in running processes
|
||||||
|
if [ -f $PIDFILE ] && kill -0 `cat $PIDFILE` 2>/dev/null
|
||||||
|
then
|
||||||
|
echo 'Service already running\n'
|
||||||
|
else
|
||||||
|
echo "Starting service $NAME"
|
||||||
|
nohup 2>/dev/null java -jar $DAEMON &>/dev/null &
|
||||||
|
expr $! - 1 > $PIDFILE
|
||||||
|
echo "Service started [`cat $PIDFILE`]\n"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
stop()
|
||||||
|
{
|
||||||
|
# If PIDFILE doesn't exists or PID isn't in running processes
|
||||||
|
if [ ! -f "$PIDFILE" ] || ! kill -0 `cat "$PIDFILE"`
|
||||||
|
then
|
||||||
|
echo 'Service not running\n'
|
||||||
|
else
|
||||||
|
echo 'Stopping service...'
|
||||||
|
# Send signal to end to the process
|
||||||
|
kill -15 `cat "$PIDFILE"` && rm -f "$PIDFILE"
|
||||||
|
echo 'Service stopped\n'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
status()
|
||||||
|
{
|
||||||
|
if [ -f $PIDFILE ] && kill -0 `cat $PIDFILE` 2>/dev/null
|
||||||
|
then
|
||||||
|
echo "Service is running (${GREEN}● active${NC})\n"
|
||||||
|
else
|
||||||
|
echo "Service not running (${RED}● inactive${NC})\n"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
'start')
|
||||||
|
start
|
||||||
|
;;
|
||||||
|
'stop')
|
||||||
|
stop
|
||||||
|
;;
|
||||||
|
'status')
|
||||||
|
status
|
||||||
|
;;
|
||||||
|
'restart')
|
||||||
|
stop
|
||||||
|
sleep 5
|
||||||
|
start
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Usage: $SCRIPTNAME {start|stop|status|restart}\n" >&2
|
||||||
|
exit 3
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
exit 0
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
workspace='/opt/codiki'
|
|
||||||
codikiScreenName='codiki'
|
|
||||||
logFile='codiki-'`date '+%Y-%m-%d_%H-%M-%S'`'.log'
|
|
||||||
screenRegex='codiki.\(([0-9]{2}/){2}[0-9]{4} [0-9]{2}(:[0-9]{2}){2}\)'
|
|
||||||
screenRegexInteg='codiki-integ.\(([0-9]{2}/){2}[0-9]{4} [0-9]{2}(:[0-9]{2}){2}\)'
|
|
||||||
|
|
||||||
# Colors
|
|
||||||
RED='\033[0;31m'
|
|
||||||
GREEN='\033[0;32m'
|
|
||||||
NC='\033[0m'
|
|
||||||
|
|
||||||
start() {
|
|
||||||
screen -dmS "$codikiScreenName" java -jar $workspace/bin/codiki.jar &>> "$workspace/log/$logFile"
|
|
||||||
if [ $? -ne 0 ]
|
|
||||||
then
|
|
||||||
echo "Service failed to start..."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
stop() {
|
|
||||||
if [ $(status_by_screen) -eq 1 ]
|
|
||||||
then
|
|
||||||
screen -dr $codikiScreenName -X -S quit
|
|
||||||
if [ $? -eq 0 ]
|
|
||||||
then
|
|
||||||
sleep 10
|
|
||||||
else
|
|
||||||
echo "Service failed to stop..."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "Service is not running."
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
status() {
|
|
||||||
if [ $(status_by_screen) -eq 1 ]
|
|
||||||
then
|
|
||||||
echo -e " codiki.service - The Codiki web app server"
|
|
||||||
echo -e " Active: ${GREEN}active${NC} (running)"
|
|
||||||
echo ''
|
|
||||||
else
|
|
||||||
echo -e " codiki.service - The Codiki web app server"
|
|
||||||
echo -e " Active: ${RED}inactive${NC} (dead)"
|
|
||||||
echo ''
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
restart() {
|
|
||||||
if [ $(status_by_screen) -eq 1 ]
|
|
||||||
then
|
|
||||||
stop
|
|
||||||
fi
|
|
||||||
start
|
|
||||||
}
|
|
||||||
|
|
||||||
status_by_screen() {
|
|
||||||
echo $(screen -ls | egrep -o "$screenRegex" | wc -l)
|
|
||||||
}
|
|
||||||
|
|
||||||
case "$1" in
|
|
||||||
start)
|
|
||||||
start
|
|
||||||
;;
|
|
||||||
stop)
|
|
||||||
stop
|
|
||||||
;;
|
|
||||||
status)
|
|
||||||
status
|
|
||||||
;;
|
|
||||||
restart)
|
|
||||||
restart
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "Usage: $0 {start|stop|status}"
|
|
||||||
echo ''
|
|
||||||
exit 1
|
|
||||||
esac
|
|
||||||
exit 0
|
|
||||||
77
src/main/bash/codiki.sh
Executable file
77
src/main/bash/codiki.sh
Executable file
@@ -0,0 +1,77 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
|
||||||
|
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
|
||||||
|
set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Colors
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
NC='\033[0m'
|
||||||
|
|
||||||
|
DESC="The Codiki web app server"
|
||||||
|
NAME='codiki'
|
||||||
|
DAEMON_HOME='/opt/codiki/bin'
|
||||||
|
DAEMON="$DAEMON_HOME/$NAME.jar"
|
||||||
|
PIDFILE="$DAEMON_HOME/$NAME.pid"
|
||||||
|
SCRIPTNAME="$DAEMON_HOME/$0"
|
||||||
|
|
||||||
|
start()
|
||||||
|
{
|
||||||
|
# If PIDFILE exists and PID is in running processes
|
||||||
|
if [ -f $PIDFILE ] && kill -0 `cat $PIDFILE` 2>/dev/null
|
||||||
|
then
|
||||||
|
echo 'Service already running\n'
|
||||||
|
else
|
||||||
|
echo "Starting service $NAME"
|
||||||
|
nohup 2>/dev/null java -jar $DAEMON &>/dev/null &
|
||||||
|
expr $! - 1 > $PIDFILE
|
||||||
|
echo "Service started [`cat $PIDFILE`]\n"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
stop()
|
||||||
|
{
|
||||||
|
# If PIDFILE doesn't exists or PID isn't in running processes
|
||||||
|
if [ ! -f "$PIDFILE" ] || ! kill -0 `cat "$PIDFILE"`
|
||||||
|
then
|
||||||
|
echo 'Service not running\n'
|
||||||
|
else
|
||||||
|
echo 'Stopping service...'
|
||||||
|
# Send signal to end to the process
|
||||||
|
kill -15 `cat "$PIDFILE"` && rm -f "$PIDFILE"
|
||||||
|
echo 'Service stopped\n'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
status()
|
||||||
|
{
|
||||||
|
if [ -f $PIDFILE ] && kill -0 `cat $PIDFILE` 2>/dev/null
|
||||||
|
then
|
||||||
|
echo "Service is running (${GREEN}● active${NC})\n"
|
||||||
|
else
|
||||||
|
echo "Service not running (${RED}● inactive${NC})\n"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
'start')
|
||||||
|
start
|
||||||
|
;;
|
||||||
|
'stop')
|
||||||
|
stop
|
||||||
|
;;
|
||||||
|
'status')
|
||||||
|
status
|
||||||
|
;;
|
||||||
|
'restart')
|
||||||
|
stop
|
||||||
|
sleep 5
|
||||||
|
start
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Usage: $SCRIPTNAME {start|stop|status|restart}\n" >&2
|
||||||
|
exit 3
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user