croakingtoad Posted January 4, 2007 Share Posted January 4, 2007 I am trying to write a cron job to automate a task I can never remember to do.I need it to execute the following commands with variables. I started dabbling with it in PHP ... can someone help me fill in the gaps?[code]$month = date("M"); // I actually need this to be the previous month, so whatever the current month go back to the previous month$year = date("Y");$grep = shell_exec("grep -i '$month/$year' /usr/local/apache/domlogs/logfile > /home/roanoke/logs/logfile-$month-$year");*pause* // this is a big file and will need 10-15 minutes to process the grep command$gzip = shell_exec("gzip '/home/roanoke/logs/logfile-$year-$month'");*pause* // gzip will probably take 2-3 minutes$drop = ""; // I need to strip any line from the file /usr/local/apache/domlogs/logfile containing "$month/$year". Or could I use the already existing var $grep somehow from above to do that?[/code]All help is much appreciated! Quote Link to comment Share on other sites More sharing options...
AV1611 Posted January 4, 2007 Share Posted January 4, 2007 As far a setting up the cron goes, simply use this for the executable'PHP /path/to/file.php' Quote Link to comment Share on other sites More sharing options...
trq Posted January 4, 2007 Share Posted January 4, 2007 Honestly, if what your doing is within the Linux shell (which it is), why use php at all? Use bash.[code]#!/bin/bash$MONTH=$(date +'%m' --date='1 month ago')$YEAR=$(date +'%Y')$GREP=/bin/grep$GZIP=/bin/gzip$SED=/bin/sed$GREP -i ${MONTH}/${YEAR} /usr/local/apache/domlogs/logfile > /home/roanoke/logs/logfile-${MONTH}-${YEAR}$GZIP /home/roanoke/logs/logfile-${YEAR}-${MONTH}$SED -i -e "/${MONTH}\/${YEAR}/d" /usr/local/apache/domlogs/logfile[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.