Tutorial8: Links / Process Management
LINKING FILES / MANAGING PROCESSES
Main Objectives of this Practice Tutorial
- Define the term i-node as it relates to the Unix/Linux File System
- Issue the ls -i command to view i-node ID numbers associated with Unix/Linux files
- Define the terms Hard and Symbolic Links
- Issue the ln command to create hard and symbolic links
- Define term process as it relates to the Unix/ Linux operating system
- Run and terminate processes in the foreground and background
- Display and manipulate background and foreground processes
- Use alias and history commands in Unix/Linux
Tutorial Reference Material
Course Notes |
Concepts / Commands |
YouTube Videos | ||
Course Notes:
|
Links
Managing Processes |
Linux Commands | Brauer Instructional Videos: |
KEY CONCEPTS
i-node (index) ID Number of a File
The i-node number is like a finger-print, and is considered to be unique for each file on the Unix / Linux file system.
Referring to the diagram on the far right, issuing the ls command with the -i option displays the i-node number for each file. You can see that each file (whether it is a directory or regular file) has its own unique
i-node number.
Hard Links
A Hard link is a reference to the physical data on a file system.
It does this by creating a file that shares the same i-node number with the original file.
Advantages of hard links are that if one hard link remains (even if original file has been removed), the data in that hard linked file is NOT lost, as well as hard linked files will automatically be updated to that of the original file since they share the same i-node number.
Disadvantages of hard links are that they take-up extra space,
you cannot hard link directories, and you cannot hard link files from other Unix/Linux servers
(since the inode number may already be used by the other Unix/Linux server).
Examples:
touch myfile.txt
ln myfile.txt myfile1.hard.lnk
ln myfile.txt myfile2.hard.lnk
ln myfile.txt ~/backups/myfile.hard.lnk
Symbolic Links
A Symbolic Link is an indirect pointer to a file and are also known as soft link or symlink. The symbolic link file contains the pathname to the original file.
Advantages of symbolic links are that they are shortcuts to other files, where the symbolic link only contains the pathname to the original file, you can create symbolic links
on different Unix/Linux servers, and that you can create symbolic links for directories.
Disadvantages of symbolic links are that they are NOT good for backup purposes
since a symbolic link can point to a nonexistent file (referred to as a "broken link").
Examples:
touch otherfile.txt
ln -s otherfile.txt otherfile1.sym.lnk
ln -s otherfile.txt otherfile2.sym.lnk
ln -s otherfile.txt ~/backups/otherfile.sym.lnk
Managing Processes
All commands/programs (tasks) that are running on a Unix/Linux computer system are referred to as processes.
Characteristics of Processes:
- Each process has an owner
- Each process has a unique ID (PID)
- Processes keep their PID for their entire life.
- Usually a parent sleeps (i.e. suspended) when a child is running (the exception is when the child process is running in the background)
- UNIX / Linux processes are hierarchical. The process structure can have child processes, great grandchild processes, etc.
Users can manage processes to become more productive while working in the Unix / Linux Command-line environment.
Common Linux commands / keyboard shortcuts to manage processes:
Linux Command / Key Combination | Purpose |
---|---|
ps | The ps (process status) command displays snapshot information about processes. By default, the ps command displays information only about the current terminal (ps -l provides a detailed listing, ps -U username shows all) Examples: ps , ps -l , ps -ef , ps -u , ps aux |
top | The top command provides a realtime status of running processes. NOTE: You can press ctrl-c to exit |
fg | The fg (foreground) command moves a background job from the current environment into the foreground. The fg command issued without arguments will place the most recent process in the background to the foreground. Example: fg %job-number |
ctrl-c | Terminates a process running in the foreground |
ctrl-z | Sends a process running in the foreground into the background. |
bg | The bg utility resumes suspended jobs from the current environment. The bg command issued without arguments will run the most recent process that was placed into the background. Example: bg %job-number |
jobs | The jobs utility displays the status of jobs that were started in the current shell environment Examples: jobs [1]+ Stopped vim a <-- Job #1 (+ most recent process / background) [2] Running sleep 200 & <-- Job #2 [3] Running sleep 300 & <-- Job #3 [4]- Running sleep 400 & <-- Job #4 (- second recent process / background) |
kill | The kill command sends the specified signal to the specified processes or process groups. If no signal is specified, the TERM signal is sent. The default action for this signal is to terminate the process. Examples: kill PID , kill -9 PID , kill %job-number , kill -9 %job-number |
Aliases / Command History
Alias:
Assigns a nickname to an existing command or group of commands.
Examples:
alias (Alias command without an argument will display all the aliases currently set)
alias dir=ls
alias ls='ls -al'
alias clearfile='cat /dev/null >'
unalias alias-name (removes alias from memory)
Command History:
The filename ~/.bash_history stores recently executed command lines
Examples of commands that use command history:
up arrow or down arrow | move to previous command or next command within Bash shell prompt |
fc -l | display last 16 commands |
history | more | display all stored commands |
!num | re-execute an issued command number by command number (determined from history command) |
!xxx | re-execute an issued command beginning with string "xxx" |
INVESTIGATION 1: LINKING FILES
In this section, you will learn how to create hard links and symbolic links on your Matrix account.
Perform the Following Steps:
- Login to your matrix account.
- Issue a command to confirm you are located in your home directory.
- Issue the following Linux command to create a directory called: ~/links:
mkdir ~/links - Issue the ls command to confirm that the directory ~/links exists.
- Use a text editor to create a file called: ~/links/data-file.txt
- Enter the following text displayed below:
This is line 1
This is line 2
This is line 3 - Save your editing session and exit your text editor.
- Issue the following Linux command:
ls -li ~/links/data-file.txt
View the i-node number for this file. What does this i-node number represent?
We will now create a hard link file and demonstrate how creating hard links are useful for back-ups. - Issue the following Linux command to create the following hard link in the same directory:
ln ~/links/data-file.txt ~/links/data-file.hard.lnk - Issue the following Linux command to display i-node ID numbers for both files:
ls -i ~/links/data-file.txt ~/links/data-file.hard.lnk
What do you notice about both of those file's i-node numbers? - Use a text editor to edit ~/links/data-file.txt and add some lines to the bottom of that file.
- Save your editing session and exit your text editor.
- Issue the following Linux command:
cat ~/links/data-file.hard.lnk
You should notice that the hard linked file also contains the additional line(s) that you added to the original file.
This is very useful for backing up your files without using the cp command! - Use a text editor to edit the hard-linked file ~/links/data-file.txt.hard.lnk and add some lines to the bottom of this file.
- Save your editing session and exit your text editor.
- Issue the following Linux command:
cat ~/links/data-file.hard.lnk
What do you noticed what happened to this original file? - Issue the following Linux command to create a hard-linked file in your home directory:
ln ~/links/data-file.txt ~/data-file.hard.lnk - Issue the ls -i command to determine the i-node number for the file called ~/data-file.hard.lnk
What do you notice about this file's i-node number? - Issue the following Linux command to check that you created those hard links:
bash /home/murray.saul/scripts/week8-check-1
If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.
If you receive a congratulation message that there are no errors, then proceed with this tutorial. - Issue the following Linux command to remove the ~/links directory and its contents:
rm -rf ~/links - Issue a Linux command to confirm that the ~/links directory has been removed.
- Issue the following Linux command to view the contents of your linked file in your home directory:
cat ~/data-file.hard.lnk
What do you notice? What does this tell you about hard links? - Issue the following Linux command to create a directory called ~/links2:
mkdir ~/links2 - Issue the ls command to confirm that the directory called ~/links2 exists.
- Use a text editor to create a file called ~/links2/text-file.txt
- Enter the following text displayed below:
This is line one
This is line two
This is line three - Save your editing session and exit your text editor.
- Issue the following Linux command to create the following symbolic link in the same directory:
ln -s ~/links2/text-file.txt ~/links2/text-file.sym.lnk - Issue the ls -li command for the ~/links2 directory.
What do you notice about both of these file's i-node numbers?
What do you notice about the size of the file ~/links2/text-file.sym.lnk?
What pathname do you think it represents? - Change to your home directory.
- Issue the following Linux command to create the following symbolic link in your home directory:
ln -s ~/links2/text-file.txt ~/text-file.sym.lnk - Issue the ls -l command for the ~/text-file.txt.sym.lnk file.
What do you notice? What is the file size?
What pathname do you think this file contains? - Issue the following Linux command:
cat ~/text-file.txt.sym.lnk
What did you notice? - Issue the following Linux command to check that you created those symbolic links:
bash /home/murray.saul/scripts/week8-check-2
If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.
If you receive a congratulation message that there are no errors, then proceed with this tutorial. - Issue the following Linux command to remove the ~/links2 directory:
rm -rf ~/links2 - Re-issue the Linux command you performed in step #31.
What happened? Why? - Issue the following Linux command:
ls -l ~/text-file.txt.sym.lnk
What do you notice? - Issue the following Linux command: ln -s ~uli101 linux
- Issue the following Linux command: linux/assign1
What happened? Why?
- In the next investigation, you will learn how to manage processes on your Matrix server.
INVESTIGATION 2: MANAGING PROCESSES
In this section, you will learn how to manage processes on a Unix / Linux server.
Perform the Following Steps:
- Make certain that you are logged into your Matrix account.
- Issue the following Linux command: sleep 700
NOTE: The sleep command waits for a specified number of seconds before returning to the shell prompt. It is useful in order to force a pause in a sequence of commands. - Notice that this process will run for 700 seconds, and is forcing the user to wait until this process finishes.
A process that is running in the terminal is referred to as a foreground process.
The Unix/Linux system is designed to allow users to send preemptive signals to manage those processes. - Press the key combination: <ctrl><c>
You should notice that the process that was running in the foreground has been interrupted (i.e. terminated). NOTE: The ctrl-c key combination sends SIGINT (Signal Interrupt) to terminate a process that is running. - Reissue the Linux command: sleep 700
- Press the key combination: <ctrl><z>
- You should now see output similar to what is displayed below:
[1]+ Stopped sleep 700
NOTE: This indicates that this process has been placed into the background.
This is useful in order to "free-up" the terminal to run other commands. - Clear your bash shell terminal screen.
- Issue the following Linux command: jobs
You should see the following output similar that was displayed above:
[1]+ Stopped sleep 700
This display indicates that this process (that is now in the background) has stopped.
In other words, the sleep command is NOT counting-down to zero to terminate.
NOTE: You need to use the bg command to run that process that was sent into the background. - Issue the following Linux command: bg
NOTE: You can use the bg command WITHOUT arguments to specify the most recent process
that was placed into the background. From the jobs command, the process that has a plus sign "+"
indicates the most recent process placed into the background. - Issue the following Linux command: jobs
You should see the following output similar that was displayed above:
[1]+ sleep 700 &
This display indicates that this process in the background is running in the background
(denoted by the ampersand character "&"). Now the command is counting-down to zero. - Issue the following Linux command: fg
You should notice that the sleep command is now running in the foreground.
You can run commands with ampersand "&" to run processes in the background.
- Press the key combination: <ctrl><c> to terminate the process running in the foreground.
- Issue the following Linux command: sleep 500 & sleep 600 & sleep 700 &
- Issue the jobs command. What do you notice?
- Issue the following Linux command: kill %1
NOTE: You can specify job number preceded by percent % with the
kill, bg, and fg commands to specify the processes' job number. - Issue the jobs command. What do you notice?
- Issue the following Linux commands:
kill %2
kill %3 - Issue the jobs command. What do you notice?
You can also manipulate processes by their PID (process ID). - Let's use grouping to run several commands in sequence within a single process.
- Issue the following Linux command: (sleep 400; sleep 500; sleep 600) &
- Issue the jobs command. What do you notice?
- Issue the following Linux command: ps
What is the PID of the previously issued Linux command in the background? - Issue the following Linux command (using the PID number instead of "PID"):
kill PID - Issue the jobs command. Is the sleep process still running?
NOTE: You need to issue the kill -9 PID command if the previous commands did not work. - Issue the following Linux command (using the PID number instead of "PID"):
kill -9 PID - Issue the jobs and ps commands to confirm there are no processes running in the background.
- In the next investigation, you will learn how to create aliases and view command history on your Matrix server.
INVESTIGATION 3: ALIASES / COMMAND HISTORY
In this section, you will learn how to manage aliases and Linux command history on your Matrix account.
Perform the Following Steps:
- Make certain that you are logged into your Matrix account.
- Issue the following Linux command: alias
Take a few moments to view existing aliases that have previously been declared.
Take a few moments to run those aliases to see what happens. - Issue the following to create an alias: alias lal='ls -al'
- Issue the following alias: lal
What do you notice? - Issue the following to create another alias (lowercase l and h):
alias lh='ls --human-readable --size -1 -S --classify' - Issue the following command to confirm that this newly-created alias is stored in memory:
alias | grep "lh" - Issue the following alias: lh
What do you think this command does? - Logout of your Matrix account, than log into your Matrix account.
- Reissue the lh alias. What happened?
- Issue the alias command without any arguments to see if it is stored in memory.
- Reissue the command to create the lh alias in step #5.
- Run the lh alias to confirm that it is properly set in memory.
- Issue the following Linux command to edit your ~/.bashrc startup file:
nano ~/.bashrc - Add the following line at the bottom of this file:
alias lh='ls --human-readable --size -1 -S --classify' - Save your editing changes and exit your text editor.
- Logout of your Matrix account, than log into your Matrix account.
- Reissue the lh alias. What happened?
- Issue the following Linux command: unalias lh
- Run the lh alias to see what happens.
What happenned? - Logout of your Matrix account, than log into your Matrix account.
- Reissue the lh alias. What happened? Why?
- Issue the following Linux command to check your work:
bash /home/murray.saul/scripts/week8-check-3
If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.
If you receive a congratulation message that there are no errors, then proceed with this tutorial. - Issue the following Linux command: history | grep "lh"
What do you notice? - Press an exclamation mark ! immediately followed by the number by one of those commands listed in the history list and press ENTER
What happened? - Type the following: !unalias and press ENTER
What happened? - Issue the following Linux command: history | grep "lh"
What happened? - After you complete the Review Questions sections to get additional practice, then work on your
online assignment 2 and complete section4 labelled: Linking files and directories.
LINUX PRACTICE QUESTIONS
The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.
Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:
https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week8_practice.docx
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
Review Questions:
- Hard Links:
- What is the purpose of creating a hard-link?
- What is a limitation of a hard link?
- Write a single Linux command to create a hard link called ~/backup/myfile.txt.lnk for the existing file called ~/myfile.txt
- Write a single Linux command to display the i-node number for both files. Are the i-node numbers identical?
- Symbolic (Soft) Links:
- What is the purpose of creating a symbolic (soft) link?
- What is a limitation of a symbolic (soft) link?
- Write a single Linux command to create a symbolic link called ~/shortcuts/murray.saul.lnk
to the existing directory called ~murray.saul - Are the i-node numbers identical for both of those files?
- What data is contained in the file called ~/shortcuts/murray.saul.lnk?
- Background / Foreground Processes:
- Write a single Linux command to run the program called ~/clean.sh in the background.
- Write a single Linux command to place the previously issued program in the foreground.
- Write a single Linux command to confirm that this program is running in the background.
- What key-combination would you issue to send that program again into the background?
- Write a single Linux command to have that process sent into the background to continue running?
- Managing Background processes:
Use the following diagram to answer the accompanying questions.
Each of the following questions will use the diagram below and are treated as independent situations.
[1] Stopped vim a
[2]- Stopped vim b
[3]+ Stopped vim c- Write a single Linux command to bring the second-recently process placed in the background into the foreground.
- Write a single Linux command to terminate job #3.
- Write a single Linux command to display running processes in “real-time”.
- Write a single Linux command to terminate a process that has the following PID: 22384
- Aliases / History:
- Write a linux command to create an alias called ld that issues the command: ls -ld
- Write a linux command to unset the alias created in the previous question.
- Issue a Linux command to list history of commands that match the pattern called touch.
- Create a table listing each Linux command, useful options and command purpose for the following Linux commands:
ln , ps , top , fg , bg , jobs , kill , alias , unalias , history