views:

144

answers:

3

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  A: 

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
Svisstack
great solution!
never_had_a_name
then what my solution is not accepted?;-)
Svisstack
+1  A: 

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.

cletus
You can edit crontable by the user, but each user have independent self crontable.
Svisstack
@Swisstack yes but that's one user for your whole Web app (typically "nobody" on Linux).
cletus
+1  A: 

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.

nik