How to Setup Cron Jobs in Linux

29/12/2020
Chưa phân loại
Automating and scheduling repeated tasks on desktops and servers improves productivity and cuts down manual intervention and monitoring. Numerous such tools are available for Linux and one of the most widely used among them is “cron”.

This article will explain scheduling tasks and running scripts at regular intervals using cron jobs.

What is Cron?

Cron is a task scheduling program that runs in the background as a process. It can be used to periodically run certain jobs that are scheduled using its predefined set of rules. Cron comes preinstalled on most Linux distributions or available in the repositories to be installed manually.

Cron Presets

Cron on Ubuntu comes with a set of pre-defined schedulers. You can check them by running the command below:

$ ls /etc/cron.*

Usually these folders are “/etc/cron.hourly”, “/etc/cron.daily”, “/etc/cron.monthly” and “/etc/cron.weekly”.

Just put your desired scripts in one of these folders to run automated tasks hourly, daily, monthly or weekly.

Note that cron uses “/bin/sh” shell by default. If you want to use “/bin/bash” as the shell in cron scripts, you have to use “!# /bin/bash” hashbang at the top of the script file or define shell as “SHELL=/bin/bash” variable in the first line in a scheduled bash script. Overriding “SHELL” environment variable also works for crontab editor explained below.

Not all system environment variables may be exposed to the cron jobs. So it is generally a good idea to manually define important environment variables in a script scheduled by cron.

Crontab

Crontab allows you to define your own scheduled tasks. You can use it to configure cron schedules different from the four standard cron presets explained above.

To set a scheduled task in crontab, first launch the crontab editor using the command below:

$ crontab -e

Now at the end of the file, you can add an entry in the following format:

minute hour day month weekday /full/path/to/script.sh

Where:

  • minute: any value in the range 0 to 59
  • hour: any value in the range 0 to 23, 0 being midnight
  • day: any value in the range 1 to 31
  • month: any value in the range 1 to 12
  • weekday: any value in the range 0 to 6, 0 being Sunday

The crontab entry below will take a screenshot of your desktop and store it in your home directory once at 15:13 hours on 2nd of March plus every Tuesday in March at 15:13 hours.

13 15 2 3 2 scrot

To list all crontab entries, use the command below:

$ crontab -l

More Examples

Cron tasks can be best explained through examples, so below are some examples that will periodically take screenshots of your desktop and store them in home folder.

Besides the five scheduling variables, cron also uses some special string to schedule jobs. These strings are:

  • @hourly: job is run every hour
  • @daily / @midnight: job is run every day
  • @weekly: job runs once a week
  • @monthly: job runs once a month
  • @yearly / @anually: job runs once a year
  • @reboot: job runs once every reboot / login

The crontab entry below will take screenshot once on every reboot:

@reboot scrot

You can replace “@reboot” with any other special strings mentioned above.

Using an asterisk or wildcard in place of any of the five variables will repeat job on all new instances of that variable. The entry below will take a screenshot every minute:

* * * * *  scrot

Every hour:

0 * * * *  scrot

Everyday at 18:00 hours:

0 18 * * * scrot

Every month on 6th at 18:00 hours:

0 18 6 * * scrot

Use “/” to run a script at a fixed periodic interval of 30 minutes (runs every 30 minutes):

*/30 * * * * scrot

You can use comma separated variables to define repeat execution of script at regular intervals. The example below will take a screenshot of your desktop on every 1st, 4th, and 5th minute of a new hour.

1,4,5 * * * *  scrot

Verifying Crontab Entries

Defining crontab entries can be tricky but debugging them or verifying if they work at correct timestamps is even trickier. One option is to use a virtual machine and log timestamps of a script running periodically on a cron job by including something like “date” command and redirecting the output to a text file. However, a better solution is to use one of the numerous online cron jobs testing services available on the web. I recommend giving cron tester website available here a try.

Conclusion

Running cron jobs is an excellent way to automate repeated tasks, especially on servers. The tool is extremely powerful and customizable. However, it is important to ascertain that your scheduler definition works correctly, especially if there are mission critical tasks.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

[Ossec – Part 1] Tìm hiểu về hệ thống phát hiện xâm nhập Ossec

Nghe tiêu đề thôi thì tôi đoán bạn cũng mường tượng ra chức năng của Ossec rồi đúng không?...
30/12/2020

How to install Vectr – A free Vector Graphics Software on Linux

Vectr is a free graphics software used to create vector graphics easily and intuitively. It’s a simple yet powerful...
28/12/2020

GNU Octave Getting Started

Install and Get Started with GNU Octave Octave started out as a software for chemistry class. GNU Octave has now evolved...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

mua Proxy riêng ở đâu, và nó đem lại lợi ích gì cho người sử dụng
22/11/2022

Hướng dẫn sử dụng Proxy Helper Fakeip khi thuê proxy
21/11/2022

PROXY NUÔI TÀI KHOẢN FACEBOOK – KINH NGHIỆM FAKE IP – THUÊ PROXY GIÁ RẺ
14/11/2022

Mua Proxy Nuôi Zalo Giá Rẻ Tại Onet.com.vn
14/11/2022

BẢNG GIÁ MUA PROXY VIỆT NAM và PROXY US Onet.com.vn
14/11/2022