[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.

Welcome to the Slackware Documentation Project

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
howtos:general_admin:task_scheduling [2012/09/25 02:14 (UTC)] – moved from the general howtos namespace mfillpothowtos:general_admin:task_scheduling [2015/06/24 19:36 (UTC)] (current) tdrssb
Line 1: Line 1:
 +<!-- Reviewed 2013-10-16 mfillpot -->
 <!-- Add your text below. We strongly advise to start with a Headline (see button bar above). --> <!-- Add your text below. We strongly advise to start with a Headline (see button bar above). -->
 ====== Task Scheduling in Linux ====== ====== Task Scheduling in Linux ======
- 
 =====Overview===== =====Overview=====
- +This article discusses some tools used in a Linux system to schedule tasks to run automatically at specified time intervals or at any given point of time in the future. This primer will not cover these commands in-depth; this is just a brief introduction to using these commands. See the individual HOWTOS for each command for an in-depth look at all relevant options and configurations.
-This article discusses some tools used in a Linux system to schedule tasks to run automatically at specified time intervals or at any given point of time in the future.+
  
 Some task-scheduling daemons used in Linux/UNIX are: Some task-scheduling daemons used in Linux/UNIX are:
-   * ''cron'' -- the periodic scheduler most commonly used +   * ''[[howtos:software:at|at]]'' -- schedule one-time tasks for the future 
-   * ''at'' -- schedule one-time tasks for the future +   * ''[[howtos:software:cron|cron]]'' -- the periodic scheduler most commonly used 
-   * ''anacron'' -- anachronistic cron; a periodic scheduler that doesn't rely on the system being left on 24x7+   * ''[[howtos:software:anacron|anacron]]'' -- anachronistic cron; a periodic scheduler that doesn't rely on the system being left on 24x7 
 + 
 +=====Using at===== 
 +The **//at//** command allows a user to execute commands or scripts at a specified time (required) and date (optional). The commands can be entered via standard input, redirection, or file.  
 + 
 +<code> 
 +darkstar:~% at 
 +</code> 
 + 
 +=====Interactive at===== 
 +Using the command **//at//** with standard input (keyboard) is a little more complicated than typing one line in at the prompt. The command uses an internal "sub-shell" to gather the required information. Once the command information entry is complete, <key>Ctrl-D</key> (EOT) will signify entry completion. The **//-m//** flag specifies a mail message will be sent to the user when the job is finished, regardless if any output was created. 
 + 
 +<code> 
 +darkstar:~% at 12:01 -m 
 +warning: commands will be executed using (in order) a) $SHELL b) login shell c) /bin/sh 
 +at> ./my_script.sh 
 +at> <EOT> 
 +job 4 at 2015-06-22 12:01  
 +darkstar:~%  
 +</code> 
 + 
 +=====File-driven at===== 
 +Commands can also be contained within a file and run by **//at//**: 
 +<code> 
 +darkstar:~% at 12:32 -m -f /usr/local/bin/my_script.sh 
 +warning: commands will be executed using (in order) a) $SHELL b) login shell c) /bin/sh 
 +job 8 at 2015-06-22 12:10 
 +</code> 
 +The **-m** flag will email the user after completion of the command; the **-f** flag specifies the command will read the job from a file, not from standard input. After the command is typed in (and the appropriate warning is displayed), the **//at//** job number((As distinct from a process ID (PID) known to the operating system)) is displayed. 
 + 
 +=====at Internal Scheduling===== 
 +The job numbers provided after a command is typed in, or when a file is read, allow the user to know which internal job will be run in sequential order. If a user wants to delete a specific task, all that needs to be known is this internal job number. To remove the job, the command **//atrm//** (**__at__ __r__e__m__ove**) is used: 
 + 
 +<code> 
 +darkstar:~%  at -l 
 +7      2015-06-22 12:10 p tux 
 +8      2015-06-22 12:15 p root 
 +</code> 
 + 
 +The command **//atq//** (**__at__ __q__ueue**) is the same as **//at -l//**: 
 + 
 +<code> 
 +darkstar:~%  atq 
 +7      2015-06-22 12:10 p tux 
 +8      2015-06-22 12:15 p root 
 +</code> 
 + 
 +To remove the user job, use **//atrm//** with the job number: 
 +<code> 
 +darkstar:~%  atrm 7 
 +</code> 
 + 
 +=====Using cron===== 
 + 
 +**//cron//** is a daemon that runs tasks in the background at specific times. For example, if you want to automate downloads of patches on a specific day (Monday), date (2 July), or time (1300), **//cron//** will allow you to set this up in a variety of ways. The flexibility inherent in **//cron//** can allow administrators and power users to automate repetitive tasks, such as creating backups and system maintenance. 
 + 
 +**//cron//** is usually configured using a //crontab// file. The following command will open your user account //crontab// file:  
 + 
 +<code> 
 +darkstar:~% crontab -e 
 +</code> 
 + 
 +To edit the system-level //crontab//, first log into the root account: 
 + 
 +<code> 
 +darkstar:~# crontab -e 
 +</code> 
 + 
 +If your system has **//sudo//** installed, type in: 
 + 
 +<code> 
 +darkstar:~% sudo crontab -e  
 +</code> 
 + 
 +The //crontab// file syntax is: 
 + 
 +<code> 
 + # * * * * *  command to execute 
 + # │ │ │ │ │ 
 + # │ │ │ │ │ 
 + # │ │ │ │ └───── day of week (0 - 6) (Sun(0) /Mon (1)/Tue (2)/Wed (3)/Thu (4)/Fri (5)/Sat (6)) 
 + # │ │ │ └────────── month (1 - 12) 
 + # │ │ └─────────────── day of month (1 - 31) 
 + # │ └──────────────────── hour (0 - 23) 
 + # └───────────────────────── min (0 - 59) 
 +</code> 
 + 
 +Using an asterisk in any placeholder location, will match any value. For example, the following will run //example_script.sh// at noon (1200) everyday during the first three months of the year: 
 + 
 +<code> 
 +#For more information see the manual pages of crontab(5) and cron(8) 
 +
 +# min hr day month weekday command 
 +
 +
 +  
 +0 11 * 1-3 * /home/user/example_script.sh 
 +</code> 
 + 
 +=====Using anacron===== 
 +<note> 
 +**//anacron//** is not installed in Slackware by default.((See [[http://slackbuilds.org/repository/13.37/system/anacron/|Slackbuilds.org]] for more information on **//anacron//** on Slackware))  
 +</note> 
 + 
 +**//anacron//** is unique from **//cron//** in the respect that it does not expect the operating system to be running continuously like a 24x7 server. If the time of execution passes while the system is turned off, **//anacron//** executes the command automatically when the machine is turned back on. The reverse is **not** true for **//cron//** - if the computer is turned off during the time of scheduled execution, **//cron//** will not execute the job. Another key difference between **//anacron//** and **//cron//** is the minimum chronological "granularity" - **//anacron//** can only execute jobs by //day//, versus the ability of **//cron//** to execute by the //minute//. Finally, **//anacron//** can __only__ be used by root, while **//cron//** can be used by root and normal users.
  
 ====== Sources ====== ====== Sources ======
Line 15: Line 118:
 <!-- * Original source: [[http://some.website.org/some/page.html]] --> <!-- * Original source: [[http://some.website.org/some/page.html]] -->
 <!-- Authors are allowed to give credit to themselves! --> <!-- Authors are allowed to give credit to themselves! -->
- * Originally written by [[wiki:user:vharishankar | vharishankar]]  +  * Originally written by [[wiki:user:vharishankar | vharishankar]] 
-<!-- * Contrbutions by [[wiki:user:yyy User Y]] --> +  * Contrbutions by [[wiki:user:mfillpot |mfillpot]], [[wiki:user:tdrssb |tdrssb]] 
 +  * Example crontab example modified from [[en.wikipedia.org/wiki/cron]]
 <!-- Please do not modify anything below, except adding new tags.--> <!-- Please do not modify anything below, except adding new tags.-->
 <!-- You must also remove the tag-word "template" below. Otherwise your page will not show up in the Table of Contents --> <!-- You must also remove the tag-word "template" below. Otherwise your page will not show up in the Table of Contents -->
-{{tag>howtos scheduling needs_attention author_vharishankar}}+{{tag>howtos task_scheduling needs_attention author_vharishankar author_mfillpot}} 
 howtos:general_admin:task_scheduling ()