how to set cron job from php script?

百般思念 提交于 2019-12-03 03:07:35
Javascript Coder

From How can I set cron job through PHP script by Nick Clark:

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

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

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.

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.

You can do like this..

Other links:

Install a cron job with a php script

How to invoke cron job from php script?

You should be able to add it directly

echo "2 * * * * /usr/bin/php /var/www/PATH TO THE SCRIPT/crontest1.php\n" > /etc/cron.d/username

Many environments do not permit writing to a text file from a php page called from the web. I agree that this is a security risk that one should not take.

I have a webpage save data to a MySQL table via a JQuery call to a php script. In Cron, I schedule another php script to, every so often, check data in MySQL table and execute whatever jobs necessary.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!