如何通过 PHP 脚本设置 cron 作业

2022-01-03 00:00:00 cron php

如何通过 PHP 脚本设置定时任务.

How can I set cron job through PHP script.

推荐答案

这将添加每天上午 9:30 运行的脚本.

This will add a script that runs every day at 9:30am.

exec('echo -e "`crontab -l`
30 9 * * * /path/to/script" | crontab -');

如果您从 Web 服务器运行此脚本,则可能会遇到权限问题.为了解决这个问题,我建议采用不同的方法.

You may run into problems with permissions if you are running this script from a web server. To get around this, I would suggest a different approach.

这是一种可能的解决方案.创建需要运行的脚本列表.您可以将其保存在文本文件或数据库中.创建一个脚本来读取此列表并每分钟或每 5 分钟运行一次(使用 cronjob).您的脚本需要足够智能,以决定何时运行脚本列表以及何时退出.

Here is one possible solution. Create a list of scripts that need to be run. You can save this in a text file or in a database. Create a script to read this list and run it every minute or every 5 minutes (using a cronjob). Your script will need to be smart enough to decide when to run the list of scripts and when to simply exit.

相关文章