How to periodically execute a PHP script/plugin on wordpress? - php

I need to update a database that I have stored localy based on an online API. I need to make API calls periodically. What is a good strategy when it comes to periodical PHP script execution?

Use a cron job. There are tons of tutorials out there on how to do that. There is even a Wordpress Cron Job scheduler plugin (this link is only for reference, I'm not endorsing it specifically). You can find it here. Note that you will need administrative rights on the server to execute cron jobs.

Related

Can a cronjob continue to run 30 minutes continuously?

I have created a script on PHP that creates cache files from API and it takes around 30 minutes to load the page completely means when it creates all cache files.
I have a concern that my hostinger's customer support is telling me that it won't run for 30 minutes but in some answers, I found that it can run in the background and nothing to worry about until it's loaded.
So is that possible that the cronjob will run up to 30 minutes?
If not what is the best solution to run that cache making script at a specific time in the background like the cronjob does? Please Explain in brief so I can get a way.
Thanks for the great answer.
Ideally, for long running tasks, the task should be hosted in a platform that allows extended operations and defined in a way that it can be externally triggered, this might be in the form of an endpoint in a web API.
Then you can use the cronjob to trigger that process.
Without creating a whole API, you could make this a single endpoint on your website, a hidden page that only the cronjob knows how to call, then run your script from there.
There are lots of ways around this but the methodology is similar just use the cronjob as the trigger to a different process. Move the core logic of your script to a platform that allows the long execution time.
This is a similar post: Run a “long” php-script via Cronjob with an answer that suggests you can try to execute the script without waiting for the response, that is the same expectation with calling an external web process or API, the cronjob should not wait for a response.
It's good practice to limit resources on web server, especially in the shared hosting account. Because, in most cases, it may cause the web server to slow down and Denial of Services situation.
It's recommended to run the script using php-cli and cron.
php-cli offer much more relaxation, such as time and resource limitation. Please also read
Events in MariaDB VS Cron in php - which is better

Account activation time out in PHP & general PHP help

I was wondering because I can code in PHP without minors hitches or errors but one thing I have never understood about PHP is the fact it is a server side scripting language, so it needs something to trigger it, if this is so how can I create a PHP file which runs routine checks? and example is:
Someone has not activated there account although they have had a email sent out to them and it has been the time period given, were and how would the script run, would it need to be required into a page or is there a function for this.
Thanks in advance, I just need this clearing up a little it if's not too much trouble.
For this you would usually set up a cron job.
cron is a daemon that periodically starts other programs, for tasks such as the one you outline. You can make cron start php scripts, as well.
If you're on shared hosting, there's probably an option somewhere in your control panel to set up a periodic cron job.
Otherwise, here are some guides on doing it manually:
Cron Tutorial – Managing cron tab or cron job is Easy!
Newbie: Intro to cron
Cron Tutorial
Running PHP Scripts with Cron
If you are on a Unix/Linux system, you can write a php script that can be executed from command-line and add it as a cron job.

wordpress cron task

I would like to add an intensive task (lets say 5 minutes execution time) into Wordpress using cron job.
I been using this code to add new cron task inside the Wordpress system.
wp_schedule_event(time(), "interval-name", "hook-name");
I read somewhere in the net that cron task will be executed when there is request hit the Wordpress (either in the public site or the admin). Can anybody acknowledge that is true?
If that the case then I should not put my intensive task into cron task, because it will make user wait for long time after the task finished. What should I do now?
Anybody experienced this situation? Any suggestion?
I think to create a new page to be executed by crontab (for example http://example.com/wp-content/plugins/plugin-example/intensive-task.php)
The wordpress documentation says that it will be run when someone visits your site, so yes, you're correct. It will only be one user that gets a slow page load, so it's up to you if you want to avoid that.
If you are running it from a regular con job, there's no need to make it a page on your site though; especially if it's an intensive job, as you say, then this could easily be exploited place a large load on your server. You can easily run php from the command line to execute your job safely and without causing any slow load times on your page.
If you would use regular cronjob that wouldn't be the case
but i suspect that wp does what you said, since that would make it versatile working in different hosts with different setups as long as they have php and mysql running independent from real cronjobs which must be installed by the web host separately

Automating tasks with PHP

i wonder how can i schedule and automate tasks in PHP? can i? or is web server features like cron jobs needed.
i am wondering if there is a way i can say delete files after say 3 days when the file are likely outdated or not needed
PHP natively doesn't support automating tasks, you have to build a solution yourself or search google for available solutions. If you have a frequently visited site/page, you could add a timestamp to the database linking to the file, when visiting your site in a chosen time (e.g. 8 in the morning) the script (e.g. deleteOlderDocuments.php) runs and deletes the files that are older.
Just an idea. Hope it helps.
PHP operates under the request-response model, so it won't be the responsibility of PHP to initiate and perform the scheduled job. Use cron, or make your PHP site to register the cron jobs.
(Note: the script that the job executes can be written in PHP of course)
In most shared hosting environments, a PHP interpreter is started for each page request. This means that for each PHP script in said environment, all that script will know about is the fact that it's handling a request, and the information that request gave it. Technically you could check the current time in PHP and see if a task needs to be performed, but that is relying on a user requesting that script near a given time.
It is better to use cron for such tasks. especially if the tasks you need performed can be slow -- then, every once in a while, around a certain time, a user would have a particularly slow response, because them accessing a script caused the server to do a whole bunch of scheduled stuff.

Multithreading in PHP

I am working on windows. I have built a twitter application using the twitter API which periodically checks for new tweets as well as allows users to update their status. I have written separate PHP files for reading (reader.php) and writing tweets (writer.php). The only problem is how do I periodically read the tweets. There are a few ways which I can think of -
1) Use a time-based job scheduler (like Cron) to periodically run the reader.php.
How do I do this?
2) Use multithreading to run both reader and writer.php and use a timer function in reader.php
Suggestions?
Since you are working within Windows, you probably won't be able to use cron very easily. You can however, use the task scheduler. See this link for step-by-step instructions.
Another option is to use some kind of job queuing system. The Zend Server Job Queue has the ability to schedule recurring tasks. I use it on my site for an awful lot of stuff. Actually, a lot of what you are trying to do. It does periodic Twitter searches, processes relationships and such. I have a posting about how to use it at Do you queue? Introduction to the Zend Server Job Queue

Categories