Jump to content

setting up php cron job


croakingtoad

Recommended Posts

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!


Link to comment
https://forums.phpfreaks.com/topic/32865-setting-up-php-cron-job/
Share on other sites

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.