Running shells as repeating cronjobs

Standard

It is common to use shells to handle background tasks on your server. Sometimes that task needs to be executed as quickly as possible. For example, you have a queue of tasks that need to process as soon as entered.

CakePHP can be configured on Unix servers to run a modified shell script via crontab.

Begin by modifying the defaultShell that comes with cake. I have created a new script call secondShell.

#!/bin/bash
TERM=dumb
export TERM
cmd="cake"
while [ $# -ne 0 ]; do
   if [ "$1" = "-cli" ] || [ "$1" = "-console" ]; then
      PATH=$PATH:$2
   shift
   else
      cmd="${cmd} $1"
   fi
   shift
done
COUNT=0
while [ $COUNT -le 30 ]; do
   $cmd
   let COUNT=COUNT+1
   sleep 2;
done

The modifications make this shell run ever 2 seconds for 30 occurrences (2 x 30 = 60 seconds)

To run this shell place it in the crontab by using command “crontab -e” in terminal.

*/1     *       *       *       *       /(your path)/app/Lib/Shells/secondShell YourShell -cli /usr/bin -console /path/to/your/app/Console -app /(your path)/app >/dev/null 2>&1

The */1 tells the scheduler to run the CakePHP Shell script every minute. Learn more about cron job scheduling

Leave a Reply

Your email address will not be published.