{"id":175,"date":"2020-09-26T12:59:52","date_gmt":"2020-09-26T12:59:52","guid":{"rendered":"https:\/\/dft.wiki\/?p=175"},"modified":"2025-04-11T19:07:09","modified_gmt":"2025-04-11T23:07:09","slug":"running-commands-periodically-or-scheduled-in-linux","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=175","title":{"rendered":"Running Commands Periodically or Scheduled in Linux"},"content":{"rendered":"<p>You have one command or one program that you want it to be executed automatically in one specific moment, like tomorrow at 4 am or in 1 hour from now or every day at 11 pm.<\/p>\n<p>Each one has one specific usage, for backups you would probably schedule to do it automatically at one specific time that does not affect your work. In this case, you have to use <strong>cron<\/strong>. It is one native functionality of Linux kernel that reads the crontab every minute and check if any of the rules matches to the present time.<\/p>\n<p>To edit the crontab you can type:<\/p>\n<pre>crontab -e<\/pre>\n<p>If it is the first time you edit this file it will ask you the preferred editor you want yo use. If you are not familiarized with vi ou vim use nano that is more intuitive.<\/p>\n<p>Inside this file, called table, or cron table, you have to use the syntax to inform the system when your command needs to be executed.<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" class=\"wp-image-144\" src=\"https:\/\/dft.wiki\/wp-content\/uploads\/sites\/15\/2020\/09\/image-8-1.png\" alt=\"\" \/><\/figure>\n<p>Let&#8217;s assume the command you want to run every one minute is pinging another server on the internet to see if the connection is working:<\/p>\n<pre>* * * * * * ping test.com -c 1<\/pre>\n<p>It will run the command every minute because the cron will check the parameters and every time it will match the current time.<\/p>\n<p>Let&#8217;s assume the command you want to run is to shut down the machine every day at 11:00 pm:<\/p>\n<pre>* 0 23 * * * sudo shutdown now<\/pre>\n<p>As you can see you have to use 24h syntax, not AM or PM.<\/p>\n<p>This will only be executed when the clock turns to 23:00. If you turn on your computer at 23:01 it will only execute the command on the next day.<\/p>\n<p>Let&#8217;s assume the command you want to run is to update the database in the locate command. So you can locate files easily with the database no older than one-day:<\/p>\n<pre>* * 22 * * * sudo updatedb<\/pre>\n<p>In this command, there is one logical error. The command will be performed every day but not only once at 10 pm, but every minute from 22:00 to 22:59. This is probably what anyone wants and will just make one unnecessary workload on the computer for this our.<\/p>\n<p>To solve this issue set precisely when you want to run it, for example, 30 minutes before the computer shutdown:<\/p>\n<pre>* 30 22 * * * sudo updatedb<\/pre>\n<p>There are situations when you want to perform one specific command\/program only once at one specific time and not periodically. For this, you may use the <strong>at<\/strong> command.<\/p>\n<p>Let&#8217;s assume you are in your work and you want to download one big file but you do not want to slow down the internet for all the other workers. One simple solution is to schedule the download for one specific time after the company hours:<\/p>\n<pre>at 19:00 -f .\/Downloads\/script.sh<\/pre>\n<p>This script will run only once at 7 pm.<\/p>\n<pre>at now + 30 minutes -f .\/Downloads\/script.sh<\/pre>\n<p>In this other example, the script will run only once in 30 minutes from now.<\/p>\n<hr \/>\n<p><strong>RELATED TOOLS<\/strong><\/p>\n<p>Cronitor &#8211; <strong>Crontab.Guru<\/strong> [<a href=\"https:\/\/crontab.guru\/\">Link<\/a>]. The best two-way translator for corn syntax.<\/p>\n<p><strong>AnaCron<\/strong> is a command used to execute commands periodically with a frequency-specified period [<a href=\"https:\/\/sourceforge.net\/p\/anacron\/code\/\">Link<\/a>]. It checks for the last time a periodical command was executed is greater than the defined, not based on a specific time.<\/p>\n<pre>sudo apt install anacron -y\r\ncat \/etc\/anacrontab<\/pre>\n<p>Use <strong>Cronic<\/strong> to wrap the command in <code>corn<\/code> or <code>anacron<\/code>. It will only send email notifications when the wrapped command exits with a non-zero status [<a href=\"https:\/\/github.com\/justincase\/cronic\">Link<\/a>].<\/p>\n<pre>sudo apt install cronic -y<\/pre>\n<p>The <code>at<\/code> command is a scheduler for single-time execution &#8220;at&#8221; an exact time.<\/p>\n<pre>sudo apt install at -y\r\nsudo systemctl status atd<\/pre>\n<p>Example:<\/p>\n<pre>at 10:45\r\nat&gt; echo `date` &gt; \/tmp\/output.txt<\/pre>\n<p>Use <strong>Ctrl+D<\/strong> to exit.<\/p>\n<p>You can use the command <code>atq<\/code> to see the queue of commands and <code>atrm<\/code> to remove any scheduled job.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You have one command or one program that you want it to be executed automatically [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=175"}],"version-history":[{"count":11,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":4781,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/175\/revisions\/4781"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}