bschultz Posted March 19, 2013 Share Posted March 19, 2013 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 } Quote Link to comment https://forums.phpfreaks.com/topic/275882-php-script-as-cron-alternative/ Share on other sites More sharing options...
jcbones Posted March 19, 2013 Share Posted March 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275882-php-script-as-cron-alternative/#findComment-1419693 Share on other sites More sharing options...
monkeypaw201 Posted March 20, 2013 Share Posted March 20, 2013 (edited) 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 March 20, 2013 by monkeypaw201 Quote Link to comment https://forums.phpfreaks.com/topic/275882-php-script-as-cron-alternative/#findComment-1419733 Share on other sites More sharing options...
bschultz Posted March 20, 2013 Author Share Posted March 20, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/275882-php-script-as-cron-alternative/#findComment-1419879 Share on other sites More sharing options...
monkeypaw201 Posted March 20, 2013 Share Posted March 20, 2013 Good catch! So we may need to do some nested queries to get this to work properly. Let me mull on it over lunch and I'll get back to you with some ideas. Quote Link to comment https://forums.phpfreaks.com/topic/275882-php-script-as-cron-alternative/#findComment-1419883 Share on other sites More sharing options...
bschultz Posted March 20, 2013 Author Share Posted March 20, 2013 (edited) 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 March 20, 2013 by bschultz Quote Link to comment https://forums.phpfreaks.com/topic/275882-php-script-as-cron-alternative/#findComment-1419884 Share on other sites More sharing options...
DavidAM Posted March 20, 2013 Share Posted March 20, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275882-php-script-as-cron-alternative/#findComment-1419915 Share on other sites More sharing options...
kicken Posted March 20, 2013 Share Posted March 20, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275882-php-script-as-cron-alternative/#findComment-1419941 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.