Operating System Linux

How to Use ‘at’ Command to Schedule a Task on Given or Later Time in Linux

As an alternative to cron job scheduler, the at command allows you to schedule a command to run once at a given time without editing a configuration file.

The only requirement consists of installing this utility and starting and enabling its execution:

# yum install at              [on CentOS based systems]
$ sudo apt-get install at     [on Debian and derivatives]

Next, start and enable the at service at the boot time.

--------- On SystemD ---------
# systemctl start atd
# systemctl enable atd

--------- On SysVinit ---------
# service atd start
# chkconfig --level 35 atd on

Once atd is running, you can schedule any command or task as follows. We want to send 4 ping probes to www.google.com when the next minute starts (i.e. if it’s 22:20:13, the command will be executed at 22:21:00) and report the result through an email (-m, requires Postfix or equivalent) to the user invoking the command:

# echo "ping -c 4 www.google.com" | at -m now + 1 minute

If you choose to not use the -m option, the command will be executed but nothing will be printed to standard output. You can, however, choose to redirect the output to a file instead.

In addition, please note that at not only allows the following fixed times: now, noon (12:00), and midnight (00:00), but also custom 2-digit (representing hours) and 4-digit times (hours and minutes).

For example,

To run updatedb at 11 pm today (or tomorrow if the current date is greater than 11 pm), do:

# echo "updatedb" | at -m 23

To shutdown the system at 23:55 today (same criteria as in the previous example applies):

# echo "shutdown -h now" | at -m 23:55

You can also delay the execution by minutes, hours, days, weeks, months, or years using the + sign and the desired time specification as in the first example.

Time specifications are subject to the POSIX standard.

Summary

As a rule of thumb, use at instead of cron job scheduler whenever you want to run a command or execute a given task at a well-defined time only once. For other scenarios, use cron.

Next, we shall cover how to encrypt tar archive files using openssl, till then stay connected to Tecmint.

', enableHover: false, enableTracking: true, buttons: { twitter: {via: 'tecmint'}}, click: function(api, options){ api.simulateClick(); api.openPopup('twitter'); } }); jQuery('#facebook').sharrre({ share: { facebook: true }, template: '
{total}
', enableHover: false, enableTracking: true, click: function(api, options){ api.simulateClick(); api.openPopup('facebook'); } }); jQuery('#googleplus').sharrre({ share: { googlePlus: true }, template: '
{total}
', enableHover: false, enableTracking: true, urlCurl: 'https://www.tecmint.com/wp-content/themes/tecmint/js/sharrre.php', click: function(api, options){ api.simulateClick(); api.openPopup('googlePlus'); } }); jQuery('#linkedin').sharrre({ share: { linkedin: true }, template: '
{total}
', enableHover: false, enableTracking: true, buttons: { linkedin: { description: 'How to Use ‘at’ Command to Schedule a Task on Given or Later Time in Linux',media: '/uploads/2016/08/Use-at-Command-to-Schedule-Tasks-in-Linux.png' } }, click: function(api, options){ api.simulateClick(); api.openPopup('linkedin'); } }); // Scrollable sharrre bar, contributed by Erik Frye. Awesome! var shareContainer = jQuery(".sharrre-container"), header = jQuery('#header'), postEntry = jQuery('.entry'), $window = jQuery(window), distanceFromTop = 20, startSharePosition = shareContainer.offset(), contentBottom = postEntry.offset().top + postEntry.outerHeight(), topOfTemplate = header.offset().top; getTopSpacing(); shareScroll = function(){ if($window.width() > 719){ var scrollTop = $window.scrollTop() + topOfTemplate, stopLocation = contentBottom - (shareContainer.outerHeight() + topSpacing); if(scrollTop > stopLocation){ shareContainer.offset({top: contentBottom - shareContainer.outerHeight(),left: startSharePosition.left}); } else if(scrollTop >= postEntry.offset().top-topSpacing){ shareContainer.offset({top: scrollTop + topSpacing, left: startSharePosition.left}); }else if(scrollTop < startSharePosition.top+(topSpacing-1)){ shareContainer.offset({top: startSharePosition.top,left:startSharePosition.left}); } } }, shareMove = function(){ startSharePosition = shareContainer.offset(); contentBottom = postEntry.offset().top + postEntry.outerHeight(); topOfTemplate = header.offset().top; getTopSpacing(); }; /* As new images load the page content body gets longer. The bottom of the content area needs to be adjusted in case images are still loading. */ setTimeout(function() { contentBottom = postEntry.offset().top + postEntry.outerHeight(); }, 2000); if (window.addEventListener) { window.addEventListener('scroll', shareScroll, false); window.addEventListener('resize', shareMove, false); } else if (window.attachEvent) { window.attachEvent('onscroll', shareScroll); window.attachEvent('onresize', shareMove); } function getTopSpacing(){ if($window.width() > 1024) topSpacing = distanceFromTop + jQuery('.nav-wrap').outerHeight(); else topSpacing = distanceFromTop; } });
Source: tecmint.com
Share story:

Continue reading other guides on How VPS.