
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
If you are manually taking backup of your database, running sitemap/rss generation and much more stuff manually then this article is definitely for you.
In this article you will learn what is cronjob, whats the syntax of cronjob & how to automate few simple tasks with it and how to extend it to your daily life.
You have any Linux distributions. Even you have setup server in any cloud platform then its a great start.
Cronjob is a time based job scheduler, ie if you want to automate repetitive tasks in your servers at any specific time then we usually use it.
Cron is the daemon that runs every minute to check if any commands are there to execute in crontab. Crontab is the specific file where we specify which job needs to be run on what time.
Now lets see the syntax of the cronjob.
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday but non standard)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command_to_execute
Cronjob has 5 fields to run any command (6th field), which are as follows
1st
- Minute (0-59) On which specific minute to run command
2nd
- Hour (0-23) On which specific hour to run command
3rd
- Day of month (1-31) What day of the month should the command run
4th
- Month (1-12) Which month should it run
5th
- Day of Week (0-6) Which day of the week should it run. 0 - Sunday and 6 - Saturday, 7 also Sunday but its non standard, ie it know only in few operating systems. So better to avoid 7 and use 0 instead
6th
- The command that you want to run on specific dates.
The above 1 to 5 fields can have the following values
*
-> Any value
,
-> Value list separator
-
-> Value ranges
/
-> Step values
Some non standard function
@yearly
- Run cronjob command yearly once
@monthly
- Run cronjob command monthly once
@weekly
- Run cronjob command weekly once
@daily
- Run cronjob command daily once
@hourly
- Run cronjob command hourly once
@reboot
- Run cronjob command after reboot of OS
Once you login to your server or in your local linux laptops use the following command to open the crontab
sudo crontab -e
Crontab
is the file where you place all your commands that needs to run. This is watched every minute by cron
scheduler and checks if any commands needs to run.
I am running this as the sudo
user so as to avoid any permission issues and getting failed.
If your opening this file for the first time then it may ask you to choose the default editor. Select of your choice.
After you choose the default editor add the following command. The following command runs the command_to_run
command every minute.
* * * * * command_to_run
If your not getting what the heck does the above line does then don't worry I have explained lot of examples in the coming section.
Now we have seen the syntax of the cronjob, lets dive into it by doing few examples
* * * * * command_to_run
Quick Note - Observe *
which tells all values.
The command_to_run
runs every minute, every hour, every day, all months of year, all days of week
*/5 * * * * command_to_run
Quick Note - Observe /
which is a step values.
The command_to_run
runs every 5 minutes we are using /
step this divides 60 minutes with 5 minutes interval, every hour, every day, all months, all days of week.
0 */1 * * * command_to_run
The command_to_run
runs every 0th minute every 1 hour, every day, all months, all days of week.
0 */8 * * * command_to_run
Quick Note - Observe /
which is a step values divides 24 hours/ 8 hours.
The command_to_run
runs every 0th minute every 8th hour, every day, all months, all days of week.
0 2 * * * command_to_run
The command_to_run
runs every 0th minute, at 2am, every day, all months, all days of week.
30 1 * * * command_to_run
The command_to_run
runs every 0th minute, at 1am 30 minutes, every day, all months, all days of week.
0 6-9 * * * command_to_run
Quick Note - Observe -
which accepts range of values.
The command_to_run
runs on 0th minute, at 6,7,8 & 9am, every day, all months, all days of week.
0 0 * * 0 command_to_run
The command_to_run
runs on 0 th minute, o th hour, all months, on Sunday.
0 0 1 * * command_to_run
The command_to_run
runs on 0 th minute, 0 th hour, every 1st day, all months, all days of week.
0 0 1 */3 * command_to_run
The command_to_run
runs on 0 th minute, 0 th hour, every 1st day, of every 3rd month, irrespective days of week.
0 0 1 1 * command_to_run
The command_to_run
runs on 0 th minute, 0 th hour, on 1st day, 1st month of the year, irrespective days of week.
If you want to take your database backup every once a week
than you can do as follows
0 0 * * 0 mysqldump -uroot -p{PASSWORD} DATABASE_NAME >/etc/database_backups/DATABASE_NAME/$(date +%F)_full_myDB.sql
sitemap.xml
Generation Daily ExampleFor example if your sitemap.xml
generating script exists in /var/www/html/sitemap.php
then to run the cronjob use the following command
0 0 * * * /usr/bin/php /var/www/html/sitemap.php
If you wan to run any specific PHP version example PHP 7.2
then use the following command
0 0 * * * /usr/bin/php7.2 /var/www/html/sitemap.php
The cron scheduler runs the sitemap.php
script daily mid night 12am.
rss.xml
Generation Daily ExampleFor example if your rss.xml
generating script exists in /var/www/html/rss.php
then to run the cronjob use the following command
0 0 * * * /usr/bin/php /var/www/html/rss.php
The cron scheduler runs rss.php
script daily mid night 12am.
Hope you learnt about cronjob and expect you to implement it in your projects and in your daily work.
Ensure text remains visible during Webfont load
What Is HTTP? Different HTTP Methods And Status Codes Explained With Examples
Website Speed and Performance Optimizations
Install RabbitMQ with Docker & Running with NodeJS
PHP file_put_contents failed to open stream: Permission denied
Sass or SCSS @function vs @mixin
Getting Started With AMP (Accelerated Mobile Pages)
What Is Composer? How Does It Work? Useful Composer Commands And Usage
Accessors And Mutators In PHP Laravel
Google, Twitter, GitHub, Facebook & Many Other Social Generic Logins With PHP Laravel Socialite
Multiple GIT Key Pairs Or Account In The Same Computer
Integrate Google Translate Into Your Website
Create / Save / Download PDF From Blade Template In PHP Laravel