当前位置

编写PHP程序生成crontab定时用设置文件

James Qi 在 2017年8月31日 - 18:46 提交
内容摘要:《百度MIP版本链接的批量提交》中获取、整理URL是靠手工进行,设置crontab定时文件也是用手工编辑,这两项都很费时。昨天把第一项工作用程序实现了,详见《从XML网站地图中获取纯URL网址的PHP......

  《百度MIP版本链接的批量提交》中获取、整理URL是靠手工进行,设置crontab定时文件也是用手工编辑,这两项都很费时。昨天把第一项工作用程序实现了,详见《从XML网站地图中获取纯URL网址的PHP程序》,今天干脆把第二项工作也用程序来生成。

  直接贴PHP程序代码:


<?php
/*
 * create cron file for submit mip urls to baidu
 * jamesqi 2017-8-31
 *
*/

// please set below:

$start_month = 8;
$start_day = 31;
$start_hour = 22;
$start_minute = 00;
$next_time_minute = 5;

$php_script = '/alidata/server/php/bin/php';
//$submit_php_file = '/root/mip/submit_mip.php';//for Drupal
$submit_php_file = '/root/mip/submit_subdomain.php';//for MediaWiki
$submit_domain = "cn.mip.youbianku.com";
$submit_url_file = "/root/mip/cn.mip.youbianku.com.url.txt";
//$output_cron_file_name = "/root/mip/mip.$site.mingluji.com.cron.txt";//for test
$output_cron_file_name = "/etc/cron.d/cn.mip.youbianku.com.cron.txt";//for set cron

// please set above
// do not change below
ini_set('memory_limit', '1024M');

print "programe start\n";
print "start_month = $start_month\n";
print "start_day = $start_day\n";
print "start_hour = $start_hour\n";
print "start_minute = $start_minute\n";
print "next_time_minute = $next_time_minute\n";
print "php_script = $php_script\n";
print "submit_php_file = $submit_php_file\n";
print "submit_domain = $submit_domain\n";
print "submit_url_file = $submit_url_file\n";
print "output_cron_file_name = $output_cron_file_name\n";

$submit_url_file_content = file_get_contents($submit_url_file);
$submit_url_file_line_count = substr_count($submit_url_file_content,"\n");
print "submit_url_file_line_count = $submit_url_file_line_count lines\n";

$submit_times = ceil($submit_url_file_line_count / 2000);
$submit_days = ceil($submit_url_file_line_count / 10000);

$submit_count = 0;
$output = '';
$submit_month = $start_month;
$submit_day = $start_day;
for ($day_count=1; $day_count<=$submit_days; $day_count++) {
	$output .= "\n# day_count = $day_count : submit_month = $submit_month, submit_day = $submit_day\n\n";
	$submit_minute = $start_minute;
	$submit_hour = $start_hour;
	for ($i=1; $i<=5; $i++) {
		$submit_count++;
		if ($submit_count > $submit_times) break;
		$count_start = ($submit_count - 1) * 2000 + 1;
		$output .= "$submit_minute $submit_hour $submit_day $submit_month * root $php_script $submit_php_file $submit_domain $submit_url_file $count_start 2000\n";
		$submit_minute = $submit_minute + $next_time_minute;
		if ($submit_minute >= 60) {
			$submit_minute = $submit_minute - 60;
			$submit_hour++;
		}
	}
	if ($submit_month == 1 && $submit_day == 31) {
		$submit_month = 2;
		$submit_day = 1;
	} elseif ($submit_month == 2 && $submit_day == 28) {
		$submit_month = 3;
		$submit_day = 1;
	} elseif ($submit_month == 3 && $submit_day == 31) {
		$submit_month = 4;
		$submit_day = 1;
	} elseif ($submit_month == 4 && $submit_day == 30) {
		$submit_month = 5;
		$submit_day = 1;
	} elseif ($submit_month == 5 && $submit_day == 31) {
		$submit_month = 6;
		$submit_day = 1;
	} elseif ($submit_month == 6 && $submit_day == 30) {
		$submit_month = 7;
		$submit_day = 1;
	} elseif ($submit_month == 7 && $submit_day == 31) {
		$submit_month = 8;
		$submit_day = 1;
	} elseif ($submit_month == 8 && $submit_day == 31) {
		$submit_month = 9;
		$submit_day = 1;
	} elseif ($submit_month == 9 && $submit_day == 30) {
		$submit_month = 10;
		$submit_day = 1;
	} elseif ($submit_month == 10 && $submit_day == 31) {
		$submit_month = 11;
		$submit_day = 1;
	} elseif ($submit_month == 11 && $submit_day == 30) {
		$submit_month = 12;
		$submit_day = 1;
	} elseif ($submit_month == 12 && $submit_day == 31) {
		$submit_month = 1;
		$submit_day = 1;
	} else {
		$submit_day++;
	}
}

print "output = $output\n";
$output_cron_file = fopen("$output_cron_file_name", "w") or die("Unable to open file!");
fwrite($output_cron_file, $output);
fclose($output_cron_file);
print "please check the output file: $output_cron_file_name\n";
print "programe end\n";
?>

  生成的文件类似这样:

# day_count = 1 : submit_month = 8, submit_day = 31

0 21 31 8 * root /alidata/server/php/bin/php /root/mip/submit_subdomain.php mip.xizang.mingluji.com /root/mip/mip.xizang.mingluji.com.url.txt 1 2000
5 21 31 8 * root /alidata/server/php/bin/php /root/mip/submit_subdomain.php mip.xizang.mingluji.com /root/mip/mip.xizang.mingluji.com.url.txt 2001 2000
10 21 31 8 * root /alidata/server/php/bin/php /root/mip/submit_subdomain.php mip.xizang.mingluji.com /root/mip/mip.xizang.mingluji.com.url.txt 4001 2000
15 21 31 8 * root /alidata/server/php/bin/php /root/mip/submit_subdomain.php mip.xizang.mingluji.com /root/mip/mip.xizang.mingluji.com.url.txt 6001 2000
20 21 31 8 * root /alidata/server/php/bin/php /root/mip/submit_subdomain.php mip.xizang.mingluji.com /root/mip/mip.xizang.mingluji.com.url.txt 8001 2000

# day_count = 2 : submit_month = 9, submit_day = 1

0 21 1 9 * root /alidata/server/php/bin/php /root/mip/submit_subdomain.php mip.xizang.mingluji.com /root/mip/mip.xizang.mingluji.com.url.txt 10001 2000

  当数据量大的时候,这样方便了许多。

自由标签: