linux - PHP, crontab, DateInterval: Convert human-readable interval to crontab format -
i'm having little confusion crontab interval format. point want intervals human-readable strings "20 minutes", "16 hours , 30 minutes". done php datetime already. need passing crontab-valid string exec(sprintf('echo "%s %s %s * * %s" | crontab', $minute, $hour, $day, $command));
. anyhow, here sample php script
<?php function getcrontabinterval($timestring) { $interval = dateinterval::createfromdatestring($timestring); $minute = $interval->i > 0 ? "*/{$interval->i}" : '*'; $hour = $interval->h > 0 ? "*/{$interval->h}" : '*'; $day = $interval->d > 0 ? "*/{$interval->d}" : '*'; $crontab = sprintf('echo "%s %s %s * * %s" | crontab', $minute, $hour, $day, '%command%'); echo "days:\t\t", $interval->d, "\n", "hours:\t\t", $interval->h, "\n", "minutes:\t", $interval->i, "\n", "command:\t", $crontab, "\n"; } getcrontabinterval($_server['argv'][1]);
and output:
serge@serge-laptop:~/www/bin$ php periodical.php '2 hours 25 minutes' days: 0 hours: 2 minutes: 25 command: echo "*/25 */2 * * * %command%" | crontab
so, "*/25 */2 * * *" cron value match running command each 2 hours 25 minutes? or should "0/25 0/2 * * *"? not clear me manpages. , how act days?
upd: under each "2 hours 25 minutes" mean running @ 0:00, 2:25, 4:50, 7:15 , etc
solution: used two-component value intervals manual recommendation use values 20 minutes, 3 hours, 4 days , etc.
*/25 */2 * * *
means @ 0:00, 0:25, 0:50, 2:00, 2:25, 2:50, 4:00, 4:25, 4:50 etc...
25 */2 * * *
means 0:25, 2:25, 4:25, 6:25, etc...
0/25 0/2 * * *
means 0:25 only
edit - after update
to knowledge there no way single line in crontab. mechanism specifying @ time cron job runs not meant kind of complexity. can have 9 or 10 entries in crontab different times same script if need 2h25 functionality
Comments
Post a Comment