0% found this document useful (0 votes)
2 views17 pages

linux-day3

Linux Material study guide

Uploaded by

mohammedb.kassab
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)
2 views17 pages

linux-day3

Linux Material study guide

Uploaded by

mohammedb.kassab
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/ 17

Linux Administration

Azza Khalel
[email protected]
Azza Khalel | LinkedIn
Day3 contents
• Processes, priorities and signals Concepts .
• Bash shell properties
• Shell history
• Auto complete
• File globbing
• Alias
• I/O & error redirection
• Piping
• Environment variables [HOME, PATH, PWD,SHELL, 0, PS1, PS2]
• Initialization and startup files
• Job control
Processes
• Every program you run creates a process (shell, command, an application)
• Everything that happens on a Linux server, a process is started.
• System starts processes called daemons which are processes that run in the
background and provide services.
• Every processes has a PID.
• When a process creates another, the first is the parent of the new process. The
new process is called the child process.
• In older versions of Linux, killing a parent process would kill all of its child
processes.
• Start from RHEL 8, if you kill a parent process, all of its child processes become
children of the systemd process.
• Types of process:
• Shell jobs.
• Daemons.
• Kernel threads.
Listing processes
• ps (process status) command
• ps [options]
• Outputs
• PID.
• TTY -> terminal identifier.
• Execution time.
• Command name.

• Options
• -e: all system processes.
• -f: full information.
• -u <uid>: display processes of that user.
• – a: all processes attached to a terminal.
• – x:all other processes.

• Search for a process


• you can use pgrep command.
• pgrep option(s) pattern.
• #pgrep vi ➔ search for all vi processes
• #pgrep –l vi ➔ display the name with pid
• #pgrep –u azza ➔ seach for all azza’s processes #pgrep –lu azza
Shell jobs
• Shell jobs are commands started from the command line. They associated
with the shell that was current when the process was started.
• When a user types a command, a shell job is started.
• By default, any executed command is started as foreground job.
• If you know that a job will take a long time to complete, you can start it in
with an & behind it to start it in background

• #jobs ➔ print all jobs and their status in the background


• #sleep 100 ➔ stop shell for 100 sec
• ctrl+z ➔ pause the command and send it to background
• #bg ➔ to run the last job enter background and it's ran in background also
• #bg %<nu> ➔ to run specific pause command from background
• #fg ➔ return the last command from background to foreground
• #fg %<nu> ➔ return specific command from background to foreground
Signals
• A signal is a message sent to a process to perform a certain action.
• To send signals to process or process group , you can use kill command, or pkill
command.
• #Kill -[signal] PID ➔ kill 12047
• #pkill -[signal] process_name ➔ kill -9 mail
• #killall process_name ➔ killall vim
• Signals are identified by a signal number and a signal name, and has an
associated action.
• SIGTERM → 15
• SIGKILL → 9
• SIGINT→ 2 ➔ctrl+c
• SIGSTOP →19 ➔ctrl+z
• SIGCONT→18
• If no signal is specified, the TERM signal is sent.
Process priority
• When Linux processes are started, they are started with a specific priority.
• By default, all regular processes are equal and are started with the same priority,
which is the priority number20.
• Every process which is ready to run has a scheduling priority.
• The Linux process divides CPU time into time slices, in which each process will get
a turn to run, higher priority processes first.
• User can affect the priority by setting the niceness value for a process.
Adjusting priority
• Niceness values range from -20 to +19, which indicates how much of a bonus or
penalty to assign to the priority of the process.
• Use #nice command if you want to start a process with an adjusted priority.
• nice [-n adjustment] command
nice -n 5 dd if=/dev/zero of=/dev/null &
• Use #renice to change the priority for a currently active process, or you can use
the r command from the top utility to change the priority of a currently running
process
• #renice priority [[-p] pid ...] [[-g] group ...] [[-u] user ...]
#ps | aux
#renice -n 10 -p 1234
• The default niceness of a process is set to 0 (which results in the priority value
of 20.
• By applying a negative niceness, you increase the priority.
• Use a positive niceness to decrease the priority.
• Do not set process priority to -20, it risks blocking other processes from getting
served.
• The regular users can only decrease the priority of a running process.
• You must be root to give processes increased priority.
Viewing processes
• Use top to display Linux processes.
• The top program provides a dynamic real-time view of a running system.
• It can display system summary information as well as a list of processes or threads
currently being managed by the Linux kernel.
Standard input and output
• Standard input:
• Refers to the data source from which data is input to a command.
• Typically the keyboard.
• Standard output:
• Refer to data destination to which data from the command is written.
• Typically the screen.
• Standard error:
• Refer to the output destination for the errors and messages generated by the command.
• Typically the screen also.
• In I/O redirection, files can be used to replace the default standard input, standard
output and standard error.
• You can also redirect to device files.
• If you want to discard a command’s output, you can redirect to /dev/null.
Redirection
• Standard input:
• command < fname
• EX
• #mail < file2.txt
• #sort < file2.txt > sortedf1.txt
• Standard output:
• command > fname ➔ remove old content and add new
• command >> fname ➔ append new content to the old
• Standard error:
• command 2> fname
• command 2> /dev/null ➔ to discard output
Piping
• A pipe (|) is used to send the output of one command as the input to another.
• Command 1 | Command 2.
• Examples:
• #ls -lR / | more
• #ps -ef | more
• #history | more
Environment variables
• There are linux system environment variables ➔ its name is all capital
• $HOME➔ Complete path of the user home directory
• $PWD ➔ user current working directory
• $SHELL➔ path name of the default shell.
• 0 ➔ current working shell that user enter
• $PATH ➔ A colon-separated list of directories used by the shell to look for executable
program names.
• $USER ➔ Currently logged in user.
• $HOSTNAME ➔ Name of the computer
• $PS1➔ prompt string of the bash shell
• $PS2➔ has the prompt that print when the command not completed
• ex. When I run this command #ls \
• $$ ➔ has the bash process ID
• When a user logs in, an environment is created.
• The environment consists of some variables that determine how the user is
working
• Such as $PATH, which defines a list of directories that should be searched when a user
types a command.
• I can define my variables which called as user variables ➔ recommended to be
small to differentiate between it and system variable
Viewing Variable Contents
• The shell assumes whatever follows the dollar sign ($) in the command line is a
variable and substitutes its value.
#echo $HOME
• To display the current Environment variables with values use
• #env or #printenv command.
Creating a user environment
• To construct the user environment, a few files play a role:
• Global initialization file: /etc/profile and /etc/bashrc
• /etc/profile: Used for default settings for all users when starting a login shell.
• /etc/bashrc: Used to define defaults for all users when starting a subshell.
• Initialization file: ~/.profile
• ~/.profile: Specific setting for one user applied when starting a login shell.
• Startup files: ~/.bashrc
• ~/.bashrc:Specific setting for one user applied when starting a subshell.
• When logging in, these files are read in this order, and variables and other
settings that are defined in these files are applied.
• If a variable or setting occurs in more than one file, the last one wins.
Linux shell and alias
• The purpose of the linux shell is to provide an environment in which commands
can be executed.
• The shell takes care of interpreting the command that a user has entered
correctly.
• To do this, the shell makes a distinction between three kinds of commands.
• Aliases.
• Alias is a command that a user can define as needed.
• #alias newcommand = ‘oldcommand’ alias ll=’ls -l’
• Alias are executed before anything else.
• Internal commands.
• It is a command that is a part of the shell itself and, as such, doesn’t have to be loaded
from disk separately.
• External commands.
• It is a command that exists as an executable file on the disk of the computer.
• To find out whether a command is a Bash internal or an executable file on
disk, you can use the type command.
• To find out which exact command the shell will be using, you can use the
which command.
• Type alias at the terminal to see all set aliases.
• To remove aliases, you can use unalias command.
Command history
• Bash stores a history of commands you have entered so that you can recall
them later.
• The history is stored in the user's home directory and is called .bash_history
by default.
• You can recall commands by pressing the up arrow key.
• !!➔ Repeats the last command.
• !string ➔ Repeats the last command that started with string.
• !n ➔ Repeats a command by its number in history output.
• !-n ➔ Repeats a command entered n commands back.
• #echo $HISTSIZE

You might also like