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.







Automate Repeating Tasks In Linux Server With Cronjobs


Share On     Share On WhatsApp     Share On LinkedIn


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.


Prerequisites


You have any Linux distributions. Even you have setup server in any cloud platform then its a great start.


What Is Cronjob?


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.


Cronjob Syntax


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


How & Where To Add These Cronjob Commands


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.


Examples


Now we have seen the syntax of the cronjob, lets dive into it by doing few examples


Run Cronjob Every Minute (ie 1 Minute)


 * * * * * 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



Run Cronjob Every 5 Minute


 */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.



Run Cronjob Every 1 Hour


0 */1 * * * command_to_run


The command_to_run runs every 0th minute every 1 hour, every day, all months, all days of week.



Run Cronjob Every 8 Hour


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.



Run Cronjob Daily At 2 am


0 2 * * * command_to_run


The command_to_run runs every 0th minute, at 2am, every day, all months, all days of week.



Run Cronjob Daily 1 am 30 minute (1.30 am)


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.



Run Cronjob Daily Between 6am - 9am (ie 6, 7, 8, 9)


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.



Run Cronjob Weekly Once


0 0 * * 0 command_to_run


The command_to_run runs on 0 th minute, o th hour, all months, on Sunday.



Run Cronjob Monthly Once


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.


Run Cronjob 3 months Once


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.


Run Cronjob Yearly


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.


Automating DB Backup Example


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



Automating sitemap.xml Generation Daily Example


For 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.



Automating rss.xml Generation Daily Example


For 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.


Conclusion


Hope you learnt about cronjob and expect you to implement it in your projects and in your daily work.







Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing