Cron jobs
Scheduled tasks (known as cron jobs
) can be created and managed via the Linux crontab
utility.
Listing existing crons
crontab -l
crontab -l -u <username>
Editing the crontab file
crontab -e -u <username>
Crons should always be run as the relevant user. Do not run crons as admin
or root
.
How do I understand cron syntax?
https://crontab.guru/ has an excellent expression editor which can offer guidance on how to understand cron syntax.
Example
To create a cronjob invoked nightly at 2am, add the following line to your crontab:
0 2 * * * /usr/bin/php7.4 /home/username/public_html/mycrontask.php /dev/null 2>&1
In this example:
0 2 * * *
specifies the cron to run at 2am/usr/bin/php7.4
is the executable to run/home/username/public_html/mycrontask.php
is the argument for the executable- in this case the script to be executed./dev/null 2>&1
redirects the cron output to/dev/null
i.e. supresses it.