Getting started with cron job in the linux server

      49

Table of contents (only for web)

1 What are cron, cron job, & crontab?2 Understand a cron job syntax3 How khổng lồ handle an error on your cron job • Send output đầu ra to a specific tệp tin • Use /dev/null4 Write a simple cron automation script5 Conclusion6 References

What are cron, cron job, & crontab?

Cron is a system that helps Linux users khổng lồ schedule any task. However, a cron job is any defined task to run in a given time period. It can be a shell script or a simple bash command. Cron job helps us automate our routine tasks, it can be hourly, daily, monthly, etc.

Bạn đang xem: Getting started with cron job in the linux server

Note: in most of Linux system, we must get a permission of system administrator before defining a spesific cron job that is listed on crontab

Meanwhile, the crontab stands for cron table. It is a Linux system tệp tin that contains a danh mục of the cron job. We define our task — bash command, shell script, Python script, etc scheduled in crontab.

# check cron service on Linux systemsudo systemctl status cron.service

*

*

Check the cron service on the Linux system (Image by Author)

Understand a cron job syntax

It’s time lớn learn about cron job syntax on crontab.

crontab -a : create a new as crontab filecrontab -e: edit our crontab file or create one if it doesn’t already existcrontab -l: show up our crontab filecrontab -r: delete our crontab filecrontab -v: show up the last time we have edited our crontab file

minute(s) hour(s) day(s) month(s) weekday(s) command(s)

*

*

Description of the six columns on crontab file (Image by Author)Note: day names 0–6 begin with Sunday. We can easily determine our schedule on https://crontab.guru/

How khổng lồ handle an error on your cron job

If the cron job encounters an error, the default, it will send an thư điện tử to the system administrator. Instead, we will find out two common ways when we encounter the error.

1 Send đầu ra to a specific file

It’s a common way and I always use it on my cron job. It’s simple. We just need khổng lồ create a tệp tin that will save our cron job logs. It will print out the output just in case the job is accomplished properly or print out an error if it fails.

In this tutorial, it just created a log.out file. The output đầu ra will be redirected to lớn log.out.

* * * * * cd /home/audhi && /bin/bash shell-script.sh >> log.outThe mô tả tìm kiếm of the above syntax on a crontab file is as follows.

*

The crontab file on Linux system (Image by Author)

Note: we need khổng lồ write the complete và clear command in a crontab. It needed to specify the file location using cd

2 Use /dev/null

We can easily send our cron job logs & error khổng lồ the dev/null instead of an alert via email. Whatever we send or write lớn dev/null, it will be discarded.

Xem thêm: Hướng Dẫn Tải Giả Lập Droid4X, Droid4X: Giả Lập Android Trên Pc Siêu Nhẹ

* * * * * cd /home/audhi && /bin/bash shell-script.sh > /dev/null 2>&1A little description of the commands:

The > /dev/null tells the cron lớn redirect the đầu ra (STDOUT) lớn /dev/null2 is the file descriptor for Standard Error (STDERR)& is the symbol for tệp tin descriptor (without it, the following 1 will be a filename)1 is the tệp tin descriptor for Standard Out (STDOUT)

Note: The 2>&1 tells the cron khổng lồ redirect all errors (STDERR) lớn same as standard out (STDOUT)

Write a simple cron automation script

To complete this article, I have created a Python script to demonstrate how to use a cron job. This Python script will collect the Covid-19 data from one of the largest Indonesian online news, Kompas News. You can find out my Python script for Covid-19 data web scraping at my GitHub repo. Its filename is Web Scraping Covid-19 Kompas News.py.

Open our terminal & type crontab -e to open a crontab file. Then, scroll down & type the following command.

5 16 * * * cd 'covid19 data' && /usr/bin/python3 'Web Scraping Covid-19 Kompas News.py' >> test.outThe mô tả tìm kiếm of the above syntax on a crontab tệp tin is as follows.

The crontab is located in /home and my script is in /home/covid19 data , so we need to switch lớn the /home/covid19 data firstThe python3 interpreter is located in /usr/bin/python3The đầu ra will be redirected lớn test.out file in /home/covid19 data

Note: cron uses the local time

You can also learn Apache Airflow as job orchestration to lớn automate the regular task!


Conclusion

The cron job runs on a Linux system lớn run & execute our regular tasks (terminal commands). The most important thing to lớn learn about the cron job is the bash command on the terminal, how to set our task schedule, và make sure to catch the whole possibilities when our script is running on production, so we can prevent the error.

References

<1> Schkn. Cron Jobs and Crontab on Linux Explained (2019), https://devconnected.com/cron-jobs-and-crontab-on-linux-explained/.

<2> C. Murray. Understanding Crontab in Linux With Examples (2019), https://linuxhandbook.com/crontab/#quick-introduction-to-key-cron-concepts.

<3> N. Agatha. Cron Job: Panduan Lengkap untuk Pemula 2020 (2020), https://www.hostinger.co.id/tutorial/cron-job/.