Service/Mountpoint definition syntax:
M:0:/mnt/storage
S:1:transmission
0 defines service which needs to be started successfully, otherwise the script will end.
Services with 1 can fail during start without any effect on next script execution.
#!/bin/bash
##############
# krisko 2014
# script for starting services with systemctl and mounting FS
##############
# we need to run this as root
[ "$UID" == 0 ] || exec sudo $0
# JOB QUEUE: M -mountpoint, S -service; 0 -exit on fail, 1 -dont exit on fail
QUEUE=('M:0:/mnt/storage' 'S:1:transmission' 'S:0:mysqld' 'S:1:php-fpm' 'S:1:nginx' 'S:1:rpc-mountd' 'S:1:fail2ban')
# handle mounting
M() {
if [ $(mount | grep -c " $2 ") -ge 1 ]; then
echo INFO: $2 already mounted, skipping...
else
echo INFO: mounting $2
if [ $(mount $2 2>/dev/null; echo $?) -ne 0 ]; then
if [ $1 -eq 0 ]; then
echo FATAL: failed mounting $2
return 1
else
echo ERROR: failed mounting $2
fi
fi
fi
}
# handle service starting
S() {
if [ $(systemctl status $2 | grep -q "(running)"; echo $?) -eq 0 ]; then
echo INFO: $2 already running, skipping...
else
echo INFO: starting $2
if [ $(systemctl start $2 1>/dev/null 2>&1; echo $?) -ne 0 ]; then
if [ $1 -eq 0 ]; then
echo FATAL: failed to start $2, see \'systemctl status $2\' for more details
return 1
else
echo ERROR: failed to start $2, see \'systemctl status $2\' for more details
fi
fi
fi
}
# export functions to sub-shell, so they can be called from awk
export -f M S
for JOB in ${QUEUE[*]}; do
echo | awk -v JOB="$JOB" '{
gsub(":"," ",JOB)
if ( system(JOB) == 1 )
exit 1
}'
# exit on fail
[ $(echo $?) -ne 1 ] || exit 1
done
exit 0
##################
# ChangeLog/TODO #
##################
# 1.0.0 20140101 Genesis
# 1.0.1 20140312 Changed services starting order
##################
No comments:
Post a Comment