0% found this document useful (0 votes)
4 views

Linux UNIT 2 Automatting Task

The document provides an overview of automating tasks in Linux administration, focusing on the boot process and service management. It details the stages of booting, including BIOS, MBR, GRUB, kernel initialization, and runlevel management, as well as tools for controlling services at boot. Additionally, it covers manual service management and task scheduling using the 'at' command.

Uploaded by

muthamadhavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Linux UNIT 2 Automatting Task

The document provides an overview of automating tasks in Linux administration, focusing on the boot process and service management. It details the stages of booting, including BIOS, MBR, GRUB, kernel initialization, and runlevel management, as well as tools for controlling services at boot. Additionally, it covers manual service management and task scheduling using the 'at' command.

Uploaded by

muthamadhavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

BSC CS TY LINUX ADMINSTRATION

Unit II Automating Tasks

1. Running Services at Bootup-Beginning the Boot Loading Process


2. Booting into the Default Run level
3. Understanding init Scripts and the Final Stage of Initialization
4. Controlling Services at Boot with Administrative Tools
5. Starting and Stopping Services Manually
6. Scheduling Tasks

1. Running Services at Bootup:- Beginning the Boot Loading Process:

• An operating system (OS) is the software that manages resources,


controls peripherals, and provides basic services to other software.
• In Linux, there are 6 distinct stages in the typical booting process.

1
BSC CS TY LINUX ADMINSTRATION

1. BIOS
• BIOS stands for Basic Input/Output System. In simple terms, the BIOS loads and
executes the Master Boot Record (MBR) boot loader.
• When you first turn on your computer, the BIOS first performs some integrity checks of
the HDD or SSD.
• Then, the BIOS searches for, loads, and executes the boot loader program, which can be
found in the Master Boot Record (MBR).
• The MBR is sometimes on a USB stick or CD-ROM such as with a live installation of
Linux.
• Once the boot loader program is detected, it's then loaded into memory and the BIOS
gives control of the system to it.

2. MBR
• MBR stands for Master Boot Record, and is responsible for loading and
executing the GRUB boot loader.
• The MBR is located in the 1st sector of the bootable disk, which is
typically /dev/hda or /dev/sda depending on your hardware.
• The MBR also contains information about GRUB, or LILO in very old
systems.
• The Master Boot Record (MBR) is the information in the first sector of a
hard disk or a removable drive.
• It identifies how and where the system's operating system (OS) is located
in order to be booted (loaded) into the computer's main storage or random
access memory (RAM).
2
BSC CS TY LINUX ADMINSTRATION

Understanding the Master Boot Record


• Simply put, the MBR is a boot sector category that provides information about
the hard disk partitions.
• It also provides information about the OS so it can be loaded for the system boot.
• The MBR contains programs that determine which partition on the hard disk is
used for the system boot.
• Without the MBR, the system is unable to start.
• The MBR is about 512 bytes. As the first sector of the hard disk, it has a specific
address: Cylinder 0, Head 0, Sector 1.
• The MBR can function as a chain boot loader independently of the OS.
• The MBR is sometimes called the partition sector or the master partition table
because it includes a table that locates each partition on the storage media or hard
disk drive.
• Other common names include sector zero, master boot block and master
partition boot sector.

3
BSC CS TY LINUX ADMINSTRATION

3. GRUB

• Sometimes called GNU GRUB, which is short for GNU Grand Unified
Bootloader, is the typical boot loader for most modern Linux systems.
• The GRUB splash screen is often the first thing you see when you boot your
computer.
• It has a simple menu where you can select some options.
• If you have multiple kernel images installed, you can use your keyboard to select
the one you want your system to boot with.
• By default, the latest kernel image is selected.
• The splash screen will wait a few seconds for you to select and option.
• If you don't, it will load the default kernel image.
• In many systems you can find the GRUB configuration file at
/boot/grub/grub.conf or /etc/grub.conf.

4. Kernel
• The kernel is often referred to as the core of any operating system, Linux
included. It has complete control over everything in your system.
• In this stage of the boot process, the kernel that was selected by GRUB first
mounts the root file system that's specified in the grub.conf file.
• Then it executes the /sbin/init program, which is always the first program to be
executed. You can confirm this with its process/program id (PID), which should
always be 1.
• The kernel then establishes a temporary root file system using Initial RAM Disk
until the real file system is mounted.

4
BSC CS TY LINUX ADMINSTRATION

5. Init

• At this point, your system executes runlevel programs.


• At one point it would look for an init file, usually found at /etc/inittab to decide
the Linux run level.
The startup process
• The startup process follows the boot process and brings the Linux computer up
to an operational state in which it is usable for productive work.
systemd
• systemd is the mother of all processes and it is responsible for bringing the Linux
host up to a state in which productive work can be done.
• First, systemd mounts the filesystems as defined by /etc/fstab, including any
swap files or partitions.
• It uses its configuration file, /etc/systemd/default.target, to determine which
state or target, into which it should boot the host.
• The default.target file is only a symbolic link to the true target file.
• For a desktop workstation, this is typically going to be the graphical.target,
which is equivalent to runlevel 5.
• For a server, the default is more likely to be the multi-user.target which is like
runlevel 3.
• The emergency.target is similar to single user mode.”
• Modern Linux systems use systemd to choose a run level instead.

5
BSC CS TY LINUX ADMINSTRATION

• These are the available run levels.


Run level 0 is matched by poweroff.target
Run level 1 is matched by rescue.target
Run level 3 is emulated by multi-user.target
Run level 5 is emulated by graphical.target
Run level 6 is emulated by reboot.target
Emergency is matched by emergency.target.

• systemd will then begin executing runlevel programs.

6. Runlevel programs

• Depending on which Linux distribution you have installed, you may be able to
see different services getting started.
• These are known as runlevel programs, and are executed from different
directories depending on your run level.
• Each of the 6 runlevels described above has its own directory:

• Run level 0 – /etc/rc0.d/


• Run level 1 – /etc/rc1.d/

• Run level 2 – /etc/rc2.d/


• Run level 3 – /etc/rc3.d/
• Run level 4 – /etc/rc4.d/
• Run level 5 – /etc/rc5.d/
• Run level 6 – /etc/rc6.d/

6
BSC CS TY LINUX ADMINSTRATION

• The sysinit.target and basic.target targets can be considered as checkpoints in the


startup process

• The sysinit.target starts up all of the low-level services and units required for the
system to be marginally functional and that are required to enable moving on to the
basic.target
• After the sysinit.target is fulfilled, systemd next starts the basic.target, starting all
of the units required to fulfil it.
• The basic target provides some additional functionality by starting units that are
required for the next target.
• Finally, the user-level targets, multiuser.target or graphical.target can be
initialized.

2. Booting into the Default Run level:

• By default, most of the LINUX based system boots to runlevel 3 or runlevel 5.


• In addition to the standard runlevels, users can modify the present runlevels or
even create new ones according to the requirement.
• A runlevel is one of the modes that a Unix-based, dedicated server or a VPS
server OS will run on.
• Each runlevel has a certain number of services stopped or started, giving the user
control over the behaviour of the machine.
• Conventionally, seven runlevels exist, numbered from zero to six.

7
BSC CS TY LINUX ADMINSTRATION

Standard run levels

• The chkconfig tool is used in RedHat based systems (like CentOS) to control
what services are started at which runlevels.
• Running the command chkconfig –list will display a list of services whether they
are enabled or disabled for each runlevel.

8
BSC CS TY LINUX ADMINSTRATION

• Single User mode is a mode that a multi-user system (like a Linux server) can
be booted into the operating system as a superuser.
• Booting a system into this mode does not start networking, but can be used to
make changes to any configuration files on the server.
• One of the most common usages for single-user mode is to change the root
password for a server on which the current password is unknown.
• Runlevels are an important part of the core of the Linux operating system.

3. Understanding init Scripts and the Final Stage of Initialization:

init
• It is the first process executed by the kernel during the booting of a system.
• It is a daemon process which runs till the system is shutdown. That is why, it is
the parent of all the processes.
• First of all, init reads the script stored in the file /etc/inittab.
• Command init reads the initial configuration script which basically take care of
everything that a system do at the time of system initialization like setting the
clock and so on.
• By reading this file, init determines how the system should be set up in each
runlevel and sets default run level.
• After determining default runlevel for the system, init starts all background
processes required to run the system.
• First it runs each of the kill script (their file name starts with a K) with a stop
parameter.
• Then it runs all start scripts (their file name starts with an S) to start all services
and applications.

9
BSC CS TY LINUX ADMINSTRATION

4. Controlling Services at Boot with Administrative Tools:


• As the master control file for system, /etc/inittab and its corresponding system of symbol
links control system services.
• We can manage /etc/inittab and its symbolic links following graphical non
graphical administrative tools.
1) chkconfg: -based small script that helps us configure system services.
2) Ntsysv: A graphical interface for the chkconfig configuration script.
3) System-config-segrvices:A full graphical configuration client.

1) Using chkconfig the text based Command line tool

• The chkconfig is an effective, text based command line tool,which can be used
to display, diagnose, or change the starting & stopping of system services
available under /etc/rc.d/init.d in each runlevel.
Example: To list all services installed use the following command

List All Services

• To list all services, execute the command without options or run:


chkconfig --list

10
BSC CS TY LINUX ADMINSTRATION

• Using --list with the chkconfig command displays the status of all available
services in all runlevels.
List All Enabled Services

• To find all enabled services (in one or multiple runlevels), execute combine
chkconfig --list with the grep command:
chkconfig --list | grep on

list all the services running in runlevel 5 with:


chkconfig --list | grep 5: on

11
BSC CS TY LINUX ADMINSTRATION
• start the ntpd service on the third runlevel and verify the service’s configuration
with:
• chkconfig --level 3 ntpd on chkconfig --list ntpd

2) Managing Services using ntsysv


• The ntsysv utility is a command-line application with a simple text user interface
to configure which services are to be started in selected runlevels.
• To start the utility, type ntsysv at a shell prompt as root.
• ntsysv is a simple interface for configuring runlevel services which are also
configurable through chkconfig.
• By default, it configures the current runlevel.

• If the user would like to configure other runlevels, those levels can be specified
on the command line by listing the levels after --level, without any spaces.

• For example, the option --level 016 edits runlevels 0, 1, and 6.

• A service is considered to be started in the runlevel set if it is started in any of the


runlevels which are being edited.
• The ntsysv window normally contains a Cancel button. If --back is specified, a
Back button appears instead.
• ntsysv returns 0 on success, 2 on error, and 1 if the user cancelled (or backed out
of) the program.

12
BSC CS TY LINUX ADMINSTRATION

Fig . Managing Services using ntsysv

3) Using system-config-srvices

• Linux provides its own service Configuration tool for the control and
administration of services.
• We can access this tool from the GUI System Settings, and then select Services
option, or with command-line using system-config-services command.
• The "Service Configuration" dialog is available from the menu

(System > Administration > Services) or directly from the command line by
running the system-config-services command.
• The "Enable" and "Disable" buttons are used to toggle the auto-start on reboot
for each service. The "Start", "Stop" and "Restart" buttons affect the current
state of the service.

Fig.- Managing Services using system-config-services graphical tool

13
BSC CS TY LINUX ADMINSTRATION

14
BSC CS TY LINUX ADMINSTRATION

5. Starting & Stopping Services Manually:


• When we change a configuration file for system services, it’s usually necessary to
stop & restart the service to make it read the new configuration.
• If a service is improperly configured, it is easier to stop & restart it until we
configure it correctly.
• There are several ways to manually start & stop services or to change runlevels.
To quickly manage a service (as root) call the service’s /etc/rc.d/init.d/name on
the command line with the appropriate keyword such as start , status or stop.
• For e.g. To start network service, call the script network like this:
[root@localhost]# /etc/rc.d/init.d/network start
• The script will execute the proper program & report the status of it. Stopping
services is equally easy, & we can also check the status of some services by using
the status keyword like this

• status [root@localhost]# /etc/rc.d/init.d/network network (pid


3156) is running
• To stop service, use the script as
[root@localhost]# /etc/rc.d/init.d/network stop
• A much easier way to manually start or stop a service is to use a script named
service.
• Here we do not have to know the full pathname of the service, only the service
name is sufficient to manipulate it, as shown below.
For e.g. # service apmd start

15
BSC CS TY LINUX ADMINSTRATION

6. Scheduling Tasks :

 Linux at command is used to schedule a task to execute it once at a specified


time, without editing a configuration file.
 It is an alternative to the cron job scheduler. It can be useful if we want to
execute a process after some time, such as a shutdown system, taking backups,
sending emails as reminders, and more.
 The at command is capable of executing a command or script at a specified
time and date, or at a given time interval.
 We can use minutes, hours, days, or weeks to specify the time
 It also allows some keywords such as midnight or teatime (which is used for
'to 4 pm').
 The atq and atrm commands are also a part of the at command.
 Let's understand both commands in brief.
 The atq command:
The atq command is used to display the user's pending tasks. If the user is a
root user, it will list all the tasks. Otherwise, it will list the particular user's job.
The format of displaying output is Task id, date, hour, queue, and username.

 The atrm command:


 It is used to delete tasks, identified by their job number.

Schedule the first task using at

 To schedule a task, execute the at command followed by the time or a keyword.


 For example, to schedule the task print/echo hello at 5 pm,
 execute the command as follows:
16
BSC CS TY LINUX ADMINSTRATION

1. at 5 pm
2. warning: commands will be executed using /bin/sh
3. at> echo “hello”
4. at> ^d

 Press CTRL+D or ^d keys to complete the task.


 The above command will schedule the backup.sh at 10.20 pm.
 Consider the below output:

 the example above, we scheduled a job for 5 PM, which opens the interactive
prompt.
 The scheduled job is to execute the echo command.
 Use the cat command to check if the file exists to make sure the job was executed:
 cat example.txt

17
BSC CS TY LINUX ADMINSTRATION

List the scheduled tasks


 We can list all the scheduled tasks by using the atq command. If we execute it
from the root user, it will list all user's jobs.
 But, if we are not a root user, it will only list the particular user's jobs.
 Execute the command as follows:
# atq
Consider the below output:

 Remove scheduled task


 We can remove a scheduled task by its task id with atrm command.
 To remove a scheduled job, execute the atrm command followed by job id as follows:
SYNTAX- atrm 2

18
BSC CS TY LINUX ADMINSTRATION

Cron job :

What is crontab?
 Cron is named after Greek word “Chronos” that is used for time.
 It is a system process that will automatically perform tasks as per the specific
schedule.
 It is a set of commands that are used for running regular scheduling tasks.
Crontab stands for “cron table”.
 It allows to use job scheduler, which is known as cron to execute tasks.
 Crontab is also the name of the program, which is used to edit that schedule. It
is driven by a crontab file, a config file that indicates shell commands to run
periodically for the specific schedule.
 In this Operating system tutorial, you will learn:

How to use cron in Linux?

 Linux system pack has a useful task scheduler named crontab.


 Crontab is popular because it can be scheduled to run an automated process as root.
Therefore, having an automated process running as root makes system changes
easier.
 You just need to change the task and then wait until the task is re-initiated.

Linux Crontab format


 Crontab of Linux has six fields.
 The first five fields define the time and date of execution, and the 6’th field is used for
command execution.
19
BSC CS TY LINUX ADMINSTRATION

Crontab syntax:

[Minute] [hour]
[Day_of_the_Month]
[Month_of_the_Year]
[Day_of_the_Week]
[command]

• Astrics (*): Use for matching


• Define range: Allows you to define a range with the help of hyphen like 1-10 or 30-40 or
jan,mar, mon-wed.
• Define multiple ranges: Allows you to define various ranges with command separated like
apr,jun,oct-dec.

• How to List Crontab

 Command to view crontab entries of current user


# crontab –l
 Command to view crontab entries of a specific user:
crontab -u username -l

20
BSC CS TY LINUX ADMINSTRATION

Important Crontab Examples

1. This command schedule a task to execute on Monday and Command to


execute on selected days.
* * * * mon
2. Cron scheduler command helps you to execute the task on every 5 AM ON MONDAY
0 5 * * mon
3. It executes the task in the second minute of every day.
0 2 * * mon-sun

21

You might also like