How can I easily and simply schedule a cron job in PHP? Rails has BackgroundRB...
Most website control panels (assuming you've got cPanel or something similar running) include a crontab application. If you're on shared hosting ask your host about this.
If you're on a dedicated server and have installed cron then have a look at the crontab syntax. These commands go in crontab, usually in /etc on *nix.
Here's a semi-PHP solution to add to a crontab:
$cmd = 'crontab -l > /tmp/crontab.bak'; // preserve current crontab
$cmd .= ' && echo "*/5 * * * * /foo/bar" >> /tmp/crontab.bak'; // append new command
$cmd .= ' && crontab /tmp/crontab.bak'; // update crontab
$cmd .= ' rm /tmp/crontab.bak'; // delete temp file
exec($cmd); // execute
There is PHP-Resque, a PHP port of the queue&background process framework written by GitHub guys.
I recommend http://www.phpjobscheduler.co.uk/
You're conflating a language with a framework. PHP doesn't have a cron scheduling any more than Ruby does. If you're using a PHP framework or cms however, there is likely some utility for cron tasks.
Here is a useful link if you have control over the machine.
http://troy.jdmz.net/cron/
If you have shared hosting, there's probably some tool they'd give you for cron jobs; ask them or look in the knowledge base.
Related
I have to work with cron job and i read something about that like
* * * * * /usr/bin/wget http://blablaba/cron.php
(i know how set time m ex. run every minute).
My question is where i have to put that code?
in php index?
like:
You should put all cron jobs into crontab with
crontab -e
As far as I know it's the same for all Linux distros and Mac OS. As for Windows, just save yourself a trouble and go for Vagrant virtual machine
I usually put this command in WEBMIN.
Ask your hosting, where do you need to configure crons jobs.
For example in CPANEL:
You can't edit it with PHP directly.
You need to add new cron jobs using console command:
crontab -e
or add new files to the /etc/cron.d directory.
Best if you create a new file under /etc/cron.d directory which is where you should put your own cron jobs.
I am using the simple_html_dom script to parse a value from a website.
My code:
<?php
include('simpleparser/simple_html_dom.php');
$html = file_get_html('http://www.example.com');
foreach($html->find('strong') as $e) // the tag that I am fetching
echo $e->innertext ;
?>
Now, I'd like to run this only once per day as the data, I am parsing updates only once every day.
I've read a couple of articles about the cron task, but can not get it to work. The examples seem to overcomplicate things and are not relevant to my case.
My hosting plan has the cron scheduler disabled and no shell access and I don't know how else to set it up.
To create cronjob from the command line use:
#write out current crontab
crontab -l > mycron
# echo new cron into cron file | runs everyday at 22h
echo "* 22 * * * php /full/path/to/script.php" >> mycron
#install new cron file
crontab mycron
rm mycron
To create a cronjob using Parallels Plesk Panel, take a look at this answer
UPDATE:
I have no shell access and cronjob disabled in my Plesk Panel.
Use a online cronjob https://www.setcronjob.com/
If there's really no way for you to setup a cron job on your hosting - you could also use some online service to trigger your script once in a while.
For example https://cron-job.org seems to do what you need
Attaching a sample of settings they provide
You can contact with your hosting server but temporary solution for this kind of problem is to use cronjob service there are lot of free cronjob service out there in web. you can try those service .
I used this kind of service while creating a DDoS bot .. :p ..
You can use these cronjob service but there are more ...
https://www.setcronjob.com/
https://www.easycron.com/
search in google with "Cron job service" you'll find thousands of service like this
Happy coding :)
I use windows task-schedule. To execute a .php script you can either insert it like this or create a .bat file and execute it through there.
This is my first contact with cron jobs, so I'm sorry if my question sounds dumb.
BackWPup is for making a back up automatically after some period of time, but its own cron job does not work correctly. It starts only when I sign into wp-admin. So I decided to use the server's cron jobs, but I don't know how to. It says:
If you would use the cron job of your hoster you must point it to the
url: http://example.com/wp-cron.php
Also, I want to know how to remove a job.
Note: I have only ssh access, there is no hosting control panel. OS: CentOS.
I guess you should add the command
wget http://example.com/wp-cron.php >> /path/to/my/wp-cron.log 2>&1
to the crontab. Of course you can use any other CLI http-tool instead of wget, but it's the most simple I know and I think is sufficient here.
Call
crontab -e
then add a line like
0 * * * * wget http://example.com/wp-cron.php >> /path/to/my/wp-cron.log 2>&1
This will call this command every hour. For further information see man crontab.
I am using Ubuntu server, and I want to do a wget cron job for just about every day of the week for different files.
I have gotten this to work for only one task, but anytime I try to do more it automatically overwrites the old one. I know how to set up times, and the format, etc; but I do not know how to do multiple wget cron jobs.
This is how I've been doing only one so far:
echo "*/10 * * * 5 wget http://XXX.XXX.XXX/files/thursday.php" | crontab -
Can anyone help me? Thanks
best to use the command line crontab function for maintaing cron jobs
crontab -e
will bring up the editor.
The default on most *nix system is vi, which is not newbie friendly, but you can change it to nano or pico with
export EDITOR=nano
and if your on a system like mine, your logged in user may not be the best user to run cron jobs as; so you may may have to use su to switch users before editing the crontab file.
looking at what you are specify doing, unless you really need to go through appache, you can just call the php file like so "php file.php" no wget needed.
*/10 * * * 5 php FULL_PATH/files/thursday.php > /dev/null 2>&1
How to set up a cron job via PHP (not CPanel)?
Most Linux systems with crond installed provides a few directories you can set up jobs with:
/etc/cron.d/
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
...
The idea here is to create a file in one of these directories. You will need to set the proper permissions/ownership to those (or one of those) directories so that the user launching the PHP script can write to it (Apache user if it's a web script, or whatever CLI user if CLI is used).
The easiest thing is to create an empty file, assign proper permission/ownership to it, and have the PHP script append/modify it.
Per example:
$ touch /etc/cron.d/php-crons
$ chown www-data /etc/cron.d/php-crons
Then in PHP:
$fp = fopen('/etc/cron.d/php-crons', 'a');
fwrite($fp, '* 23 * * * echo foobar'.PHP_EOL);
fclose($fp);
If what you're getting at is dynamically adding lots of jobs to crontab form your application, a better way to do that is manually add ONE cron job:
php -f /path/to/your/runner.php
Store your jobs that you would be adding to cron manually in a table (or one table per task-type), and then have your runner go through the table(s) every minute/hour/day/whatever and execute all the ones that should be executed at that time.
From pure PHP I will create deamon that will manage this (those) cron job(s).
how to create it:
http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/ to start with
Finding crontab file isn't easy on shared hosting and there's no certainty that cron will read that file again while it's already running.
Actually I the best way is to use corntab command.
If you don't have access to shell you can use for example PHPShell. Try this.
Uplode a txt file via FTP with jobs in crontab fomat for example
5 * * * * /some/file/to/run.sh > /dev/null
(remember to put a newline at the end of that line)
Log in to your PHPShell and run
crontab uploded_filename.txt
Remember to change file permissions
chmod 775 uploded_filename.txt
Check your cron jobs using
crontab -l
Cheers
There is an embargo on the use of PHP to edit crontabs which has been in place since 2004. You may not be allowed to do this if you live outside of the United States, check with your local government agency.
But seriously, you could always call "crontab -" with a system call. If you need to do this for some user other than the webserver, you'll need some ssh or sudo magic. But it all seems like a bad idea.