add php script in cron for scheduled task from php?

一曲冷凌霜 提交于 2020-01-06 06:41:08

问题


is there a way to add a php script (file) in cron for running this script every ten minutes or at a scheduled time?

cause i want the user to be able to schedule when to send newsletter to a lot of emails he choses.

so i have to create a cron job from php to run a php file on that scheduled time.

is this possible if you have a shared web hosting environment (not vps)


回答1:


If you script filename is news.php and in /home/user/news.php crontab line seems like to be:

* * * * * php /home/user/news.php

If you want dont run this in every minute. You can edit * with from left (m, h, dom, mon, dow)

But you cant do this if only you have same web hosting, you must have access to shell or other way to configure your crobtab file (maybe from your provider access panel)

But you can run crontab job on other server to run your news.php by the apache over http protocol. In this option your crontab job on remote server must run your script by the web. Eg. wget is a good option for it:

* * * * * wget http://www.yourdomain.com/dir/news.php



回答2:


My advice is don't allow a PHP script invoked by Apache to alter the cron. That's got disaster written all over it. Normally cron is setup to require root permision. That can be changed but the point is that if your site does get hacked, giving an attacker the ability to modify your cron could be really really bad.

More to the point, it's not necesary. All you do is pick some granularity, say every minute or 5 minutes or 10 minutes. You say to the users that they can schedule it down to that level. Give them some options from every month to every 5 minutes. Whatever they choose, write it to the database.

Then run a different PHP script every 1, 5 or 10 minutes (whatever the minimum granularity is) and have it look at the database to see if there is anything to run, do or send out.




回答3:


Yup It is possible but on shared hosting it will affect your as well as other websites on that host.

So if ur file is not called often by cron then it is ok to put cron on shared host, but if You have to call it often then it is not advisable, u can also be thrown out by your provider since nobody wants there site to under-perform because of other website.



来源:https://stackoverflow.com/questions/2666671/add-php-script-in-cron-for-scheduled-task-from-php

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