{"id":4138,"date":"2019-09-07T19:40:48","date_gmt":"2019-09-07T16:40:48","guid":{"rendered":"https:\/\/kifarunix.com\/?p=4138"},"modified":"2024-03-12T21:57:09","modified_gmt":"2024-03-12T18:57:09","slug":"scheduling-tasks-using-at-command-in-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/scheduling-tasks-using-at-command-in-linux\/","title":{"rendered":"Scheduling tasks using at command in Linux"},"content":{"rendered":"\n<p>Our previous two guides, whose links are below, we covered how to schedule system tasks or jobs in Linux using the <code>cron<\/code> and <code>anacron<\/code> commands. This guide will focus on scheduling tasks using <code>at<\/code> command in Linux systems.<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-schedule-cron-jobs-tasks-in-linux-unix\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">How to Schedule Cron Jobs\/Tasks in Linux\/Unix<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/scheduling-tasks-using-anacron-in-linux-unix\/\" target=\"_blank\">Scheduling tasks using anacron in Linux\/Unix<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schedule tasks using at command in Linux<\/h2>\n\n\n\n<p><code>at<\/code>&nbsp;utility is used to schedule a one-time task. These are the tasks that are executed only once at a specific time.<br><code>at<\/code>&nbsp;reads commands from standard input or from the file that is specified using the&nbsp;<code>-f<\/code>&nbsp;option.<br>Jobs scheduled using&nbsp;<code>at<\/code>&nbsp;are executed by the&nbsp;<code>atd<\/code>&nbsp;service.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scheduling tasks using at command<\/h3>\n\n\n\n<p>The at command ordinarily expects time as the argument.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at TIME<\/code><\/pre>\n\n\n\n<p>Where TIME can be specified using keywords such as;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>noon<\/code>,&nbsp;<code>midnight<\/code>&nbsp;or&nbsp;<code>teatime (4:00 PM)<\/code>,<\/li>\n\n\n\n<li><code>next week<\/code>,&nbsp;<code>tomorrow<\/code>,&nbsp;<code>teatime tomorrow<\/code>,&nbsp;<code>next tuesday<\/code>,<code>next Tue<\/code>,&nbsp;<code>next Tuesday<\/code>,&nbsp;<code>next month<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>When executed with no commands or scripts passed to it,&nbsp;<code>at<\/code>&nbsp;commands runs interactively and expects you to enter the commands to execute from the prompt.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at 11 PM Dec 2\nat&gt;<\/code><\/pre>\n\n\n\n<p>At the prompt, enter the command to run. For example;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at 11 PM Dec 2\nat&gt; echo \"This is a test at Job &gt; \/tmp\/test-at-job\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at 11 PM Dec 2\nat&gt; df -hT<\/code><\/pre>\n\n\n\n<p>When done typing commands, press Ctrl+D to terminate input. When you press Ctrl+D, you will see&nbsp;<code>&lt;EOT&gt;<\/code>&nbsp;at the command line.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at 11 PM Dec 2\nat> df -hT<EOT><\/code><\/pre>\n\n\n\n<p>You can as well pass the commands to execute to at command from command line by specifying the file with commands using the&nbsp;<code>-f<\/code>&nbsp;option. For example, consider a file called&nbsp;<code>mycommands.sh<\/code>&nbsp;with two shell scripts<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat mycommands.sh\n\/home\/me\/myscripts\/clean-tmp.sh\n\/home\/me\/myscripts\/backup.sh<\/code><\/pre>\n\n\n\n<p>To have at execute these commands next week the same day at midnight;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at -f mycommands.sh midnight next week<\/code><\/pre>\n\n\n\n<p>You can as well use the standard input redirection symbol;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at teatime next month < mycommands.sh<\/code><\/pre>\n\n\n\n<p>You can also pipe tasks\/commands to <code>at<\/code> command. For example to create a notification using the <code>notify-send<\/code> command, <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo 'notify-send \"You need to refill the gas\"' -t 1000 | at 17:00<\/code><\/pre>\n\n\n\n<p>This will send you a notification at 5 PM to refill the gas.<\/p>\n\n\n\n<p><strong>NOTE:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For all the&nbsp;<code>at<\/code>&nbsp;examples that follows below,  you can either pass the the commands or scripts or tasks\/jobs to be executed to at command or you can interactively enter them.<\/li>\n<\/ul>\n\n\n\n<p>To run the job one week from today, same time as when the at job was scheduled;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at next week<\/code><\/pre>\n\n\n\n<p>To run the job at 4 PM tomorrow<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at teatime tomorrow<\/code><\/pre>\n\n\n\n<p>Run a job on next Tuesday, same time as when the job was scheduled.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at next tue<\/code><\/pre>\n\n\n\n<p>Run the job next month, same date at midnight.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at midnight next month<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>using the keyword&nbsp;<code>now<\/code>&nbsp;plus a time period. For example, if to schedule a task to run 4 hours from now, use the time period&nbsp;<code>now + 4 hours<\/code>.<\/li>\n\n\n\n<li>For example;<br>To run the job today at 4PM plus 2 hours, that is at 6PM<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at 4PM + 2 hours<\/code><\/pre>\n\n\n\n<p>To run the job one and half hour from now;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at now + 90 minutes<\/code><\/pre>\n\n\n\n<p>To run the job two minutes from now;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at now + 2 minutes<\/code><\/pre>\n\n\n\n<p>To run a job next on monday 2 hours after the same time when the job was scheduled;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at monday + 2 hours<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>using a time of the day either in&nbsp;<code>24-hour<\/code>&nbsp;or&nbsp;<code>12-hour<\/code>&nbsp;clock system. For example;&nbsp;<code>16:00<\/code>&nbsp;or&nbsp;<code>4:00 PM<\/code>.<\/li>\n\n\n\n<li>For example;<br>To run the job at 5PM today;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at 5:00 PM or at 5 PM or at 17:00<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>using\u00a0<code>time of the day<\/code>\u00a0and\u00a0<code>date<\/code>.\n<ul class=\"wp-block-list\">\n<li>The date can be specified in the format&nbsp;<code>DD.MM.YY<\/code>,&nbsp;<code>MMDDYY<\/code>&nbsp;or&nbsp;<code>MM\/DD\/YY<\/code>.<\/li>\n\n\n\n<li><code>MONTH Date, Year<\/code>&nbsp;or&nbsp;<code>MONTH Date Year<\/code>&nbsp;or&nbsp;<code>MONTH Date<\/code>. You can write the first three letters of the month e.g&nbsp;<code>Jan<\/code>&nbsp;or&nbsp;<code>jan<\/code>.<\/li>\n\n\n\n<li>For example,<br>To run the job on first october, year 2019 same time as when the job was scheduled<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at 12:30 PM 10\/01\/19 or at 12:30 PM 01.10.19 or at 12:30 PM 100119<\/code><\/pre>\n\n\n\n<p>To run the job on 2nd Jan, 2020 same time as when the job was scheduled<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at Jan 1, 2020 ot at Jan 1 2020<\/code><\/pre>\n\n\n\n<p>To run the job at 11 PM on 2nd Dec, this year;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at 11 PM Dec 2<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">List scheduled at jobs<\/h3>\n\n\n\n<p>At command has a utility called&nbsp;<code>atq<\/code>&nbsp;that can be used to list at jobs that are pending execution.<br>To simply list the at jobs, run;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>atq<\/code><\/pre>\n\n\n\n<p>If there are any jobs pending execution, you will see them on the output;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>atq<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>31  Mon Dec  2 23:00:00 2019 a root\n33  Sun Oct  6 16:00:00 2019 a root\n32  Fri Sep 13 00:00:00 2019 a root\n<\/code><\/pre>\n\n\n\n<p>Similarly, you can use at command with&nbsp;<code>-l<\/code>&nbsp;option.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at -l<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Delete Scheduled at jobs<\/h3>\n\n\n\n<p>Scheduled at jobs can be removed using the&nbsp;<code>atrm<\/code>&nbsp;utility or by passing option&nbsp;<code>-r<\/code>&nbsp;to at command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>atr -r &lt;JOB ID><job number=\"\" as=\"\" shown=\"\" by=\"\" atq=\"\"><\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>atrm &lt;JOB ID><job number=\"\" as=\"\" shown=\"\" by=\"\" atq=\"\"><\/code><\/pre>\n\n\n\n<p>For example, based on the output of the&nbsp;<code>atq<\/code>&nbsp;command above, you can remove at job with job number 31 using the command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at -r 31<\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>atrm 31<\/code><\/pre>\n\n\n\n<p>To remove all the jobs in the queue, simply obtain the job numbers and remove them as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>atrm $(atq | cut -f1)<\/code><\/pre>\n\n\n\n<p>There are other options that are aliased to <code>atrm<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at -d &lt;JOB ID&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Print Listed At Jobs to STDOUT<\/h3>\n\n\n\n<p>To print listed at jobs to standard output, simply use the command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at -c &lt;JOB ID&gt;<\/code><\/pre>\n\n\n\n<p>For example, to cat the at job number 23 to standard output.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>at -c 23<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Controlling Access to At<\/h3>\n\n\n\n<p>It is also possible to control who can run at jobs using the&nbsp;<code>\/etc\/at.allow<\/code>&nbsp;and&nbsp;<code>\/etc\/at.deny<\/code>&nbsp;files.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>only users listed in the&nbsp;<code>at.allow<\/code>&nbsp;file are allowed to use&nbsp;<code>at<\/code><\/li>\n\n\n\n<li>users listed in&nbsp;<code>at.deny<\/code>&nbsp;file are not allowed to use&nbsp;<code>at<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Using batch command<\/h3>\n\n\n\n<p>Another command almost similar to at command is&nbsp;<code>batch<\/code>&nbsp;command. This command is used to schedule tasks that can only be executed when system load drops below 1.5 or any value specified in<br>Batch command does not accepts any parameters and runs interactively.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>batch\nat&gt;<\/code><\/pre>\n\n\n\n<p>For example to execute the script,&nbsp;<code>\/home\/me\/myscripts\/clean-tmp.sh<\/code>, simply run&nbsp;<code>batch<\/code>&nbsp;command and enter the script at the&nbsp;<code>at<\/code>&nbsp;prompt.<br>Press Ctrl+d once you are done typing commands;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>batch\nat&gt; \/home\/me\/myscripts\/clean-tmp.sh\nat&gt; <eot><\/eot><\/code><\/pre>\n\n\n\n<p><code>batch<\/code>&nbsp;command is no longer maintained on most Linux\/Unix distributions.<\/p>\n\n\n\n<p>Read more about\u00a0<code>at<\/code>\u00a0command on\u00a0<code>man at<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our previous two guides, whose links are below, we covered how to schedule system tasks or jobs in Linux using the cron and anacron commands.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,1113,1110],"tags":[1116,1117,1118],"class_list":["post-4138","post","type-post","status-publish","format-standard","hentry","category-howtos","category-at-batch","category-automate-system-tasks","tag-at","tag-at-jobs","tag-batch","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4138"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=4138"}],"version-history":[{"count":2,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4138\/revisions"}],"predecessor-version":[{"id":21203,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4138\/revisions\/21203"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=4138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=4138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=4138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}