How to develop a schedule task system like the one used by Wordpress to schedule post? Do they use Cron Job? If they use it, how they configure it through PKP script?
As far as I know, WordPress just sets the publish date to some time in the future. It doesn't actually cause an event to happen in the future, it just looks that way when someone views the site.
You'd need a cron job if you actually wanted to do something in the future, web servers only respond to requests made by user agents.
Use cron and in iterations get run php_cli interpreter with your script.
I use php cron tasks, they look like the following
0 0 * * * * php -f /var/www/public_html/mycronphpfile.php
This means, at midnight, every day, run the command line php and execute the specified file.
Related
I was wondering how to make a php daemon script that runs one time at the day?
Do you know any good frameworks with benefits?
or is it just small code?
Thanks
I was wondering how to make a php deamon script that runs one time at
the day?
In order to do this, get familiar with cron jobs. A cron job is a function that gets executed by the server on a time interval. Usually you'd edit your "crontab" by executing crontab -e
Then, once inside, you'd write the interval you want, followed by the command.
Typically it looks like:
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log
Since its PHP, you can either a) run your php command as a php cli command, OR b) you can make the command get executed when a particular page is run... and just execute that in cron via a curl -X GET 'http://url/' (etc.)
Also, note that you can write all of your stuff in a shell script file and actually run that file as your cron command... that reduces line-item complexity
cron
Sorry I haven't closed this one.
I actually discovered that my host didn't allowed cron jobs running. So I found a relevant homepage that offer a free service to make a request for me when I needed. In my case, I have specified a url link that should be requested to my RESTful API each day.
The link is here and works like a charm :)
Is it possible to automaticly download xml files from one server to another server on a daily basis with PHP?
The goal is to create a webapplication in CakePHP which makes use of an xml report that comes from a online accountingserver.
So it can be done using a cronjob? But is cron supported with PHP?
Where can i configure that cronjob?
What kind of code should i write to get the file from the accountingserver in the first place?
Yes, you can create a PHP script that will download the data from the second server and then call it from using a cron job. Cron jobs allow you to run code at specific times. More info on that here.
It would probably look something like this:
0 0 * * * php path/to/file.php
This basically tells it to run file.php at minute 0, hour 0, every day, every month.
Surely you can.
You write a PHP program that does this transfer once, when called. Then we run that program automatically every day at a fixed time by setting up a cron job.
So... for example I want to add to 1 five every 5 minuts (1 is in the DB)... With out direct calls from users....
So... How to make PHP code work without direct calls (on some kind of timer)?
If you are unable to schedule cron jobs on your server (as is the case with most cheap hosting solutions), there are some pure php alternatives to run scheduled jobs: phpjobscheduler is one of those alternatives.
build a script which does what you need to do. and call it via crontab in the needed interval
but make sure its not callable from a user or searchengine.
see http://www.unixgeeks.org/security/newbie/unix/cron-1.html for more information on cron
a typical call could look like:
*/5 * * * * lynx -source http://yourhost.com/yourscript.php >/dev/null
Yes configuring a cron job is the correct answer.
See the Wikipedia article for the syntax of a cron job.
You simply create a new cron job and let it request the page where the script is.
The following cron job requests update.php every five minutes.
*/5 * * * * wget http://www.example.com/update.php
Update
Syntax with wget.
If I understood you correctly, CRON is what you're looking for (google it)
Cron is the obvious suggestion.
If you can only invoke your code via the web (i.e. no command line PHP) then you can call a URL using 'wget' or 'curl' on most *nix boxes.
Unless of course your code sits on a microsoft OS (which is unlikely to have cron available) in which case you can use the 'at' service to do the same thing - and there's a free wget.exe out there somewhere.
C.
I have a php script that reads one file through http(the file is on other domain). I would like to read this file only once or twice a day, instead of connecting to it every time the website is refreshed.
Is there any other way than doing it with cron?
I dont want to use cron cause I prefer to setup this behaviour in the script itself .. so it is flexible, so I can use it anywhere without setting up cron every time.
thanks
I've done this kind of thing in the past when I didn't have access to cron:
$lastRunLog = '/path/to/lastrun.log';
if (file_exists($lastRunLog)) {
$lastRun = file_get_contents($lastRunLog);
if (time() - $lastRun >= 86400) {
//its been more than a day so run our external file
$cron = file_get_contents('http://example.com/external/file.php');
//update lastrun.log with current time
file_put_contents($lastRunLog, time());
}
}
If you can't or don't want to use use cron and it's ok to update it only when the page is accessed. You could cache the result of the HTTP request and only update it on a page load it if the cache is older than a day or whatever interval you choose.
You can also use Web Based Cron if you want to hit a site on a timed interval.
You could even use a database table - really simple in structure, id, date, script url, and whatever you need - and add a row every time you run the script.
Then, before run the script simply check the numbers of row for each day you have.
You can use a Cronjob. You can then run the php script by the command line.
php /someplace/somefile.php
The Cronjob would be the following if you update every day.
0 0 * 0 0 php /someplace/somefile.php
Since you explicitly state that you don't want to use cron, the only other way to do this (without something analogous to cron) is to set up your script as a daemon. However, unless you really need the flexibility that daemons provide, cron is much easier and simpler.
Here's one daemon walk-through.
If you're using a Linux distro with systemd:
I had a need for scheduling yearly based jobs, independent of the application (in case the system rebooted or anything like that), and I was given the suggestion to use systemd Timers. The Arch Wiki Page on it gives some examples.
If you are on a *nix environment you can use cron jobs
What's wrong with cron?
You have a couple choices with cron - your php can be invoked by the command line PHP interpreter, or you could use wget or fetch or the equivalent to invoke your PHP on the server.
In general, PHP run from within the context of the web server has a time limit on how long it can execute, so in general you can't set up "background" PHP threads to do stuff "later".
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.