add php script in cron for scheduled task from php? - php

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)

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

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.

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.

Related

Created a cron job in wordpress But the logs are displayed only when someone visits the page

In wordpress, I have worked to create a cron job.Currently, The Cron job displays the logs only when somebody visits the page. But, i want it to run without any page visit. I know that can be done. But how , that is what i dont know. Please guide me friends. I have used the function wp_scheduled_event
This is normal behaviour. Wordpress crons are not server crons, they depend of PHP to be executed, so it'll run only if someone visit your website.
From wp_schedule_event documentation:
The action will trigger when someone visits your WordPress site, if
the scheduled time has passed.
You'll need a server cron if you want to get through this limitation. Depends of your hosting, you may be able to configure a cron through the manager, or if you have SSH access on your server (so if it's not a shared hosting), execute this command :
crontab -e
It'll open your crontab file. Add this line inside to add a cron job:
*/30 * * * * php-cli -f /path/to/your/php/script.php
Replace with the correct path to your php script. This command will execute the script every 30 minutes - change this to whatever you need (read more).
If you're on a shared hosting and your manager doesn't allow you to create cron jobs, you could maybe use an online service like this one to execute your script.

cron job on webserver

the last days I have researched about cron jobs. First I want to tell you abaout my problem.
I want to run a php script on my webserver every minute without loading the page. this php script inserts some data via yql. I read that cron is just working with unix/linux. my server runs on unix (wait for it). is it now possible for me to let my script run while sleeping in bed (computer off) just by the server side? do I got this all right? if yes, I also have to know how the path have to be look like in my command part in my schedule, because every example contents something like "* */1 * * * /usr/bin/wget http://www.example.com/cron.php", but why there is /usr/bin/wget in the path (that means, this cron is running just from my computer system not from the webserver) and how to put my schedule on the server and better where. my server have the root public_html where my index.php is inside. please give me an example of the schedule and how to fire my php data called "to_fire.php" that puts the data on my database via mysql.
I hope that some of you know what I exactly mean by own experience. im kinda confused by this command part and how to let it run from just the webserver and not my computer system.
thanks
wget is a unix command to visit a web URL. curl is another command working similarly. You could also use php path\to\script.php to run, as Gigawatt mentioned. There is no big difference from all of them
* */1 * * * is to set cron timing.
/usr/bin/wget is the location where wget on your server
Yes, once cron set, you can leave it alone. As long as server is living, the cron will run as scheduled. You can even set cron to send you an email, but that's no applicable to your case as running per min is to short.
Running cron per min seems not common. If you are doing something like realtime update, consider other solution.
Cron can be set on cPanel.
You need to config your cron script to accept request from same server only, to avoid others access this URL.
Some resource for cron
Configuring cron jobs in cPanel http://drupal.org/node/369267
Backup Databases using a cron http://wiki.lunarpages.com/Backup_Databases_using_a_cron
Configuring cron jobs using the cron command http://drupal.org/node/23714
Managing Cron Jobs with PHP http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/

How to schedule in php

I have some files on my server, how to open them programatically once a day?
Let them be
http://site.com/scripts/video.php
http://site.com/scripts/music.php
Without my hands, just like sheduling (automatically).
Even if I sleep and server is working, they should open on given time.
And additionally, how to open them once a 10 seconds (for tests)?
Thanks.
The Solution is very clear when you are using a Linux server;CRON JOBS.
One can easily run a cron job by configuring it through the terminal.I saw everyone has provided the Solution,but my answer will be for the people who are novice to Linux servers and don't know much about Cron Jobs.Go to Terminal and type the below commands..
root>which php
The above line will give you the path to where PHP is in your linux systems
Now,
root>crontab e
The above line will open the Cron file in edit mode.
Enter the number of times you want to run a particular php file and what time of the day,month,week,etc.
I am providing the syntex for running a particular file every 15 mins.
So here you go,
(write this in the cron file in edit mode)
*/15 * * * * path/to/your/php path/to/the/file/you/want/to/run
Now,path/to/your/php has to be replaced by the path what you got when you typed
root>which php
And you are done just save the file and close it.You will see a messege on you terminal that a new CronJob is installed.
That's it.
If you're on a Linux/Unix host using a cron job is generally the best approach, as you can simply call the command line version of PHP as a part of the cron job. (You may need to tweak your script if it relies on $_SERVER variables, that said.)
Administration middleware (such as Plesk) often offer the ability to add cron tasks as well, although you many need to check the user/group rights that such tasks are executed with.
Finally, if you use a cron task you can simply enter the required command via the command line during the testing phase. (i.e.: Rather than force a 10 second update (which would be tricky unless you had cron execute a shell script) you could execute the script as required.)
It's not possible with pure PHP. You'll need a cron job for this - ask your provider or administrator whether they are available.
Cron has a resolution of 1 minute, though: Calling a script once every 10 seconds would have to be done e.g. using a PHP script that gets called every minute, and makes six requests every ten seconds.
Running them once a day requires a seperate program running them.
For linux servers the usual choice is a Cron Job, for Windows the Task Sheduler works fine, too.

How to send on a specific day in php?

I want to a e-cards or something like that. The user can choose the e-cards, after chosen, he must enter the some fields like name(to and from), email(to and from), message and I want to let user to choose which date to send the e-cards.
How to send the e-cards on specific day? I need to write a script that run every new day? How to do that? Sorry, I am new to php... (but not beginner like not even know how to execute mysql query, get message from url etc)
Yes, you need a script that runs every day. (Barring ridiculous maneuvers like trying to fake this by checking on Web requests.) The usual way to do this in a Unix context is called a cron job; if your hosting provider is Unix-based, you should look into what they provide for making cron jobs available to you. On Windows there's a parallel service called Scheduled Tasks.
A similar question was disscussed here resetting-a-mysql-field-value-without-user-execution
I'll just reiterate: There are web based cron services too. This could come in handy if you only got a shared hosting plan and can't add cron jobs. They will call an URL at a regular interval that you can set. Usually very cheap. (Cheaper than upgrading to a root-access server anyway.)
Just search Google for web based cron
ciao!
/0
This is for *nix.
Let's say you have a php script that sends email on a specific day called mailer.php
<?php
//mailer.php
if (date("m/d/Y") == "06/02/2009") {
mail("client#email", "Subject", "Body");
}
?>
We are going to assume that you already have cron daemon running in the background.
If you have root access to your machine, then setting up a cron job is simple as editing a file.
Open up /etc/crontab file and add the following task:
1 14 * * * root php /path/to/your/scrip/mailer.php
This means, as a root, the mailer.php script will be running daily at 02:01PM. You can change the numbers to whatever you desire.
This is somehow complex. First it depends on your system. If it is Linux/BSD/Unix/Solaris then you have this handy utility as cron. If you are using Windows, you have Scheduled Tasks. Run your script daily (or as you wish) and check what cards you have to send today.

Resetting a MySQL Field value without user execution

I need to reset a MySQL Field value automatically at midnight. It is a specific column in a specific row in a table. I know how to do this in PHP but I do not know how to execute the PHP Script at midnight without someone having to do it themselves. Do you have any viable solutions?
Edit:--------------------
Preferably without using Cron Jobs.
If you are running on linux you would use a cronjob
On most distros there is a command called crontab that schedules tasks for you and a specific format you need:
0 0 * * * php /path/to/file.php
EDIT:
Wrote this before you edited yours :p I'm not sure there is any other way. Do you have a specific reason not to use a cronjob?
You may not even need to specify the php part if the php file is marked as executable i.e
0 0 * * * /path/to/file.php
Just do "chmod +x /path/to/file.php" form the linux command line
There are web based cron services too. Basically you set up an account and then they visit an URL of your choosing at a regular interval. On your server you set up a page that does what you need to get done. Preferably give it a unlikely-to-find-name like myjob789273634ahhh8s2nhw8sghusgf874wfu.php. You get the idea. (Remember that PHP-scripts timeout after like 30secs.)
Here's a google search:
**oops I'm new so I can't post URL apparently. Just search for "web based cron".
Good luck
/0
You could write a job scheduler into your program that runs jobs in a cron-like way. It would require a user to interact with the system to trigger, but it might be good enough depending on your needs. This is much more complicated than just running a cronjob, and does not ensure prefect timing (since it wont run until a user hits a page).
You'd probably need to add a table into you database that would list the job, the time you want them done, and a locking flag to avoid concurrent attempts to run the job. Each time your script runs, you'd check this table for overdue jobs and run them as needed.
Asking how to reliably set off a script at the same time every night without cron (or a scheduled task, on Windows) is like asking how to make a dynamic website without a server-side language.
If your app absolutely relies on a script running exactly at midnight, cron is a requirement. If your users have a hosting company that (stupidly) does not permit cron, they're going to be out of luck.

Categories