Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#! /bin/sh
### BEGIN INIT INFO
# Provides: cp
# Required-Start: $local_fs $remote_fs $syslog
# Required-Stop: $local_fs $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start ruby scripts
# Description: Start ruby scripts or daemons in rails control panel website on system startup
### END INIT INFO
# Ruby executable needs to be a wrapper script because rvm is being used
RUBYEXEC="/home/naftilos76/.rvm/gems/ruby-2.2.0/wrappers/ruby"
SCRIPTSPATH="/home/naftilos76/rails/cp/scripts"
status=0
# Run all ruby files in path /home/naftilos76/rails/cp/scripts/
execute_script() {
if [ -f "$SCRIPTSPATH/$1" ]; then
$RUBYEXEC "$SCRIPTSPATH/$1";
echo "complete"
status=0
else
echo "⚫ Error: Script file $SCRIPTSPATH/$1 was not found"
status=1
fi
}
# Run all ruby files in path /home/naftilos76/rails/cp/scripts/
execute_scripts() {
for filename in $SCRIPTSPATH/*.rb; do
echo "⚫ Initiating process $filename";
$RUBYEXEC $filename;
done
status=0
}
kill_script() {
if [ -f "$SCRIPTSPATH/$1.pid" ]; then
rm "$SCRIPTSPATH/$1.pid";
echo "complete"
status=0
else
echo "⚫ Error: No PID file found for file $SCRIPTSPATH/$1, omitting shutdown...";
status=1
fi
}
kill_scripts() {
for filename in $SCRIPTSPATH/*.rb; do
if [ -f "$filename.pid" ]; then
echo "⚫ Shutting down process $filename";
rm "$filename.pid";
if [ status = 0 ]; then
status=0
fi
else
echo "⚫ Error: No PID file found for file $filename, omitting shutdown...";
status=1
fi
done
}
case "$1" in
start)
if [ -z "$2" ]; then
echo "Starting ruby scripts in $SCRIPTSPATH..."
execute_scripts
echo "Initialization of all processes complete"
exit $status
else
echo "Starting ruby script $2..."
execute_script $2
exit $status
fi
;;
stop)
if [ -z "$2" ]; then
echo "Stopping all ruby scripts in $SCRIPTSPATH..."
kill_scripts
echo "Shutting down of all processes complete"
exit $status
else
echo "Shutting down ruby script $2..."
kill_script $2
exit $status
fi
;;
restart)
$0 stop
$0 start
;;
*)
log_success_msg "Usage: /etc/init.d/cp {start|stop|restart}"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment