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
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.