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

CourseNotes_Linux CentOS 7 Shells and Processes (1)

The course 'Linux CentOS 7: Shells and Processes' provides an in-depth understanding of Linux shells and system processes, focusing on efficiency and customization. Key topics include different shell types, variable management, command history, process monitoring, and management techniques. The course is led by instructor Grant McWilliams, who covers practical applications and advanced features of the Bash shell and Linux processes.

Uploaded by

20cse0129
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

CourseNotes_Linux CentOS 7 Shells and Processes (1)

The course 'Linux CentOS 7: Shells and Processes' provides an in-depth understanding of Linux shells and system processes, focusing on efficiency and customization. Key topics include different shell types, variable management, command history, process monitoring, and management techniques. The course is led by instructor Grant McWilliams, who covers practical applications and advanced features of the Bash shell and Linux processes.

Uploaded by

20cse0129
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Course Title: Linux CentOS 7: Shells and Processes

Description: In this course, gain a more thorough understanding of the shell and
system processes to help you work faster and more efficiently in Linux. Follow
computer science instructor and Linux enthusiast Grant McWilliams as he dives into
Linux shells and their environments, and explains how to customize your shell. He
discusses using the Bash shell, and covers topics like command and variable
substitution. Grant also explains how to stack simpler commands together using
named and unnamed pipes and redirects; discusses how to start, pause, and end
processes; shows how to schedule one-time jobs and recurring jobs; and covers
complex system services.

***********************************************
Chapter: 1. Linux Shells Overview
***********************************************

-----------------------------------------------
Video: Shell types
-----------------------------------------------
Note Time: Note Text:

0:02:35 Types of Shells:

Bourne Shell (sh): One of the earliest shells, basic but POSIX-compliant.
C Shell (csh): Resembles the C programming language, but not commonly used for
scripting anymore.
Korn Shell (ksh): Introduced job control and command history.
Bash (Bourne Again Shell): The most popular shell, combines features from Bourne
and Korn shells and is mostly POSIX-compliant.
Dash: A lightweight shell, uses less memory, popular on Debian-based systems.
Fish: Focuses on user-friendliness with features like syntax highlighting and
command suggestions.
Zsh: Similar to Bash but with enhanced features like better command completion and
pattern matching.

-----------------------------------------------
Video: Variables and shell environment
-----------------------------------------------
Note Time: Note Text:

0:00:12 There are two types of variables accessible in a shell session.

Environmental variables are variables that are defined for the


current shell and are inherited by any child processes or child shells.

Shell variables are variables that are contained exclusively


within the shell in which they were defined. They are often used to keep track of
data like the current working directory.

0:01:54 Creating and Exporting Variables:

Create a shell variable: VAR=TEST


Make it an environmental variable: export VAR
To remove an environmental variable: export -n VAR
To remove a shell variable: unset VAR
0:01:54 printenv --> to see enviroment variables
set --> to see enviroment variables

0:03:54 Shell Options:

View all shell options: set -o


Change a shell option: set -o posix
Unset a shell option: set +o posix To change the
behavior of set do posix mode, which doesn't show functions.
these values only survive for the current login session. To make them persistent,
we need to add them to one of the bash startup files. Bash stores it's
configuration settings in multiple startup files. Different files are processed,
depending on how the shell starts.

-----------------------------------------------
Video: Shell tips and tricks
-----------------------------------------------
Note Time: Note Text:

0:02:30 To quickly move between the beginning and end of the line, just
use the Home and End keys

0:02:38 alt + b to go backwards and alt + f to go forwards.

-----------------------------------------------
Video: Shell history tricks
-----------------------------------------------
Note Time: Note Text:

0:02:03 see what commands you've typed in,--> use the history command
to last command u typed --> use !!
to show last n line command -->!-n
to show nth line --> !n
show the line starting with ls--> !ls
If you want to run a command with a previous line's arguments and options--> you
can append !* to the command name --> example : cd !*

-----------------------------------------------
Video: Configure shell history
-----------------------------------------------
Note Time: Note Text:

0:00:04 A faster way to search through previous commands is using the


reverse i search. Press ctrl + r,

0:01:54 when we don't want to add commands to our command history:


export HISTCONTROL=ignorespace --> any command starts with space is not recorded
in history. export HISTCONTROL=ignoredups --> option is ignore duplicate
commands. export HISTCONTROL=
erasedups --> To get rid of all duplicates, in history
You can add more than one value to HISTCONTROL by separating them with a colon.

If you wanted to erase duplicates and lines


starting with a space:
export HISTCONTROL ="erase dups: ignorespace".
0:02:16 To keep certain lines from being recorded, we can place them in
the hist ignore variable. export, space HISTIGNORE equals double quote, history
asterisk, this is going to ignore any lines that start with the word history and
are followed by any number of characters

0:02:16 To keep certain lines from being recorded, we can place them in
the hist ignore variable. export, space HISTIGNORE equals double quote, history
asterisk, this is going to ignore any lines that start with the word history and
are followed by any number of characters

0:03:46 By default our history file's 1,000 lines.


If you'd like to increase this --> Command export HISTSIZE = 10,000.

If you wanted it unlimited, --> set it to a negative value(-1).

If we want to turn off recording of our history, --> set it to zero,

0:03:46 to include time stamps in history


export HISTTIMEFORMAT " %h %d %H:%M:%S>"d

To get the list of possible variables, --> man strftime --> You can
see the variables that are available to customize your date and time format.

0:05:16 our history is saved and the file named .bash_history, in our
home directory.
We can choose how many lines are saved in this file by this command:
export HISTFILESIZE = 10,000.

Set this to zero if you want to turn off saving of your command
history on the disk.
All of these changes we've made so far are just for our current
login session, -- > make it permanent open the directory --> insert mode --> type
same commands

0:05:16 our history is saved and the file named .bash_history, in our
home directory.
We can choose how many lines are saved in this file by this command:
export HISTFILESIZE = 10,000.

Set this to zero if you want to turn off saving of your command
history on the disk.
All of these changes we've made so far are just for our current
login session,

-----------------------------------------------
Video: Pattern matching with globs
-----------------------------------------------
Note Time: Note Text:

0:02:10 In Bash, pattern matching is called file globbing,


* matches any number of characters.
? matches exactly one character.
Square brackets [] can be used to match a range or list of characters.
! --> to not match the list

0:03:29 ls file[0-9].txt --same-- ls file[[:digit]].txt

0:03:46 to match both digits and spaces --> ls file[[:digit]


[:space]].txt

0:03:46 to match both digits and spaces --> ls file[[:digit]


[:space]].txt

0:04:45 to match multiple file--> use braces extension --> ls


{*.png,*gif}

-----------------------------------------------
Video: Pattern matching with extended globs
-----------------------------------------------
Note Time: Note Text:

0:01:14 Extended globs give us more pattern matching power.

They grant us the power to specify the number of occurrences to match.

They also allow grouping matches. Patterns can be more than one
character.

0:03:29 file?(abc).txt matches zero or one occurrence of "abc".


file+(abc).txt matches one or more occurrences of "abc".
+(*.jpg|*.gif) matches files ending in either ".jpg" or ".gif".
!(+(photo|file)*+(.jpg|.gif)) matches files that do not start with "photo" or
"file" and do not end with ".jpg" or ".gif".

***********************************************
Chapter: 2. Using the Bash Shell
***********************************************

-----------------------------------------------
Video: Escape characters and quotes
-----------------------------------------------
Note Time: Note Text:

0:01:23 To show the value of a variable, you use the echo command with a
$ sign before the variable name.

0:01:39 Single quotes tell Bash to treat everything inside them


literally, ignoring special meanings.
Example: echo 'My name is $USER' will display "My name is $USER" (it won't replace
$USER with your username).

0:01:39 Single quotes tell Bash to treat everything inside them


literally, ignoring special meanings.
Example: echo 'My name is $USER' will display "My name is $USER" (it won't replace
$USER with your username).

0:02:47 A backslash is used to escape a single character,

-----------------------------------------------
Video: Brace and path substitution
-----------------------------------------------
Note Time: Note Text:
0:01:14 Brace Expansion:

Braces {} can be used to create patterns.


Example: echo S{P,E}LL will output "SPELL" and "SELL".

0:01:14 Directory Stack:

Use pushd to add directories to the stack.


Use dirs to view the stack.
Use popd to remove directories from the stack.
Access directories in the stack using ~n, where n is the directory's position in
the stack.

0:01:14 Tilde Substitution:

~ expands to the home directory.


~+ expands to the current directory.
~- expands to the previous directory.

-----------------------------------------------
Video: Named and unnamed pipes
-----------------------------------------------
Note Time: Note Text:

0:01:45 If we want to send output from a command in one terminal to


another command in a different terminal, we can use a named pipe or FIFO. FIFO
stands for first in, first out.

0:03:37 in one terminal : mkfifo named_pipe--> echo "hi">named_pipe


second terminal : cat named_pipe --> prints hi

-----------------------------------------------
Video: File redirects and tees
-----------------------------------------------
Note Time: Note Text:

0:03:24 &> sends all output of screen to file


tee helps to send o/p to two places

***********************************************
Chapter: 3. Linux Processes
***********************************************

-----------------------------------------------
Video: Introduction to processes
-----------------------------------------------
Note Time: Note Text:

0:00:08 A program is an executable file that is stored on disk

When a process is created by running a program, it's allocated some


system resources including memory and a process ID number, or PID.

Every process has a unique ID number. Each process has a parent process
that started it.
A process can spawn other processes called child processes.

The process ID number is used by the kernel to manage and control


the process during its lifetime.

System administrators use the process ID for changing task


priority and ending tasks.
Processes are organized in a hierarchical manner, just like files
and folders. So the child processes are nested under the parent processes, and so
on.

When a process ends, it is reported back to the parent process. Its


resources are freed, and the process ID is removed.

-----------------------------------------------
Video: Monitor processes using ps
-----------------------------------------------
Note Time: Note Text:

0:06:59 Basic Usage of ps:

Typing ps in the terminal shows processes run by the current user, displaying
columns like process ID (PID), terminal, execution time, and command.

Syntax Options:

ps has three syntax types: Unix (e.g., -e), BSD (no dashes, e.g., e), and GNU
(double dashes, e.g., --format).

Displaying All Processes:

Use ps -e to show every process on the system.


Adding -H (Hierarchy) shows parent-child relationships with indentation.

Detailed Information:

ps -ef adds more details like user name, parent process ID, CPU usage, start time,
and command with arguments.
ps -eF includes memory usage and the CPU the process is running on.
ps -elF provides even more columns of information.

Customizing Output:

You can customize the output with --format. For example, ps -e --format
uid,pid,ppid,%cpu,cmd shows user ID, process ID, parent process ID, CPU
utilization, and command.
Sorting can be done with --sort. For instance, ps -e --sort %cpu sorts processes by
CPU usage.

Filtering by User or Group:

To show processes for a specific user, use ps -U username (e.g., ps -U root).


For group filtering, use ps -G groupname.

Filtering by Command:
To display processes for a specific command, use ps -C commandname (e.g., ps -C
firefox).

-----------------------------------------------
Video: Monitor processes in real time
-----------------------------------------------
Note Time: Note Text:

0:00:01 for displaying real-time process information called TOP


Command: top
Display: The screen updates every few seconds, showing system uptime, load average,
number of processes, CPU usage, and memory usage.

Shortcuts and Customizations


Load Average/Uptime: Press L to show or hide this line.
CPU Usage for All Cores: Press 1 to toggle between showing CPU usage for all cores.
Task and CPU States: Press T to toggle between task and CPU states.
Memory Displays: Press M to toggle between different memory displays.

Fields and Sorting


Modify Fields: Press f to enter the fields menu. Use the spacebar to
select/deselect fields and arrow keys to navigate.
Sort Fields: Press S to change the sort field. For example, sort by Parent Process
ID.
Move Fields: Select a field, press the right arrow key to move it, and then press
the left arrow key to place it.

Interacting with Processes


Kill a Process: Press K, enter the process ID (PID), and choose a kill signal
(e.g., 9 for SIGKILL).
We can also select a kill signal. SIGTERM is the default. This is a very friendly
way of killing a process. If that doesn't work, we can send it as SIGKILL signal
which forcibly removes the process.

Renice a Process: Press R, enter the PID, and set a new nice value to
change its priority.
The higher the nice value, the nicer the process is to the CPU and
thus less priority it gets. Lowering the nice value increases its priority.
Quick Sorting
Sort by Memory Usage: Press M.
Sort by CPU Usage: Press P.
Sort by Running Time: Press T.
Sort by Process ID: Press N.

Filtering and Navigation


Filter by User: Press U, type the user name, and hit Enter to show processes by
that user.
Toggle Command Name/Line: Press C to switch between command name and command line.
Scroll Through Tasks: Use the up/down arrow keys or Page Up/Page Down keys.

Quit: Press Q to quit top.

Additional Resources
Manual Page: For more features, type man top
-----------------------------------------------
Video: Monitor processes graphically
-----------------------------------------------
Note Time: Note Text:

0:00:00 monitor processes and other system resources graphically with a


tool called GNOME System Monitor. GNOME System Monitor has three tabs: Processes,
Resources, and File Systems. The
middle tab is for resources. Namely: CPU, memory, and network usage history.

-----------------------------------------------
Video: Manage processes
-----------------------------------------------
Note Time: Note Text:

0:00:02 killall -u <username>: Ends all processes run by a specific


user.Example: sudo killall -u your_username.

0:00:02 killall -u <username>: Ends all processes run by a specific


user.Example: sudo killall -u your_username.

0:00:02 kill -USR1 <PID>: Sends the USR1 signal to a process. Example:
kill -USR1 $(pidof dd)

0:00:02 killall <process_name>: Kills all processes with the specified


name.

0:00:02 Sending Signals to Processes:

kill: Use kill <PID> to send a signal to a process. By default, it sends a SIGTERM
signal (15) to terminate the process.
List Signals: kill -l shows all possible signals.
Common Signals:
SIGTERM (15): Gracefully terminate a process.
SIGKILL (9): Forcefully kill a process.
SIGHUP: Often used to reload configuration files.

0:00:02 Finding Process IDs (PID):

pidof: Use pidof <process_name> to get the PID of a specific process. Example:
pidof firefox.
pgrep: Similar to pidof, but more powerful. Example: pgrep firefox.

-----------------------------------------------
Video: Process priority
-----------------------------------------------
Note Time: Note Text:

0:00:02 Starting a Process with a Nice Level:

Command: nice -n <nice_level> <command>


Example: nice -n 10 top (starts top with a nice level of 10).

Changing Nice Level of a Running Process:

Command: renice <new_nice_level> -p <PID>


Example: sudo renice 5 -p 4772 (changes the nice level of process with PID 4772 to
5).

Command: nice -n 2 top


Note: A positive nice level of 2 means the process is less demanding on the CPU.(+2
means type -2 and for negative --2);

Changing Nice Level:

Command: sudo renice 5 -p <PID>

0:00:02 Starting a Process with a Nice Level:

Command: nice -n <nice_level> <command>


Example: nice -n 10 top (starts top with a nice level of 10).

Changing Nice Level of a Running Process:

Command: renice <new_nice_level> -p <PID>


Example: sudo renice 5 -p 4772 (changes the nice level of process with PID 4772 to
5).

Command: nice -n 2 top


Note: A positive nice level of 2 means the process is less demanding on the CPU.(+2
means type -2 and for negative --2);

Changing Nice Level:

Command: sudo renice 5 -p <PID>

0:00:02 Nice Levels:


Range: -20 (highest priority) to 19 (lowest priority).
Default: 0.
Higher Nice Number: Lower CPU priority (process is "nicer" to other processes).
Lower Nice Number: Higher CPU priority (process demands more CPU time).

-----------------------------------------------
Video: Manage process jobs
-----------------------------------------------
Note Time: Note Text:

0:00:11 Starting a Process with a Nice Level:

Command: nice -n <nice_level> <command>


Example: nice -n 10 top (starts top with a nice level of 10).

Changing Nice Level of a Running Process:

Command: renice <new_nice_level> -p <PID>


Example: sudo renice 5 -p 4772 (changes the nice level of process with PID 4772 to
5).

Command: nice -n 2 top


Note: A positive nice level of 2 means the process is less demanding on the CPU.(+2
means type -2 and for negative --2);
Changing Nice Level:

Command: sudo renice 5 -p <PID>

0:00:58 Watch executes a command every two seconds until terminated.

0:02:10 cltrl +z --> suspend the process

***********************************************
Chapter: 4. Job Scheduling
***********************************************

-----------------------------------------------
Video: One-time jobs using at and batch
-----------------------------------------------
Note Time: Note Text:

0:00:00 Create a Batch Job:


Type batch and press Enter.

At the prompt, type the commands you want to run. For example:
sh
touch ~/batchfile.txt

Press Ctrl + D to save the batch job.

Verify Jobs: Use atq to list batch jobs. If the job doesn't appear, it has already
run.
Check Job Completion: Verify the job ran by checking if the file was created with
ls -l ~/batchfile.txt.

0:00:00 Create a Job:


Type at now + 5 minutes and press Enter.

You'll get a prompt where you can type the commands you want to run. For example:
sh
mkdir ~/Documents.bak
rsync -a ~/Documents/ ~/Documents.bak/

Press Ctrl + D to save the job.

Verify Jobs: Use atq to list scheduled jobs.


View Job Details: Use at -c [job number] to see the job's details.
Cancel a Job: Use atrm [job number] to remove a scheduled job.

0:00:00 Create a Job:


Type at now + 5 minutes and press Enter.

You'll get a prompt where you can type the commands you want to run. For example:
sh
mkdir ~/Documents.bak
rsync -a ~/Documents/ ~/Documents.bak/

Press Ctrl + D to save the job.


Verify Jobs: Use atq to list scheduled jobs.
View Job Details: Use at -c [job number] to see the job's details.
Cancel a Job: Use atrm [job number] to remove a scheduled job.

0:00:00 There are two different types of scheduled jobs: One


time jobs and recurring jobs.
For one time jobs we use a service called at. The at service runs at jobs at a
certain time. Or in the case of a batch job, when the CPU load average drops below
0.8.
The syntax: at <time format>

0:00:07 jobs Command: This command lists all the jobs you have started
in the current terminal session.
bg Command: This command resumes a stopped job in the background.
fg Command: This command brings a background job back to the foreground,
killall Command: This command terminates all processes.

0:00:07 Manage process jobs


Background Process: Sometimes, you might want to run a task in the background so
you can continue using the terminal for other commands. You can do this by pressing
Control + Z to stop the process and then using the bg command to resume it in the
background.

-----------------------------------------------
Video: Reccuring user jobs using cron
-----------------------------------------------
Note Time: Note Text:

0:00:00 Create/Edit Crontab: Use crontab -e to open the crontab file in


the default text editor. Verify Crontab: Use crontab -l to list
the current crontab entries.

0:00:00 Run a task every day at 1 AM:

0 1 * * * rsync -a ~/Documents/ ~/Documents.bak/

Run a task every 10 minutes:

*/10 * * * * your_command

0:00:00 minute hour day_of_month month day_of_week command

Minute: (0-59) When the task will run within the hour.
Hour: (0-23) When the task will run within the day.
Day of Month: (1-31) When the task will run within the month.
Month: (1-12 or JAN-DEC) When the task will run within the year.
Day of Week: (0-6 or SUN-SAT) When the task will run within the week.
Command: The command to execute.

0:00:00 Types of crontabs


User Crontab:

Managed by individual users.


Stored in /var/spool/cron/username.
No need for elevated privileges to manage.

System Crontab:
Managed by the superuser.
Stored in /etc/cron.d.

0:00:00 What is cron?


cron is a service used to schedule recurring tasks in Linux. These tasks
can run every minute, hour, day, week, or month.

-----------------------------------------------
Video: Reccuring system jobs using cron
-----------------------------------------------
Note Time: Note Text:

0:04:10 Shortcuts for Common Intervals


Hourly: Place scripts in /etc/cron.hourly
Daily: Place scripts in /etc/cron.daily
Weekly: Place scripts in /etc/cron.weekly
Monthly: Place scripts in /etc/cron.monthly

-----------------------------------------------
Video: Limit access to AT and cron jobs
-----------------------------------------------
Note Time: Note Text:

0:01:35 By default, the allow file does not exist so all users are
allowed.

0:02:12 We can also control access to the cron service using pluggable
authentication modules or PAM. PAM provides a modular authentication system for all
Linux services.

***********************************************
Chapter: 5. System Services
***********************************************

-----------------------------------------------
Video: Introduction to system services
-----------------------------------------------
Note Time: Note Text:

0:00:50 System services are processes that are started by the OS and sit
in the background waiting to answer requests. These services might include web
servers, file servers, mail servers, and others. In Linux, a system service is
called a daemon,

-----------------------------------------------
Video: Get systemd service status
-----------------------------------------------
Note Time: Note Text:

0:00:21 System D objects are called units and for each unit, there's a
unit file for configuration.

0:00:49 The command that System D uses to manage these units is system
CTL.
0:01:33 the service state which can be either enabled disabled or
static. Enabled means it will start automatically at boot. Disabled means it will
not start automatically. Static means the service is not enabled and has no
provisions to be enabled.

-----------------------------------------------
Video: Manage systemd services
-----------------------------------------------
Note Time: Note Text:

0:02:11 EXTRA
Basic Commands for Managing Services
Check All Services:

systemctl list-unit-files -t service: Lists all services and shows if they are
enabled or disabled.

Stop a Service:

sudo systemctl stop [service_name]: Stops a running service.


Example: sudo systemctl stop atd stops the ATD service.

Check Service Status:

systemctl status [service_name]: Shows if a service is running or stopped.


Example: systemctl status atd shows the status of the ATD service.

Start a Service:

sudo systemctl start [service_name]: Starts a stopped service.


Example: sudo systemctl start atd starts the ATD service.

Restart a Service:

sudo systemctl restart [service_name]: Restarts a service.


Example: sudo systemctl restart atd restarts the ATD service.

Check if a Service is Active:

systemctl is-active [service_name]: Checks if a service is currently running.


Example: systemctl is-active atd checks if the ATD service is active.

Prevent a Service from Running:

sudo systemctl mask [service_name]: Prevents a service from being started.


Example: sudo systemctl mask atd prevents the ATD service from running.

Allow a Service to Run Again:

sudo systemctl unmask [service_name]: Allows a previously masked service to run.


Example: sudo systemctl unmask atd allows the ATD service to run again.

0:02:25 EXTRA
Understanding systemctl Commands
systemctl list-unit-files -t service: Shows all service unit files and their status
(enabled, disabled, or static).
Enabled: Starts automatically at boot.
Disabled: Does not start automatically.
Static: Cannot be enabled; runs only when needed.

systemctl list-units -t service: Lists all active service units.

Columns:
Service Name: Name of the service.
Loaded: Whether the unit file is loaded.
Active: General state (active, inactive).
Sub: Detailed state (running, exited).
Description: Brief description of the service.

systemctl list-units -t service --state running: Lists only the currently running
services.

systemctl cat [service_name]: Displays the unit file for a specific service.

Example: systemctl cat rsyslog shows the configuration for the rsyslog service.

systemctl status [service_name]: Provides detailed status information for a


specific service.

Example: systemctl status rsyslog shows the status, process ID, and log messages
for the rsyslog service.

Summary
Use systemctl commands to check the status and configuration of services.
list-unit-files shows all services and their startup settings.
list-units shows active services.
status gives detailed info about a specific service.

-----------------------------------------------
Video: Make systemd services persistant
-----------------------------------------------
Note Time: Note Text:

0:00:00 Check if a Service is Enabled:systemctl is-enabled


[service_name] Check Service Status:
systemctl status [service_name]
List Services: systemctl list-unit-files -t service
here atd is service Disable
a Service: sudo systemctl disable atd Re-enable a Service: sudo systemctl
enable atd Verify
Service Status: systemctl status atd

You might also like