How to check for WordPress updates with system Cron

I have been building a system to track paid/private wordpress plugins utilizing SatisPress. A key component of that is to trigger wordpress to fetch or check for updates often enough. There’s no issue if you have a busy site because the wordpress cron will have ample site visits to do its work. 

If, though you are not building a public site, and the only purpose is to track when plugins get updated so that SatisPress can capture the plugin version.  Then you will need to trigger wordpress being run some other way. If that’s what you’re looking for, then

Let Me Show You What I Figured Out!

There are three ways to trigger wordpress to go look for updates. 

  • Manually visit the plugins page on the admin dashboard. (and probably other pages, but for sure the plugins page)
  • System cron to run the WP-CLI command wp plugins list
  • System cron to login to the admin dashboard and visit the plugins page. 
I’m assuming your here because you don’t want to periodically visit the page manually, So I’ll Skip that one. 

Using the System Cron to check plugin updates.

Login to your website shell and execute the following command which will start up a text editor and allow you to edit the cron tab file for the currently logged in user. 
0
Do you want additional info on how to ssh (secure shell) into your website?x

Terminal
				
					crontab -e     
				
			

If this is the first time you have opened your crontab then it will ask you to choose an editor, Personally I like using Vim, but Nano is also good if you are unfamiar with editing files in a terminal. 

When you are actually editing the crontab, review the following format: 

At a specific time, the system will run a command. Most of the time, I just run over to Crontab.Guru and play with the times until I get what I want.

  • A * represents Every unit: every minute, hour, day, etc. 
  • you can do it every x minutes by using a division */x
    For example: */10 will be every 10 minutes. ( or any other unit as well )
  • You can list specific times with a single number or a comma-separated list. 
    ex. 10 or 10,20,30 

We are going to be running this every 6 hours, so this will run on the hour at UTC 0,6,12,18. If you have a different timezone. For example, I want this to run at midnight in my timezone, you would have to calculate when that would be and just list the hours. For Mountain Standard Time (-7 hours), I would list MST 1,7,13,19.  You get the picture yeah?

Let’s get to the actual command inside our crontab file:

crontab
Terminal
				
					# Edit this file to introduce tasks to be run by cron.
# m h  dom mon dow   command
  0 */6 * * * cd /srv/www/<path>/<to>/<wordpress>/ && wp plugin list > /<path>/<to>/<log>/<folder>/log_$(date +\%Y-\%m-\%d_\%H-\%M).log
  0 */6 * * * cd /srv/www/<path>/<to>/<wordpress>/ && wp plugin list > /dev/null 2>&1
				
			

Remember, the point of checking for the updates in my case is to allow satispress to capture the new versions and zip up the plugin version so i can fetch them later with PHP Composer. Sometimes its nice to have a log of what happened or what the result of running that command was The downside is that it generates logs that fill up your hard drive space if you collect too many of them. 

If you want to keep the output of the command as a log, use the first to send the output to a file. 
If you want to send the output of the command to the trash, use the second one. 

This works well for things that use the official wordpress update hooks, but it seems like a few plugins do something different and only actually check when you visit the plugin page or access the admin area of wordpress. Its unfortunate because we have to find some other way to check for updates if that’s the case. 

For example if I run the wp plugin list command it shows that there are no updates for these crocoblock plugins but more importantly, Satispress does not pick up the update:

But if I navigate over to the admin plugin page then that update shows up. 

And satispress sees the update and captures that plugin version. Something is triggered when you visit the page, but it is not triggered when you run: wp plugin list So, how do we automatically visit the admin page?

Using the login WP-CLI command

There is a very useful package called WP-Cli Login Command. What this does is allow you to create a magic login link to allow someone (yourself via the shell) to login via a link. Additionally, it has a command line flag to specify where to redirect the login request after logging in. 

crontab
				
					# m h  dom mon dow   command
0 */6 * * * cd /srv/www/<path>/<to>/<wordpress>/ && curl -L -c - -o /srv/www/<path>/<to>/<updateLogs>/log_$(date +\%Y-\%m-\%d_\%H-\%M).html $(wp login as johnkraczek --redirect-url=https://example-site.com/wp/wp-admin/plugins.php --url-only) > /dev/null 2>&1
0 */6 * * * cd /srv/www/<path>/<to>/<wordpress>/ && curl -L -c - $(wp login as johnkraczek --redirect-url=https://example-site.com/wp/wp-admin/plugins.php --url-only) > /dev/null 2>&1
				
			

A little convoluted, but lets break it down: This will be out of order in the command, but in order of how its calculated. 

  • 0 */6 * * * – Cron String for every 6 hours at x:00
  • cd /srv/www/<path>/<to>/<wordpress>/ – Change the directory to the wordpress root. 
  • && – Chain the previous command to the next one. 
  • $(wp login as YourUserName --redirect-url=https://example-site.com/wp/wp-admin/plugins.php --url-only) – Start a subshell, and run this command which will output a URL we can call with curl to login and be redirected to the page we specify. 
  • curl -L -c - <URL to fetch> – make a curl request, Allow Redirects, and turn on the cookie jar with an In Memory location. 
  • > /dev/null 2>&1 – Send the output of the curl command to the trash so that the command runs silently. 
  • -o /srv/www/<path>/<to>/<updateLogs>/log_$(date +\%Y-\%m-\%d_\%H-\%M).html – Output the fetched file to a log file as an HTML document. (OPTIONAL)

Clean Up After Yourself

If we are generating lots of logs, eventually it will cause an issue. 

Lets add another cron to remove all log files older than 2 weeks. 

crontab
				
					0 0 * * * find /srv/www/<path>/<to>/<logs>/ -name "log_*.log" -type f -mtime +14 -exec rm {} \;
				
			

All Done

And that pretty much sums up this post. Let me know if this is helpful in the comments below. And we will see you in the next one. 

Related Articles

The 4 C’s

Historically, I have been crippled by perfectionism, preventing me from sharing my journey and the lessons I’ve learned. I’ve embraced a new paradigm: “This is what I’m working on… Let me show you what I figured out.” This mindset shift allows me to share my experiences without judgment, inviting others to learn from my successes and failures. Join me as I explore the four levers of building an online business—Code, Content, Capital, and Collaboration—and discover how we can grow together in this entrepreneurial journey.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x