问题描述
您好,我正在做一个cron状态页面,显示cron运行的脚本上的所有状态信息。我知道如何建立显示状态信息,但我需要帮助和建议以下...
Hi I am making a cron status page that shows all the status info on scripts run by cron. I know how to build the "showing status info" but I need help and suggestion on the following...
我需要允许用户看到状态页有手动启动cron脚本的权力。像一个按钮,它会调用cron来运行一个特定的脚本,如现在。
I need to allow a user seeing the status page to have the power to kickoff a cron script manually. Like a button that would call the cron to run a specific script like "now".
这是可能还是有一个解决方法我可以做吗?
Is that possible or is there a workaround I can do?
请帮助并提前感谢。
推荐答案
您可以手动执行cron脚本:
You can manually execute the cron script:
exec("cron script here");
如果需要,可以获取可用的cron脚本(未测试):
If you want, you can get the available cron scripts (untested):
$crontab = file_get_contents("path/to/cron");
$cron_jobs = array_filter(explode(PHP_EOL, $crontab), function($cron_job) {
return $cron_job[0] !== "#"; // Remove comments.
});
$cron_jobs = array_map(function($cron_job) {
$fields = explode(" ", $cron_job);
$fields = array_splice($fields, 0, 5); // Get rid of timing information.
return implode(" ", $fields);
}, $cron_jobs);
$cron_script = $_GET["cron_script"];
if ($cron_script < sizeof($cron_jobs)) {
exec($cron_jobs[$cron_script]);
}
这篇关于PHP:如何手动/编程启动cron脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!