#!/bin/sh # virtualcron Start virtual cron daemons # # chkconfig: 345 99 10 # description: Virtual site cron daemon service RETVAL=0 OK="echo -en \\033[1;32m" FAIL="echo -en \\033[1;31m" END="echo -en \\033[0;39m" UP="echo -en \\033[1;32m" DOWN="echo -en \\033[1;31m" END="echo -en \\033[0;39m" # See how we were called. case "$1" in start) echo "Starting virtualcron for ALL sites" ( cd /home/virtual for domain in *.* do [[ ! -d ./$domain ]] && continue if [[ -x /home/virtual/$domain/usr/sbin/crond ]] then $0 domain-start $domain fi done ) ;; domain-start) domain=$2 site=$(awk '$1 == "'"$domain"'" {print $3;exit}' \ /etc/virtualhosting/mappings/domainmap) ison=$(ls -ald /home/virtual/* | \ egrep "$site/fst"'$' | fgrep -v virtual/admin) if [ "x$ison" = "x" ] then echo "$domain not active." exit fi pid="$(egrep "r-xp.*virtual/$site/fst/usr/sbin/crond" \ /proc/*/maps 2>/dev/null | awk -F/ '{print $3}')" if [ "x$pid" = "x" ] then /usr/sbin/chroot /home/virtual/$domain /usr/sbin/crond sleep 1 pid="$(egrep "r-xp.*virtual/$site/fst/usr/sbin/crond" \ /proc/*/maps 2>/dev/null | awk -F/ '{print $3}')" else status="previously started - $pid" fi output=$(kill -0 $pid 2>&1 | sed -e 's/.*kill://') if [ "x$output" = "x" ] then color=$OK if [ "$status" = "" ] then status="started - $pid" fi else color=$FAIL status=failed fi printf "%-40s " $domain $color echo -n "[$status] " $END echo "$output" ;; stop) echo "Stopping virtualcron for ALL sites" ( cd /home/virtual for domain in *.* do [[ ! -d ./$domain ]] && continue $0 domain-stop $domain done ) ;; domain-stop) domain=$2 site=$(awk '$1 == "'"$domain"'" {print $3;exit}' \ /etc/virtualhosting/mappings/domainmap) pid="$(egrep "r-xp.*virtual/$site/fst/usr/sbin/crond" \ /proc/*/maps 2>/dev/null | awk -F/ '{print $3}')" nopid=0 if [ "x$pid" = "x" ] then ison=$(ls -ald /home/virtual/* | \ egrep "$site/fst"'$' | fgrep -v virtual/admin) if [ "x$ison" = "x" ] then continue else nopid=1 status="previously stopped" fi fi if [ $nopid = 0 ] then output=$(kill $pid 2>&1 | sed -e 's/.*kill://') fi if [ "x$output" = "x" ] then color=$FAIL if [ "$status" = "" ] then status="stopped - $pid" fi else color=$FAIL status=failed fi printf "%-40s " $domain $color echo -n "[$status] " $END echo "$output" ;; status) cd /home/virtual for site in site* do domain=$(awk '$3 == "'"$site"'" {print $1;exit}' \ /etc/virtualhosting/mappings/domainmap) base="$(egrep "r-xp.*virtual/$site/fst/usr/sbin/crond" \ /proc/*/maps 2>/dev/null | \ sed -e 's/^.* //' -e 's#/fst.*##')" if [ "x$base" = "x" ] then ison=$(ls -ald /home/virtual/* | \ egrep "$site/fst"'$' | fgrep -v virtual/admin) if [ "x$ison" = "x" ] then continue else status="stopped" color="$DOWN" fi else pid="$(egrep "r-xp.*virtual/$site/fst/usr/sbin/crond" \ /proc/*/maps 2>/dev/null | awk -F/ '{print $3}')" status="running - $pid" color="$UP" fi printf "%-40s " $domain $color echo -n "[$status]" $END echo done ;; activate) domain="$2" echo -n "Activating $domain: " site=$(awk '$1 == "'"$domain"'" {print $3;exit}' \ /etc/virtualhosting/mappings/domainmap) if [[ "x$site" = "x" ]] then $FAIL echo " invalid domain $domain specified" $END exit 1 fi ison=$(ls -ald /home/virtual/* | \ egrep "$site/fst"'$' | fgrep -v virtual/admin) if [ "x$ison" = "x" ] then $FAIL echo "[failed]: $domain not active or does not exist." $END exit fi SITECT=/home/virtual/FILESYSTEMTEMPLATE/siteinfo/usr/bin/crontab SITECD=/home/virtual/FILESYSTEMTEMPLATE/siteinfo/usr/sbin/crond if [[ ! -x $SITECT ]] then echo -n ' (adding /usr/bin/crontab to FIELSYSTEMTEMPLATE) ' cp /usr/bin/crontab $SITECT chown root:root $SITECT chmod 4755 $SITECT fi if ! ln $SITECT /home/virtual/$domain/usr/bin/crontab then $FAIL echo "[failed]: see error output" $END exit fi if [[ ! -x $SITECD ]] then echo -n ' (adding /usr/sbin/crond to FIELSYSTEMTEMPLATE) ' cp /usr/sbin/crond $SITECD chown root:root $SITECD chmod 755 $SITECD fi if ! ln $SITECD /home/virtual/$domain/usr/sbin/crond then $FAIL echo "[failed]: see error output" $END exit fi $OK echo "[OK]" $END $0 domain-start $domain ;; deactivate) domain=$2 $0 domain-stop $domain echo -n "Removing crond for $domain: " /bin/rm /home/virtual/$domain/usr/sbin/crond echo done echo -n "Removing crontab for $domain: " /bin/rm /home/virtual/$domain/usr/bin/crontab echo done ;; restart|reload) $0 stop $0 start RETVAL=$? ;; *) echo "Usage: virtualcron {start|stop|domain-start|domain-stop|restart|status}" exit 1 esac exit $RETVAL