Sie sind auf Seite 1von 13

SCHEDULING JOBS

&& LEARN WRITING


SHELL SCRIPTS
HOW TO SCHEDULE TASKS IN
LINUX SYSTEMS
To run Linux systems efficiently, especially in a production environment,
demands several tasks to run simultaneously, and some at another
specified time. These tasks keep the system well maintained and running
smoothly. The Linux operating system has built-in commands that can
take care of all scheduled tasks .
From time to time, an administrator or end user will want to run a
command or series of commands at a set point in the future. Examples
include office workers who want to schedule an email to the boss, as
well as systems administrators working on firewall configurations who
put a ‘safety’ job in place to reset the firewall settings in ten minutes,
unless they deactivate the job before that. These scheduled commands
are often called tasks or jobs.
Scheduling one-time tasks with the at command :
For users of a Red Hat Enterprise Linux (RHEL) system, at is a
solution for scheduling future tasks. This is not a standalone tool,
but rather a system daemon (atd), with a set of command-line
tools to interact with the daemon (at, atq and more). In a default
RHEL installation, the atd daemon is installed and enabled
automatically. The atd daemon can be found in the at package.
Users (including the root) can queue up jobs for the atd daemon
using the at command-line tool. The atd daemon provides 26
queues, a to z, with jobs in alphabetical order; the later queues
get less system priority.
 The service of at named atd installed and enabled by default
in linux , you can ensure this by type :
#systemctl status atd
 Structure of at command : #at TIME then type your job .
For example :
#at 20:07
at> touch /home/training/Desktop/f1
Then press ctrl+d to end at schedule .
Note : By default time format is 24 hours , if you want 12 hours
type : 8:07 PM
 Inspecting jobs: Running the atq command will give a job
scheduled to run in the future, a number, date and time
schedule, as well as the queue for the job and its owner.

 Removing jobs: The command atrm <JOBMUMBER> will


remove a scheduled job. This is useful when a job is no longer
needed—for example, when a remote firewall configuration
has succeeded and does not need to be reset.
A new job can be scheduled by using the command at <TIMESPEC>. at
will then read the commands to execute the stdin. For larger commands,
and those that are case-sensitive, it is often easier to use input redirection
from a script’s file, e.g., at now +5 min <myscript,rather than typing all
the commands manually in a terminal window. When entering
commands manually, you can finish your input by pressing CTRL+D.
<TIMESPEC> allows for many powerful combinations, giving users an
(almost) free-form way of describing exactly when a job should be run.
Typically, they start with a time, e.g., 02:00pm, 15:59, or even teatime,
followed by an optional date or the number of days in the future.
EXAMPLES :
Scheduling jobs: Normal users can use the crontab command to
manage their jobs. This command can be called in four different
ways:
Job format :
When editing jobs with crontab-e, an editor will be started (vi by
default, unless the EDITOR environment variable has been set to
something different). The file being edited will have one job per
line. Empty lines are allowed, and comments start their line with a
hash symbol (#). Environment variables can also be declared,
using the format NAME=value, and will affect all lines below the
line in which they are declared.
Individual jobs consist of five fields, which detail when and what
should be executed. When all five of the first fields match the
current date and time, the command in the last field will be
executed. The fields are in the following order:
Minute hours day_of_month day_of_week command
Important: When the „Day-of-month‟ and „Day-of-week‟ fields are both other
than *, the command will be executed when either of these fields match. This
can be used, for example, to run a command on the fifteenth of every month,
and on every Friday.
The first five of these fields all use the same syntax rules: * for “Don’t Care”/
always.
There is a number to specify the number of minutes or hours, a date, or a
weekday. (For weekdays, 0 equals Sunday, 1 equals Monday, 2 equals
Tuesday, etc. 7 also equals Sunday. x-y is used for a range, inclusive of x and
y.) x, y for lists: Lists can include ranges as well, e.g., 5, 10-13, 17 in the
„Minutes‟ column, to indicate that a job should run at 5 minutes past the hour,
10 minutes past, 11 minutes past, 12 minutes past, 13 minutes past, and 17
minutes past.
*/x indicates an interval of x, e.g., */7 in the „Minutes‟ column will run a job in
exactly every seven minutes.
Additionally, three-letter English abbreviations can be used for both months
and weekdays, e.g., Jan, Feb and Tue, Wed.
The last field contains the command to be executed. This command will be
executed by /bin/sh unless a SHELL environment variable has been declared.
EXAMPLES OF CRON JOBS:
 Execute ever Friday at 1 am :
So if we want to schedule the script to run at 1AM every Friday,
we would need the following cronjob:

The script is now being excuted when the system clock hits :
0 (minute) 1 (hours) * (every day of month) * (every month)
5 (friday)
So if we want to schedule the script to Sunday till Thursday at 1
AM, we would need the following cronjob:
0 1 * * 0-4 date

Special words :

Das könnte Ihnen auch gefallen