
{"id":39,"date":"2021-08-31T17:31:13","date_gmt":"2021-08-31T17:31:13","guid":{"rendered":"http:\/\/pi-smb\/wordpress\/?p=39"},"modified":"2021-08-31T17:31:13","modified_gmt":"2021-08-31T17:31:13","slug":"cron-job-in-the-linux-server","status":"publish","type":"post","link":"https:\/\/dsslit.ddns.net\/wordpress\/?p=39","title":{"rendered":"Cron Job in the Linux Server"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1 <a href=\"https:\/\/towardsdatascience.com\/create-your-first-cronjob-on-the-linux-server-74e2fdc76e78#bed8\">What are cron, cron job, and crontab?<\/a><br>2 <a href=\"https:\/\/towardsdatascience.com\/create-your-first-cronjob-on-the-linux-server-74e2fdc76e78#4a20\">Understand a cron job syntax<\/a><br>3 <a href=\"https:\/\/towardsdatascience.com\/create-your-first-cronjob-on-the-linux-server-74e2fdc76e78#f0c2\">How to handle an error on your cron job<\/a><br>  \u2022 <a href=\"https:\/\/towardsdatascience.com\/create-your-first-cronjob-on-the-linux-server-74e2fdc76e78#5f39\">Send output to a specific file<\/a><br>  \u2022 <a href=\"https:\/\/towardsdatascience.com\/create-your-first-cronjob-on-the-linux-server-74e2fdc76e78#99b9\">Use \/dev\/null<\/a><br>4 <a href=\"https:\/\/towardsdatascience.com\/create-your-first-cronjob-on-the-linux-server-74e2fdc76e78#d7e8\">Write a simple cron automation script<\/a><br>5 <a href=\"https:\/\/towardsdatascience.com\/create-your-first-cronjob-on-the-linux-server-74e2fdc76e78#2c02\">Conclusion<\/a><br>6 <a href=\"https:\/\/towardsdatascience.com\/create-your-first-cronjob-on-the-linux-server-74e2fdc76e78#62e2\">References<\/a><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bed8\">What are cron, cron job, and crontab?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"43b4\">Cron is a system that helps Linux users to 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.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong>\u00a0in most of Linux system, we must get a permission of system administrator before defining a spesific cron job that is listed on\u00a0<code><strong>crontab<\/strong><\/code><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"6562\">Meanwhile, the crontab stands for cron table. It is a Linux system file that contains a list of the cron job. We define our task \u2014 bash command, shell script, Python script, etc scheduled in crontab.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong># Check cron service on Linux system<\/strong><br>sudo systemctl status cron.service<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*6rp0JBPi7P6UW9x0AxaLAQ.png\" alt=\"Check the cron service on Linux system\"\/><figcaption class=\"wp-element-caption\">Check the cron service on the Linux system (Image by Author)<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4a20\">Understand a cron job syntax<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"5ab6\">It\u2019s time to learn about cron job syntax on&nbsp;<code><strong>crontab<\/strong><\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>crontab -a &lt;filename><\/strong><\/code>: create a new\u00a0<code><strong>&lt;filename><\/strong><\/code>\u00a0as crontab file<\/li>\n\n\n\n<li><code><strong>crontab -e<\/strong><\/code>: edit our crontab file or create one if it doesn\u2019t already exist<\/li>\n\n\n\n<li><code><strong>crontab -l<\/strong><\/code>: show up our crontab file<\/li>\n\n\n\n<li><code><strong>crontab -r<\/strong><\/code>: delete our crontab file<\/li>\n\n\n\n<li><code><strong>crontab -v<\/strong><\/code>: show up the last time we have edited our crontab file<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>minute(s) hour(s) day(s) month(s) weekday(s) command(s)<\/strong><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*tAtNr4Crf-TmeMo2YRCr1g.png\" alt=\"Description of the six columns on crontab file\"\/><figcaption class=\"wp-element-caption\">Description of the six columns on crontab file (Image by Author)<\/figcaption><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong>\u00a0day names 0\u20136 begin with Sunday. We can easily determine our schedule on\u00a0<a href=\"https:\/\/crontab.guru\/\">https:\/\/crontab.guru\/<\/a><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"f0c2\">How to handle an error on your cron job<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"4977\">If&nbsp;the cron job encounters an error, the default, it will send an email to the system administrator. Instead, we will find out two common ways when we encounter the error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5f39\"><strong>1 Send output to a specific file<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"624b\">It\u2019s a common way and I always use it on my cron job. It\u2019s simple. We just need to create a file 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"e4be\">In this tutorial, it just created a&nbsp;<code><strong>log.out<\/strong><\/code>&nbsp;file. The output will be redirected to&nbsp;<code><strong>log.out<\/strong><\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">* * * * * cd \/home\/audhi &amp;&amp; \/bin\/bash shell-script.sh &gt;&gt; log.out<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"482f\">The description of the above syntax on a crontab file is as follows.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The\u00a0<code><strong>* * * * *<\/strong><\/code>\u00a0means that a task will be executed every minute of every hour of every day of every month and every day of the week<\/li>\n\n\n\n<li>The directory will be switched to\u00a0<code><strong>\/home\/audhi<\/strong><\/code>\u00a0where the shell-script.sh is located<\/li>\n\n\n\n<li><code><strong>\/bin\/bash<\/strong><\/code>\u00a0is the path and executable of the\u00a0<em>Bash shell<\/em><\/li>\n\n\n\n<li>The\u00a0<code><strong>>><\/strong><\/code><strong>\u00a0<\/strong>symbol<strong>\u00a0<\/strong>will append the output to an existing file (<code><strong>log.out<\/strong><\/code>), while a single\u00a0<code><strong>><\/strong><\/code>\u00a0symbol will overwrite the file<\/li>\n\n\n\n<li>The\u00a0<code><strong>shell-script.sh<\/strong><\/code>\u00a0is a certain shell script<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/700\/1*nf2KKNBbMNSAyJ16rnzPjQ.png\" alt=\"The crontab file on Linux system\"\/><figcaption class=\"wp-element-caption\">The crontab file on Linux system (Image by Author)<\/figcaption><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong>\u00a0we need to write the complete and clear command in a crontab. It needed to specify the file location using\u00a0<code><strong>cd<\/strong><\/code><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"99b9\">2 Use \/dev\/null<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"27db\">We can easily send our cron job logs and error to the&nbsp;<code><strong>dev\/null<\/strong><\/code>&nbsp;instead of an alert via email. Whatever we send or write to&nbsp;<code><strong>dev\/null<\/strong><\/code>, it will be discarded.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">* * * * * cd \/home\/audhi &amp;&amp; \/bin\/bash shell-script.sh &gt; \/dev\/null 2&gt;&amp;1<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"e653\">A little description of the commands:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The\u00a0<code><strong>> \/dev\/null<\/strong><\/code>\u00a0tells the cron to redirect the output (STDOUT) to\u00a0<code><strong>\/dev\/null<\/strong><\/code><\/li>\n\n\n\n<li><code><strong>2<\/strong><\/code>\u00a0is the file descriptor for\u00a0<em>Standard Error\u00a0<\/em>(STDERR)<\/li>\n\n\n\n<li><code><strong>&amp;<\/strong><\/code>\u00a0is the symbol for file descriptor (without it, the following\u00a0<code><strong>1<\/strong><\/code>\u00a0will be a filename)<\/li>\n\n\n\n<li><code><strong>1<\/strong><\/code>\u00a0is the file descriptor for\u00a0<em>Standard Out\u00a0<\/em>(STDOUT)<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong>\u00a0The 2>&amp;1 tells the cron to redirect all errors (STDERR) to same as standard out (STDOUT)<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d7e8\">Write a simple cron automation script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"6305\">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,&nbsp;<a href=\"https:\/\/www.kompas.com\/covid-19\"><em>Kompas News<\/em><\/a>. You can find out my Python script for Covid-19 data web scraping at my&nbsp;<a href=\"https:\/\/github.com\/audhiaprilliant\/Web-Scraping-Covid19-Kompas-News\"><strong>GitHub repo<\/strong><\/a>. Its filename is&nbsp;<code><strong>Web Scraping Covid-19 Kompas News.py<\/strong><\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"b0ce\">Open our terminal and type&nbsp;<code><strong>crontab -e<\/strong><\/code>&nbsp;to open a crontab file. Then, scroll down and type the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5 16 * * * cd 'covid19 data' &amp;&amp; \/usr\/bin\/python3 'Web Scraping Covid-19 Kompas News.py' &gt;&gt; test.out<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"8837\">The description of the above syntax on a crontab file is as follows.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The crontab is located in\u00a0<code><strong><em>\/home<\/em><\/strong><\/code><strong>\u00a0<\/strong>and my script is in\u00a0<code><strong><em>\/home\/covid19 data<\/em><\/strong><\/code><strong>\u00a0<\/strong>,<strong>\u00a0<\/strong>so we need to switch to the\u00a0<code><strong><em>\/home\/covid19 data<\/em><\/strong><\/code><strong><em>\u00a0<\/em><\/strong>first<\/li>\n\n\n\n<li>The\u00a0<code><strong>python3<\/strong><\/code>\u00a0interpreter is located in\u00a0<code><strong>\/usr\/bin\/python3<\/strong><\/code><\/li>\n\n\n\n<li>The output will be redirected to\u00a0<code><strong>test.out<\/strong><\/code>\u00a0file in\u00a0<code><strong><em>\/home\/covid19 data<\/em><\/strong><\/code><\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong>\u00a0cron uses the local time<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"7cac\">You can also learn&nbsp;<strong><em>Apache Airflow<\/em><\/strong>&nbsp;as job orchestration to automate the regular task!<a href=\"https:\/\/medium.com\/analytics-vidhya\/apache-airflow-as-job-orchestration-e207ba5b4ac5\" target=\"_blank\" rel=\"noreferrer noopener\">Apache Airflow as Job Orchestration of Covid-19 Data (send notification via email and Telegram)Web Scraping of Covid-19 Data in Indonesiamedium.com<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2c02\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"da9b\">The cron job runs on a Linux system to run and execute our regular tasks (terminal commands). The most important thing to learn about the cron job is the bash command on the terminal, how to set our task schedule, and make sure to catch the whole possibilities when our script is running on production, so we can prevent the error.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 What are cron, cron job, and crontab?2 Understand a cron job syntax3 How to handle an error on your cron job \u2022 Send output to a specific file \u2022 Use \/dev\/null4 Write a simple cron automation script5 Conclusion6 References What are cron, cron job, and crontab? Cron is a system that helps Linux users &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-linux","category-pi"],"_links":{"self":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":0,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}