Jump to content

PHP Script as Cron Alternative


bschultz

Recommended Posts

I'd currently work for a radio station.  On a daily and weekly basis we need to download certain files (mp3) for playback later.

 

I currently have about 50 cron jobs scheduled to download the files at a given time or day.  I'd like to consolidate these files and jobs.

 

I'd like to have one cron job set to run every minute to execute a php script that will do the download if the time is a database is correct.

 

How can I write the logic of the if statements in the php script to follow similar logic as cron (0 10 * * *) like 10am every day?

 

I have the datatype in mysql set as varchar so that I can include the * wildcard.

 

Here's what I've come up with.  Is there a "better" way to accomplish this?

 

$run = 0;
 
if ($min == $current_minute || $min == '*') { $run = 1; }
if ($hour == $current_hour || $hour == '*') { $run = 1; }
if ($dom == $current_dom || $dom == '*') { $run = 1; }
if ($mon == $current_mon || $mon == '*') { $run = 1; }
if ($dow == $current_dow || $dow == '*') { $run = 1; }

if ($run = 0) { exit; )
elseif ($run == 1) { execute rest of script here }

Link to comment
Share on other sites

There is no way for PHP to execute a file, the server has to execute files, the PHP parser is just waiting for the server to send it a file to parse.  What you are asking is what the Cron Daemon was designed to do.

However, instead of having 50 Cron's running, just create a PHP file to do what you need, then tell Cron to run that file every minute.

Link to comment
Share on other sites

So I think I've understood this as the following.

 

Current situation: you have 50 crons in your crontab you want to consolidate into one.

 

If so, something like this could work.

 

Single cronjob that runs every minute: * * * * * /usr/bin/php -q /path/to/cron.php

 

cron.php sample contents:

 

// this comes from either variables or a database

$minute = 0;
$hour = '*';
$day = '';
$month = '';

// if nothing below matches, we won't do anything
$doirun = FALSE;

if(date("i") == $minute || $minute == '*')
{
     $doirun = TRUE;
}

if(date("H") == $hour || $hour == '*')
{
     $doirun = TRUE;
}

// and repeat for day, month, week, etc to be as flexible as you want

// now we execute

if($doirun)
{
      # do your cron action here
}

 

Time Reference: http://www.php.net/manual/en/function.date.php

Edited by monkeypaw201
Link to comment
Share on other sites

Monkeypaw...that's correct, 1 cron job running 1 script.

 

I've found a problem in both of our logic, though. 

 

if(date("i") == $minute || $minute == '*')
{
$doirun = TRUE;
}

if(date("H") == $hour || $hour == '*')
{
$doirun = TRUE;
}

 

If the download should occur at 0:10 of each hour...hour will be * in the database...meaning that it would execute every minute (date("H") == $hour || $hour == '*') when the cron job runs...since hour = *

 

How can I account for all of the different if statements that will be needed to act in a similar fashion as cron?

Link to comment
Share on other sites

Thanks...

 

by the way, here's the current code snipit...with column names...

 

mysql_select_db($db,$dbc);
$sql = "SELECT * FROM downloads";
$rs = mysql_query($sql,$dbc);  
while($row = mysql_fetch_array($rs))

{
$row = $row['row'];
$name = $row['name'];
$min = $row['min'];
$hour = $row['hour'];
$dom = $row['dom'];
$mon = $row['mon'];
$dow = $row['dow'];
$type = $row['type'];
$filetype = $row['filetype'];
$url = $row['url'];
$username = $row['username'];
$password = $row['password'];
$local_path = $row['local_path'];
$remote_path = $row['remote_path'];
$a1037 = $row['a1037'];
$a983 = $row['a983'];
$a921 = $row['a921'];
$a1360 = $row['a1360'];
$a1300 = $row['a1300'];
 
 
// rest of code here
}

Edited by bschultz
Link to comment
Share on other sites

Couldn't you handle it in the query?

SELECT whatever FROM table
WHERE ( min = '*' OR min = MINUTE() )
AND ( hour =  '*' OR hour = HOUR() )
AND ( dom = '*' OR dom = DAYOFMONTH() )
AND ( mon = '*' OR mon = MONTH() )
AND ( dow = '*' OR dow = DAYOFWEEK() )
Although, since you are using varchar, you will have to watch out for the two-digit vs. one-digit values -- i.e. is '02' == 2? Probably not!

 

As a long-time programmer, I would probably make those TINYINT (and leave them signed). Then use -1 instead of '*' for the "always" flag. On the other hand, cron lets you do things like ranges and lists (I believe), which this solution will not support.

Link to comment
Share on other sites

Rather than try and re-implement cron, I'd just look at making an easier to use/manage interface on top of cron. A few php scripts that would list out all the cron jobs in a nice form, let you make any changes, and the save them back into cron.

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.