Scheduling tasks in PHP without cron - php

Is it possible to schedule a task with PHP? like, I want to choose a date with the jQuery datepicker, and submit it. When that date equals today's date, an email will be sent for example (or any other PHP script). The date will be different depending on the user, also, every user can have a number of scheduled tasks to run.
EDIT
The OS is Linux, and the website is hosted on godaddy.com
Also, it's the end users who will choose when to run these tasks, so they can use command lines for that.
EDIT
Any Ruby on Rails or Django solutions are welcomed as well.

If you're using a Unix server, try at.
http://www.manpagez.com/man/1/at/
It works in a similar way to cron, in that it's totally separate from PHP, and you need to shell_exec() an external command. However, it has a simple command-line interface. Like cron jobs, at jobs survive reboots.
If you use at you'll have to write a separate script to perform the task you want scheduled.
Example:
shell_exec("echo 'php script.php' | at -t 201208011234.56");
Will run script.php on August 1st, 2012 at 12:34:56.

You need some process in the server side to run the task at the scheduled time.
A popular approach is to store the task information in some database and have a cron job checking the task queue from time to time - it is very reliable and safe.
It is impossible to give a better answer without more information about the environment where it will be deployed. For example:
target OS
hosting type (dedicated, shared, cloud)
do you have administrator privileges?
is it exposed to the internet?
Most likely you will not be able to do that in a shared hosting environment, anyway, but I must say that having a website calling shell_exec is not very wise from a security standpoint, so I would avoid that if the site is exposed to the Internet.
A good hosting provider should have some kind of background task scheduler available, even if it is not crontab. If your hosting provider hasn't, trying to pull some stunt to fill the gap probably is a bad idea.
[update]
As I said in the edited part of the original post, I'm hosting it on godaddy.com, and I can't let the clients run these command lines etc. Is there a solution with other languages too?
I'm not hosting at godaddy anymore but they used to have a "Cron Manager" at their control panel. I think the correct goDaddy cron setup is
/usr/bin/wget -O - -q "http://yoursite.com/cron.php" > /dev/null 2>&1
at the Command Option in the Cron Manager. This "cron.php" will check the task queue to see if there are any email to send (the task queue being a simple table in your database where you will record any task scheduled by your customers).

You can do this without cron by simply adding a check into the main index page, or a standard include file - but that's only for when you really can't get access to cron (e.g. shared hosting). In this check you'd grab the list of outstanding actions and execute them, possibly by using
if (time_since_last_run > 5*60) // minutes
shell_exec("/usr/bin/php /var/www/check-tasks.php &");
The best solution is to use cron to launch a PHP script that will check for scheduled items within your system and execute them. You can either do this via wget or directly via calling PHP.
One shared hosting site that I have is externally scheduled from another of my servers via cron and wget http://othersite.example.com.
example crontab entry:
0 * * * * wget -O - -q -t 1 http://othersite.example.com/cron.php

Related

How to make the hosting server run specific PHP code in timely manner?

I'm new to PHP and web applications. I have PHP code which collects info, from 5 huge XML files provided by other websites into a private MySQL database.
These XML files are updated and changed over time, and I want my database to be refreshed every day at 3:00 AM.
Can I make the hosting server run the PHP code by itsself? How?
As Hobo Sapiens says:
Create a CRON Job
Put a shell script in one of these folders: /etc/cron.daily, /etc/cron.hourly, /etc/cron.monthly or /etc/cron.weekly based on how often you want the script to run.
The shell script should look something like this:
#!/bin/sh
php -q htdocs/file.php
For more specific timings take a look at How do I set up a Cron job?.
If you don't have SSH access to the server, you could also set up a cron job locally to ping the remote server using wget --spider or curl.
You can use CRON jobs. Cron will automate your commands on a specific time.
The software utility Cron is a time-based job scheduler in Unix-like computer operating systems.
Read more about CRON jobs

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/

Is cron job/scheduled task service enabled?

I'm making a website that need's a cron job or a scheduled task to send emails weekly but the problem is that I don't know if the host supports it...
This website is for an institution and I can't put it in any other host...
The host is using Apache.
If it doesn't support, how can I send weekly emails automatically, without changing host?
EDIT
I forgot to say that I'm new in cron jobs.
Apache has nothing to do with cron jobs as the system running on the host must trigger the job (which then can invoke a script running under apache).
Do you have SSH access?
Add the job to the /etc/crontab file or the cron-file of your user.
Or do you have a Webinterface to some management software (e.g. Plesk)?
Search there for an option for Cron jobs or Scheduled tasks.
If not you can use some external services which will call an URL on you site to trigger the job like http://www.setcronjob.com/.
First, ask your host if cron jobs are supported (they should be), check your panel (if there is one), try to set up one and see if it works etc.
If not, one possible way (other than to find an external service that will do the call to the script for you) is to add a function to your code that will be called every time a visitor of your site enters a specific page (e.g. the index). There, you will check if the weekday is the day that you want to send the e-mail. If yes, then send the mail, having a flag (e.g. a record in the db) to check if it has already been sent.
Of course it's not the ideal solution, all the others (the actual crons or an external service) are better, but since it is a solution, it's worth mentioning.
Have you read some documentation about cron-daemon?
This code is check the the cron is running
ps -ef | grep cron
After then you need to create a file which can handle the email address pickups and send th mails.
And also need to add something like this to the cron (it just an example send out something at every sunday, 23 o'clock):
0 23 ? * 0 php /path/sendnewsletter.php

How to send the mail automatically to respective user before a specific time?

I Have a system which is managing the document authorization on different hierarchy level, In this i need the system to automatically mail to the user for some reminder/notification before 3 days of expiry, I am using PHP MYSQL APACHE
How can I Achieve this?
make a script, and let cron job do the automatic email sending for you
Or you can use Swiftmailer
You can really only do this with Cron. If your server is a Linux host, you can schedule a PHP script (which you'll need to write) to run at a set time, e.g. every minute. Your script will check against the users database, select any users that are 3 days from expiry and mail them. Setting up cron is host-specific and your hosting provider might not even let you do it. Usually you'll have some server administration software (e.g. cPanel) that will let you schedule a cron job and specify which script to run. If you have SSH access, even easier - you can just SSH in and run crontab -e to set up a cron schedule yourself. There are plenty of cron tutorials on Google.
Check with your hosting provider if they allow scheduled activities.

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.

Categories