![Cron 作业不执行 php 文件和普通文件](https://linux22.com/image/1128312/Cron%20%E4%BD%9C%E4%B8%9A%E4%B8%8D%E6%89%A7%E8%A1%8C%20php%20%E6%96%87%E4%BB%B6%E5%92%8C%E6%99%AE%E9%80%9A%E6%96%87%E4%BB%B6.png)
我有两个文件需要每天晚上执行。
PHP 文件包含来自数据库的最新记录,并在同一目录中创建一个源文件。
而普通文件甚至无法执行。
我尝试登记入住/var/log/syslog
,发现如下。
日志档案
Jul 29 13:56:01 ocs2 CRON[7016]: (root) CMD (/opt/lampp/bin/php /opt/lampp/htdocs/ats/cron_jobs/export_db_elastic.php)
Jul 29 13:57:01 ocs2 CRON[7118]: (root) CMD (source /opt/lampp/htdocs/ats/cron_jobs/tt2)
Jul 29 13:57:01 ocs2 CRON[7117]: (CRON) info (No MTA installed, discarding output)
PHP 文件包含创建普通文件的代码
$db = mysqli_connect($host, $username, $password, $database) or die('Error connecting to DB : ' . mysqli_error($db));
$q1 = "select * from elasticsearch_record where id = 1";
$q2 = "select max(id) as `max_num` from resumes";
$r1 = mysqli_query($db, $q1);
$r2 = mysqli_query($db, $q2);
if ($r1->num_rows > 0) {
$rd1 = mysqli_fetch_array($r1);
$rd2 = mysqli_fetch_array($r2);
$start = $rd1['max_count'];
$end = $rd2['max_num'];
$one_up = $end + 1;
$q3 = "update elasticsearch_record set max_count = '$one_up' where id = 1";
$r3 = mysqli_query($db, $q3);
$string1 = "bin=/root/elasticsearch-jdbc-1.6.0.0/bin\n";
$string1 .= "lib=/root/elasticsearch-jdbc-1.6.0.0/lib\n";
$string1 .= "echo '{\n";
$string1 .= "\"type\" : \"jdbc\",\n";
$string1 .= "\"jdbc\" : {\n";
$string1 .= "\"url\" : \"jdbc:mysql://192.168.1.17:3306/c270751_resume\",\n";
$string1 .= "\"user\" : \"ServerUser\",\n";
$string1 .= "\"password\" : \"passwordhere\",\n";
$string1 .= "\"sql\" : \"select * from resumes limit $start, $end\",\n";
$string1 .= "\"index\" : \"rforceresumes\",\n";
$string1 .= "\"type\" : \"resumetype\"\n";
$string1 .= "}\n";
$string1 .= "}' | java \\\n";
$string1 .= "-cp \"\${lib}/*\" \\\n";
$string1 .= "-Dlog4j.configurationFile=\${bin}/log4j2.xml \\\n";
$string1 .= "org.xbib.tools.Runner \\\n";
$string1 .= "org.xbib.tools.JDBCImporter";
// echo "<pre>$string1</pre>";
$myfile = fopen("tt2", "w") or die("Unable to open file!");
fwrite($myfile, $string1);
fclose($myfile);
} else {
die("Do Nothing");
}
Elasticsearch批量导入的普通文件
bin=/root/elasticsearch-jdbc-1.6.0.0/bin
lib=/root/elasticsearch-jdbc-1.6.0.0/lib
echo '{
"type" : "jdbc",
"jdbc" : {
"url" : "jdbc:mysql://192.168.1.17:3306/c270751_resume",
"user" : "ServerUser",
"password" : "passwordhere",
"sql" : "select * from resumes limit 1, 222504",
"index" : "rforceresumes",
"type" : "resumetype"
}
}' | java \
-cp "${lib}/*" \
-Dlog4j.configurationFile=${bin}/log4j2.xml \
org.xbib.tools.Runner \
org.xbib.tools.JDBCImporter
现在,我觉得这些文件没有通过 CRON 执行,即使它显示在日志文件中。因为normal file
修改日期没有反映出来。
以下是 Cron Job 的代码
56 13 * * * /opt/lampp/bin/php /opt/lampp/htdocs/ats/cron_jobs/export_db_elastic.php
57 13 * * * source /opt/lampp/htdocs/ats/cron_jobs/tt2
我不知道,甚至没有想过。我花了很多时间验证我的代码。为什么没有正确执行。
笔记:
当我通过终端或浏览器手动运行它时,它运行完美。