Unix Cron & WordPress
Versie door Jeroen Strompf (overleg | bijdragen) op 5 dec 2023 om 14:38
It is possible to use as server's cron system to execute scheduled tasks and functions.
Example
As an example, this is the cron job that WP Rocket recommends to be executed about every 10 minutes:
wget -q -O - https://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
What you can see from this example:
- To execute cron jobs within WordPress, usually the file
wp-cron.php
needs to be fetched - Output is send to
/dev/null
.
Syntaxis
Timefields:
Minute Hour Day of month Month Day of week Remarks 0-59 0-23 1-31 1-12 0-6 (0 = Sunday) Possible values ------ ---- ------------ ----- ---------------- ----------- 0 16 1,15 * * Do something on day 1 & 15 at 16:00 */10 * * * * Do something every 10 minutes
crontab - Examples
$ crontab -l no crontab for jeroen
$ crontab -l -u www-data must be privileged to use -u
$ sudo crontab -l -u www-data no crontab for www-data
$ sudo crontab -e -u www-data no crontab for www-data - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/nano <---- easiest 2. /usr/bin/vim.basic 3. /usr/bin/vim.tiny 4. /bin/ed Choose 1-4 [1]: 2 ... interactieve bijwerken cronfile... crontab: installing new crontab
$ sudo crontab -l -u www-data # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any'). # # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command */10 * * * * wget -q -O - https://example/wp-cron.php?doing_wp_cron >/dev/null 2>&1
Log
To check if it actually works:
sudo grep CRON /var/log/syslog
of
sudo grep www-data /var/log/syslog
Case: Automatic updates (July 2022)
Would this work? There are a few sudo commands in this script. And where does the output go?
$ crontab -e 0 4 * * 1 /usr/local/bin/scripts/update_all.sh
See also
Sources
- https://docs.wp-rocket.me/article/1279-cron-and-wp-rocket
- https://stackoverflow.com/questions/46442933/create-cronjob-ubuntu-for-user-www-data-using-bash#46443063
- https://docs.oracle.com/cd/E19455-01/805-7229/sysrescron-62861/index.html
- https://crontab.guru/every-10-minutes
- https://www.cyberciti.biz/faq/crontab-every-10-min/