Wordpress cron job only when 'user visit website'? - php

I've heard that the wordpress cron job only works when a trigger like 'user visits the website' occurs. What if I want to run a cron job every night automatically that runs a .php file. The complete scenario is that I want to save some data coming from a third party source as an API call to one of my custom php file, say savedata.php, and I save this data in a text file (for now) and another .php file say: executecron.php is ready to run automatically at 11 pm at night. How can I do this in wordpress without any users visiting my website? And I want to notify that I do not have permission to log into the dashboard but I can work all the php files.
Also please have patience with me as I'm totally new to wordpress development.
Thanks

Yes, WordPress Cron Job are only loaded once the user visit the site.
So, as of your problem you want to automatically load the file at certain interval of time you need to configure the cron job from the server and specify the path of the file you want to cron job at fixed interval of time.
The setting mostly found in the cPanel menu but it might vary according to the hosting provider or you can ask hosting provider support team for doing it.
Here are the useful links:
Site Ground Real Cron Job
Cron Job for WordPress
If you still find issue please contact me for solving issue.

Related

Why Wordpress's wp-cron checks schedule only on each page load?

In WordPress, I came across that wp-cron runs only on each page load and not as system cron which runs as per schedule even if no request comes. Can anyone explain this in detail? Also, is it true for all types of web servers?
Because wp-cron and system crontab - is different systems. WP Cron save scheduled event in database with time and for every load wp site, wp cron check database and do event if it needed. System cron works with system time and run scripts.
For all sites normally use system crontab. For Wordpress you also can use crontab and disable wp-cron in wp-config.php

Security In Cron Job triggered scripts in PHP & Codeigniter

First thing i want to mention is i am relatively fresher in developing, I am working on a codeigniter project, In that project I need to inform users of that application about expiry of their service a week before via email also i need to change database status after expiry date passes for a service
To accomplish that i am developing scripts that can be triggered via CRON JOBS in cpanel, Well my question is can we provide any level of security for cron jobs triggered PHP Scripts like we do in a php application where we check the user role before access to each class or script, My concern is that the scripts will be accessible via URL and anybody knowing those URL's can trigger it.
Example of URL of PHP Script that will be triggered by cron job schedules.
www.example.com/admin/remainder_emails
can i anyhow provide any level of authentication via PHP or Linux to PHP scripts that only authorised user can trigger it, i want to know this not just for this application but for my future projects too. I would be grateful if someone can just list me out the basic security issues to keep in mind using cron job triggered scripts in application.

Make a cron like schedule

My webhost doesn't have cron in it so I cannot set up cron jobs.
There is a certain php script in my website that should be ran every 24 hours to write daily logs etc.
I tried looking for external schedulers but I don't trust them.
How can I do such thing without cron?
I thought I maybe could use a mysql server and have an event or something?
I do NOT want it to rely on when a user uses my website due to the exact daily timing that I need.
Help is appreciated.
From a different host, set up a cronjob that does a wget of a page on your web-site. When that page is loaded, it does your script.

Creating PHP Bots for site maintenance

I have a PHP website in which, when a member visits a page, a series of database maintenance actions are made.
For example, in a common page, I've included a PHP script which checks how many posts every user has made and updates the database giving them points accordingly.
The problem with this method is that my website has 100+ members, and I'm worried that my scripts start to slow down as my memberbase grows.
Is there any way to code a bot in PHP, so my database can be updated without the user's intervention?
You should run a PHP file from within a cron job. Most PHP hosts including shared hosting provide cron access.
With cron you can schedule a task to run on an interval basis. This PHP program will then go through and do the updating that you require. So... take the code you do now and move it into a seperate PHP file and then tell cron to run it maybe once an hour or whatever you deem to be the correct interval.
For best performance, you need to update users table when he publish the post, not every time when need to know how many posts he published.
Create a cron to run daily (for instance) with the follow command:
php -q /home/cpaneluser/cron.php
And put a cron.php outside of public_html with all maintenance taks.
Or allow only administrators to do the maintenance tasks with a link in administrative panel.

Is there a way in which a PHP or shell script can setup a cron job on a web server?

I am writing a component for Joomla and there is a specific task that requires an update to some stats every so often. I would like to setup a cron job to do this. The only problem is that requires the user to go and setup the cron to run the php update stats script.
On installation of the component how can I automatically setup a cron job for the user? Is this possible?
I've seen this implemented in the Akeeba backup pro component for Joomla, so I was hoping that I would be able to do the same thing.
Thanks
In theory you can create a crontab file and call it from PHP
<?php
exec("crontab $path_to_cron_file");
in practice it depends on wether the server (if you're on a shared hosting) allows you to do that.
All you need to do is write a line to the crontab file, generally stored in /var/spool/cron/crontabs/username. The cron daemon will see that the file modification time has changed and reload it automatically when it wakes up to do its checks
Another option (less desirable from a server load perspective but easier for users) would be to create a plugin that is run each time a visitor visits the site. It could check whether the process has been run in a specified time and then run it if it needs to be run.
iJoobi.com has another solution where they have set up a server to run cron tasks, which would then ping the specific URL on the website to kick off the process. (http://www.ijoobi.com/Help/jNews/jNews-Cron-Task.html)

Categories