#! /bin/sh # Sugoi-Mail boot-up script. # This file should be in /etc/init.d/ # DO NOT FORGET TO SET YOUR INSTALLPATH AND SUGOIUSER # please rename this file to sugoi-mail (without the .EDIT_ME) #Written by: aavdacev@invio.co.jp INSTALLPATH="/--SET INSTALL PATH--" #ie: '/home/sugoi-user/sugoi-mail' SUGOIUSER="--SET USER RUNNING SUGOIMAIL--" #ie: 'sugoi-user' RunTests(){ maild_socket_exists=0 maild_pid='' maild_running=0 mongrel_pid='' mongrel_running=0 if test -e $INSTALLPATH/tmp/sockets/maild.socket then maild_socket_exists=1 fi if test -e $INSTALLPATH/tmp/maild.pid then maild_pid=`cat $INSTALLPATH/tmp/maild.pid` fi if [ ! -z "$(ps -fu $SUGOIUSER | grep ruby[\ ]$INSTALLPATH/bin/maild)" ] then maild_running=1 fi if test -e $INSTALLPATH/log/mongrel.pid then mongrel_pid=`cat $INSTALLPATH/log/mongrel.pid` if [ ! -z "$(ps -fu $SUGOIUSER | grep [\ ]$mongrel_pid[\ ].*mongrel_rails)" ] then mongrel_running=1 fi fi } Status(){ RunTests status_msg="" if [ "$maild_socket_exists" -eq 1 ]; then status_msg="$status_msg - 'tmp/sockets/maild.socket' exists. \n" fi if [ ! -z "$maild_pid" ]; then status_msg="$status_msg - 'tmp/maild.pid' exists and reads: $maild_pid \n" fi if [ "$maild_running" -eq 1 ]; then status_msg="$status_msg - Maild belonging to $SUGOIUSER is running. \n" fi if [ ! -z "$mongrel_pid" ]; then status_msg="$status_msg - 'log/mongrel.pid' exists and reads $mongrel_pid \n" fi if [ "$mongrel_running" -eq 1 ]; then status_msg="$status_msg - Mongrel webserver is running" fi if [ "$1" = "output" ] then echo -e "$status_msg" fi if [ "$maild_socket_exists" -eq 1 ] && [ ! -z "$maild_pid" ] && [ "$maild_running" -eq 1 ] && [ ! -z "$mongrel_pid" ] && [ "$mongrel_running" -eq 1 ] then echo "Sugoi-mail: all systems are running." elif [ ! "$maild_socket_exists" -eq 1 ] && [ -z "$maild_pid" ] && [ ! "$maild_running" -eq 1 ] && [ -z "$mongrel_pid" ] && [ ! "$mongrel_running" -eq 1 ] then echo "Sugoi-mail: all processes safely stopped." else echo "Sugoi-mail: irregular state. Failed to start or stop properly." echo ">> Try 'cleanup' or consult 'help'." fi } Menu(){ case $1 in start) if [ "$mongrel_running" -eq 1 ]; then echo "ERR: Sugoi mail is already being served by mongrel. Consult 'help'" elif [ "$maild_running" -eq 1 ]; then echo "ERR: Maild is already running. Consult 'help'." else echo " - Clearing PID and socket-files.." rm -f $INSTALLPATH/tmp/maild.pid rm -f $INSTALLPATH/tmp/sockets/maild.socket echo " - Starting maild Daemon.." su - $SUGOIUSER -c "$INSTALLPATH/bin/maild;exit;" echo " - Starting Mongrel webserver.." su - $SUGOIUSER -c "cd $INSTALLPATH; mongrel_rails start -d;exit;" #timeout for 3 sec for mongrel to boot, ensure correct status msg sleep 3s Status fi ;; status) Status "output" ;; cleanup) echo "Sugoi-mail cleanup.. [ started ]" echo "- stopping maild, mongrel, and cleaning up files.." if [ "$mongrel_running" -eq 1 ]; then su - $SUGOIUSER -c "cd $INSTALLPATH; mongrel_rails stop" fi if [ "$maild_running" -eq 1 ]; then kill -9 $maild_pid fi if [ "$maild_socket_exists" -eq 1 ]; then rm -f $INSTALLPATH/tmp/sockets/maild.socket fi if [ ! -z "$maild_pid" ]; then rm -f $INSTALLPATH/tmp/maild.pid fi Status ;; restart) if [ ! "$maild_running" -eq 1 ]; then echo "CAN'T RESTART: Maild belonging to $SUGOIUSER is not running. Use 'start'" elif [ ! "$mongrel_running" ]; then echo "CAN'T RESTART: Sugoi-Mail is not being served by Mongrel. Try 'cleanup'" else if [ -z "$maild_pid" ]; then echo "ERR: no PID file exists, but maild is running. Consult 'help'" else echo "Restarting Sugoi-Mail..." Menu stop echo "..Booting up again.." Menu start fi fi ;; stop) if [ ! "$maild_running" -eq 1 ]; then echo "ERR: Maild belonging to $SUGOIUSER is not running. Use 'start'" elif [ ! "$mongrel_running" ]; then echo "ERR: Sugoi-Mail is not being served by Mongrel. Consult 'help'" else if [ -z "$maild_pid" ]; then echo "ERR: no PID file exists, but maild is running. Consult 'help'" else echo " - Stopping Mongrel webservice.." su - $SUGOIUSER -c "cd $INSTALLPATH; mongrel_rails stop" echo " - Stopping maild daemon.." su - $SUGOIUSER -c "kill -2 $maild_pid" Status fi fi ;; *) echo "-- SUGOI-MAIL BOOT SCRIPT HELP -- INFO: This file is a boot script for Sugoi-Mail and should be located in /etc/init.d Please ensure the installpath and sugoi-user have been set properly: - Sugoi-mail installpath: $INSTALLPATH - Sugoi-mail user: $SUGOIUSER SYNTAX: sugoi-mail [ command ] COMMANDS: start If sugoi-mail is not running, 'start' will clean up PID & socketfiles, boot maild daemon, start Mongrel webserver, and finally print sugoi-mail program status. stop If sugoi-mail is running, 'stop' will stop Mongrel webservice, terminate the maild daemon, and print the sugoi-mail program status. restart If sugoi-mail is running, 'restart' will call 'stop', and then 'start' the behaviour of 'stop' and 'start' are described above. cleanup At any point in time 'cleanup' will restore sugoi-mail to a 'stopped' state. 'cleanup' does not check the current program state, harshly (kill -9) stops maild daemon, cleans up the PID and socketfiles manualy, stops Mongrel webservice, and then prints a status message help Display this help message * All other commands default to 'help' ERROR HANDLING: Most error states can be resoved with 'cleanup'. If maild is running, but the PID-file does not exist, you must kill maild manually by finding its process id in the 'ps -ef' listing AUTHOR: aavdacev@invio.co.jp " ;; esac } RunTests Menu $1 exit 0