简体   繁体   English

如何在不使用exec或系统功能的情况下运行cron作业?

[英]How to run a cron job without using exec or system function in PHP?

Gist 要旨

I am trying to run a cron job in PHP. 我正在尝试在PHP中运行cron作业。 But the exec and system functions didn't work. 但是execsystem功能不起作用。 (Because the server administrator doesn't allow me to use these two functions due to the security reasons) However, I still need to use PHP to run it, because the running time is decided by the formula and it's uncertain. (由于安全原因,服务器管理员不允许我使用这两个函数)但是,我仍然需要使用PHP来运行它,因为运行时间由公式决定,并且不确定。 As a result, how can I run the cron job without using exec or system function in PHP? 结果,如何在不使用exec或PHP system函数的情况下运行cron作业?

My thought 我的想法

The only way I have come up with is adding the cron job via the cPanel, which is set to “once per minute.” That means setup a cron job for every minute to check the target PHP page whether it has anything to do at that moment. 我想出的唯一方法是通过cPanel添加cron作业,该作业设置为“每分钟一次”。这意味着每分钟设置一次cron作业,以检查目标PHP页面是否有任何工作要做。时刻。

Issue 问题

There's a possible issue if it always checks the page every minute per day, won't it damage the host? 如果它总是每天每分钟检查一次页面,可能会出现问题,这会损坏主机吗? (Maybe it will cause a damage to CPU or maybe occupy the memory space, etc.) If so, how to improve it? (可能会损坏CPU或占用内存空间,等等。)如果是这样,如何改进它?

You can call a bash script containing a call to PHP-CLI and then the file: 您可以调用包含对PHP-CLI的调用的bash脚本,然后调用文件:

#!/usr/bin/php    

    /path/to/my/php/file.php

Go to the command line of the server and type "which php". 转到服务器的命令行,然后键入“哪个php”。 Replace /usr/bin/php above with that which is returned. 将上面的/usr/bin/php替换为返回的内容。 Then you call this bash script in your CRON. 然后,在CRON中调用此bash脚本。

As you say you have a cpanel server, im guessing its a linux box. 正如您所说的,您有一个cpanel服务器,我猜它是一个Linux机器。 You run a cronjob from a linux box by typing crontab -e and then to run a php script. 通过键入crontab -e从Linux框中运行cronjob,然后运行php脚本。 */5 * * * * /usr/bin/php /path/to/file.php to un it every 5 minutes. */5 * * * * /usr/bin/php /path/to/file.php每5分钟取消一次操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM