Tuesday, July 21, 2009

scron - A cron replacement

This piece below was designed a replacement to cron, because VPS's/VDS's were/are broken and eventually cron dies. So I coded up this installer with ideas and assistance from a co-worker for "Script Cron" or Scron to take the place of the broken vixie-cron and other crons.

Script:

#!/bin/bash

## SCron - When normal cron just isnt working ##
#
# Written by L.C. and R.P.
#

## This section builds all of scrons directories and scron.
echo "Generating directories for scron.. "
mkdir /usr/local/scron ;
chmod 700 /usr/local/scron ;
mkdir /usr/local/scron/jobs ;
chmod 700 /usr/local/scron/jobs ;
echo "Building Scron.. "
cat > /usr/local/scron/scron.sh <<\EOF
#!/bin/bash
#Scron - The 5 minute man
while [ 1 -eq 1 ]; do
for a in /usr/local/scron/jobs/*.sh; do
$a
done
sleep 300
done
EOF
chmod 700 /usr/local/scron/scron.sh ;
chattr +a +i /usr/local/scron/scron.sh ;
echo "Creating custom rc.local.. "
cat > /etc/rc.local <#!/bin/bash
# Scron custom RC.Local
screen -dmS loop /usr/local/scron/scron.sh
exit 0
EOF
## This section is for jobs optionally created upon install.
echo "Generating jobs that will run split_q and fail_restart... "
cat > /usr/local/scron/jobs/split_q.sh <#!/bin/bash
# Script to run split_q.php
php /usr/local/sendstudio/split_q.php
EOF
chmod 700 /usr/local/scron/jobs/split_q.sh
cat > /usr/local/scron/jobs/failed_restart.sh <#!/bin/bash
# Script to run fail_restart.php
php /usr/local/sendstudio/cron/fail_restart.php
EOF
chmod 700 /usr/local/scron/jobs/failed_restart.sh ;
## Patch fail_restart.sh so it will run cli send regardless of
## screen sessions.
cat > /usr/local/sendstudio/cron/fail_restart.sh <<\EOF
#!/bin/bash
screen -ls |grep send > /dev/null
if [[ $? == 0 ]]; then
screen -S send -d -m php /usr/local/sendstudio/clisend.php
fi
EOF
## since we have some custom cron jobs initially installed with
## our production installer we remove them.
echo "copying the maint script.. "
cp /usr/local/sendstudio/maint.sh /usr/local/scron/jobs/
echo "removing old cron.. "
rm -rf /etc/cron.d/obey
## Reboot at the end - optionally you could just run the same
## command in rc.local.
echo "Going down for a reboot"
init 6 ;

No comments: