Changes

Jump to: navigation, search

Tutorial8: Links / Process Management

2,195 bytes added, 10:53, 2 September 2020
INVESTIGATION 3: ALIASES / COMMAND HISTORY
:* Understand the purpose and why links are used in Unix / Linux
:* Define the term '''inode''' number as it relates to a file on Unix / Linux
:* Define the terms: '''Hard''' Link and '''Symbolic''' Link
:* Issue the '''ln''' command to create '''hard ''' and '''symbolic ''' links
:* Define and understand the purpose of a '''process''' in Unix / Linux
:* '''Display''' and '''manipulate''' background and foreground processes
 
:* Manage '''aliases''' and Unix/Linux command '''history'''
<br>
| style="padding-left:15px;" |Links
* [http://www.linfo.org/hard_link.html#:~:text=A%20hard%20link%20is%20merely,created%20to%20other%20hard%20links. Hard Links]* [https://www.computerhope.com/jargon/s/symblink.htm#:~:text=Alternatively%20referred%20to%20as%20a,or%20directory%20using%20its%20path.&text=In%20Linux%20and%20Unix%20symbolic,created%20using%20the%20mklink%20command. Symbolic Links]<br>
Managing Processes
* Process Information
* [http://man7.org/linux/man-pages/man1/jobs.1p.html jobs]
* [http://man7.org/linux/man-pages/man1/kill.1.html kill]
* [http://linuxcommand.org/lc3_man_pages/aliash.html alias] , [https://man.linuxreviews.org/man1p/unalias.1p.html unalias]
* [https://mediatemple.net/community/products/dv/204404624/using-the-history-command history]
|colspan="1" style="padding-left:15px;" width="30%"|Brauer Instructional Videos:<ul><li>[https://www.youtube.com/watch?v=tZ94-qH9unM&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=9 Inodes and Links]</li><li>[https://www.youtube.com/watch?v=q93POTgH-aQ&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=7 Processes and Jobs]</li></ul>
'''inode (index) Number of a File:'''
<table align="right"><tr valign="top"><td>[[Image:inode-number-1.png|thumb|right|300px|Each inode stores the attributes and disk block locations of the object's data.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]</td><td>[[Image:inode-number-2.png|thumb|right|360px|The '''inode number''' is like a '''finger-print''', and usually is considered to be '''unique''' for each file on the Unix / Linux file system.]]</td></tr></table>
''The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block locations of the object's data. File-system object attributes may include metadata (times of last change, access, modification), as well as owner and permission data.''
The '''inode number''' is like a '''finger-print''', and usually is considered to be '''unique''' for each file on the Unix / Linux file system.<br>Referring to the diagram on the far right, issuing the '''ls''' command with the '''-i''' option<br>displays the inode number for each file. You can see that <u>each</u> file<br>(whether it is a directory or regular file) has its own unique inode number.
<table align="right"><tr valign="top"><td>[[Image:hard-link-1.png|thumb|right|300px|(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])<br>Image manipulated by author]]]</td><td>[[Image:inode-number-3.png|thumb|right|500px|'''Hard links''' share the '''same inode number'''. Hard links are very good for '''backup''' purposes.]]</td></tr></table>
A '''Hard link''' is a '''reference ''' to the physical data on a file system.
'''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''', a as well as hard links linked files will automatically change when a change be updated to that of the original file or hard links occur since they share the same '''i-node number'''<br>and the Unix/Linux OS treats them all as the same file.
'''Disadvantages''' of hard links are that they '''take-up extra space''',<br>you cannot hard link directory filesdirectories, and you cannot hard link files from other Unix/Linux servers<br>(since the inode number may already be used by the other Unix/Linux server).
<table align="right"><tr valign="top"><td>[[Image:symbolic-link-1.png|thumb|right|300px|(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]</td><td>[[Image:inode-number-4.png|thumb|right|500px|Symbolic links do NOT share the same i-node number. '''Symbolic links''' are very good for short-cuts to other files (including directories) but NOT for backup purposes.]]</td></tr></table>
A '''Symbolic Link''' is an '''indirect pointer ''' to a file and are also known as '''soft link''' or '''symlink'''.
'''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<br>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<br>since a symbolic link can point to a nonexistent file (referred to as a "broken link").
[[Image:process-diagram-1.png|thumb|right|300px|(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
All programs that are executing running on a Unix/Linux computer system are referred to as '''processes''':
* Each process has an '''owner'''
* Each process has a unique ID ('''PID''') * Processes in UNIX can run in the '''foreground''' or '''background'''
* Programs / Commands can be run in the '''background''' by placing an '''ampersand''' '''&''' after the command<br>(eg. '''program-name &''')
UNIX processes are '''hierarchical''':
* The process structure has a '''root''', '''parents''', and '''children''', etc.
* Creation of a new process is called '''forking''' or '''spawning'''
* The Parent process can spawn a child process, and children can in turn spawn their own childrenchild processes
* Processes keep their '''PID''' for their entire life
* Usually a parent '''sleeps ''' when a child is executing<br>(the exception is when the child process is executing running in the background)
Users can learn to manage processes to become more productive while working in the Unix / Linux Command-line environment.
Below are common Linux commands / keyboard shortcuts to manage processes:
<table cellpadding="5" width="80%"><tr valign="top"><th width="25%" style="border-bottom: 1px solid black;text-align:left;">Linux Command / Key Combination</th><th style="border-bottom: 1px solid black;text-align:left;">Purpose</th></tr><tr valign="top"><td>'''ps'''</td><td>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)<br><br>''Examples: ''<span style="font-family:courier;font-weight:bold">ps , ps -l , ps -ef , ps -u , ps aux</span><br></td></tr><tr valign="top"><td>'''top'''</td><td>The '''top''' command provides a continuous update including resource usagerealtime status of running processes.<br>'''NOTE:''' You can press '''ctrl-c''' to exit<br></td></tr><tr valign="top"><td>'''fg'''</td><td>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: ''<span style="font-family:courier;font-weight:bold">fg %job-number</span></td></tr><tr valign="top"><td><span style="font-family:courier;">'''&lt;ctrl&gt;&lt;c&gt;'''</span></td><td>'''Terminates''' a process running in the foreground</td></tr><tr valign="top"><td><span style="font-family:courier;">'''&lt;ctrl&gt;&lt;z&gt;'''</span></td><td>Sends a process running in the foreground into the '''background'''.</td></tr><tr valign="top"><td>'''bg'''</td><td>The '''bg''' utility shall resume '''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.<br>''Example: ''<span style="font-family:courier;font-weight:bold">bg %job-number</span></td></tr><tr valign="top"><td>'''jobs'''</td><td>The '''jobs''' utility shall display displays the '''status ''' of jobs that were started in the current shell environment<br>''Examples:<br>''<span style="font-family:courier;">jobs<br>[1]+ Stopped vim a <span style="font-size:8pt;">&nbsp; &lt;-- Job #1 (+ means most recent process sent to background)</span><br>[2]&nbsp; Running sleep 200 & <span style="font-size:8pt;">&nbsp;&lt;-- Job #2</span><br>[3] &nbsp;Running sleep 300 & <span style="font-size:8pt;">&nbsp;&lt;-- Job #3</span><br>[4]- Running sleep 400 &<span style="font-size:8pt;">&nbsp; &lt;-- Job #4 (- means second recent process sent to background)</span><br><br></span></td></tr><tr valign="top"><td>'''kill'''</td><td>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.<br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">kill PID , kill -9 PID , kill %job-number , <br>kill -9 %job-number</span></td></tr></table>
===Aliases / Command History===
'''Command History:'''
The filename '''~/.bash_history''' stores recently executed command lines
<table width="60%"><tr><tdwidth="25%"><span style="font-family:courier;font-weight:bold;><up></span> or <span style="font-family:courier;font-weight:bold;><down></span> </td><td>move Move to '''previous ''' command or '''next ''' commandwithin Bash shell prompt</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>fc -l</span></td><td> display last '''16 ''' commands</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>history| more</span></td><td>display all stored commands</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>!num</span></td><td>'''re-execute ''' an issued command number by command number "num"(determined from ''history'' command)</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>!xxx</span></td><td>'''re-execute last ''' an issued command beginning with string "xxx"</td></tr></table><br><br>
=INVESTIGATION 1: LINKING FILES=
'''Perform the Following Steps:'''
# '''Login''' to your matrix account.<br><br>
# Issue a command to '''confirm''' you are located in your home directory.<br><br>
# Issue the following Linux command to create a directory called '''~/links''':<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir ~/links</span><br><br>
# Use a text editor to create a file called '''~/links/data-file.txt'''<br><br>
# Enter the following text displayed below:<br><br><span style="font-family:courier;font-weight:bold;">This is line 1<br>This is line 2<br>This is line 3<br><br></span>
# Save your editing session and exit your text editor.<br><br># Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links/data-file.txt</span><br><br>Note View the '''i-node''' number for this file. What does this i-node number represent?<br><br>
# Issue the following Linux command to create the following hard link in the same directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln ~/links/data-file.txt ~/links/data-file.hard.lnk<br><br>
# Issue the '''ls -li''' command for the '''~/links''' directory.<br><br>What do you notice about both of these file's i-node numbers?<br><br>
# Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/links/data-file.hard.lnk</span><br><br>What do you noticed what happened to this original file?<br><br>
# Issue the following Linux command to create a hard-linked file in your '''home''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">ln ~/links/data-file.txt ~/data-file.hard.lnk<br><br>
# Issue the '''ls -i''' command to determine the i-node number for the file called '''~/data-file.hard.lnk'''<br><br>What do you notice about this file's ''i-node'' number?<br><br>
# Make certain you are currently located in your '''home''' directory.<br><br>
# Issue the following Linux command to remove the '''~/links ''' directory and its contents: <br><span style="color:blue;font-weight:bold;font-family:courier;">rm -rf ~/links</span><br><br>
# Issue a Linux command to confirm that the '''~/links''' directory has been removed.<br><br>
# Issue the following Linux command to view the contents of your linked file in your '''home''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/data-file.hard.lnk</span><br><br>What do you notice? What does this tell you about hard links?<br><br>
# Issue the following Linux command to create a directory called '''~/links2''':<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir ~/linkslinks2</span><br><br>
# Issue the '''ls''' command to confirm that the directory called '''~/links2''' exists.<br><br>
# Use a text editor to create a file called '''~/links2/text-file.txt'''<br><br>
# Enter the following text displayed below:<br><br><span style="font-family:courier;font-weight:bold;">This is line one<br>This is line two<br>This is line three<br><br></span>
# Save your editing session and exit your text editor.<br><br>
# Issue the following Linux command to create the following symbolic link in the same directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~/links2/text-file.txt ~/links2/text-file.sym.lnk</span><br><br>
# Issue the '''ls -li''' command for the '''~/links2''' directory.<br><br>What do you notice about both of these file's i-node numbers?<br>What do you notice about the size of the file ''~/links2/text-file.sym.lnk''?<br>What pathname do you think it represents?<br><br>
# Change to your '''home''' directory.<br><br>
# Issue the following Linux command to create the following symbolic link in your home directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~/links2/text-file.txt ~/text-file.sym.lnk</span><br><br>
# Issue the '''ls -l ''' command for the '''~/text-file.txt.sym.lnk ''' file.<br><br>What do you notice? What is the file size?<br>What pathname do you think this file contains?<br><br>
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/text-file.txt.sym.lnk</span><br><br>What did you notice?<br><br>
# Issue the following Linux command to remove the '''~/links2 ''' directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">rm -rf ~/links2</span><br><br># Re-issue the Linux command you performed in '''step #3431'''.<br><br>What happened? Why?<br><br>
# Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">ls -l ~/text-file.txt.sym.lnk</span><br><br>What do you notice?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~uli101 linux</span><br><br>
In this section, you will learn how to manage processes on a Unix / Linux server.
 
# Make certain that you are logged into your Matrix account.<br><br>
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">sleep 700</span><br><br>'''NOTE:''' The '''sleep ''' command just waits for a specified number of seconds before completing<br>in order to return returning to the shell prompt. It is useful in order to force a pause in a sequence of commands.<br><br># Notice that this process will run for '''700 seconds''', and is forcing the user to wait until this process finishes.<br>A process that is running in the terminal is referred to as a '''foreground process'''.<br><br>The Unix/Linux system is designed to allow users to send '''preemptive signals ''' to manage those processes.<br><br># Press the key combination: <span style="color:blue;font-family:courier;font-weight:bold">&lt;ctrl&gt;&lt;c&gt;</span><br><br>You should notice that the process that was running in the foreground has been '''interrupted ''' (i.e. terminated).<br>'''NOTE:''' The '''ctrl-c''' key combination sends '''SIGINT''' ('''Signal Interrupt''') to terminate a process that is running.<br><br>
# Reissue the Linux command: <span style="color:blue;font-family:courier;font-weight:bold">sleep 700</span><br><br>
# Press the key combination: <span style="color:blue;font-family:courier;font-weight:bold">&lt;ctrl&gt;&lt;z&gt;</span><br><br>
# You should now see output similar to what is displayed below:<br><span style="font-family:courier;font-weight:bold">[1]+ Stopped sleep 700</span><br><br>'''NOTE:''' This indicates that this process has been placed into the '''background'''.<br>This is useful in order to "'''free-up'''" the terminal to run other commands.<br><br># Clear your bash shell terminalscreen.<br><br># Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">jobs</span><br><br>You should see the following output similar that was displayed above:<br><span style="font-family:courier;font-weight:bold">[1]+ Stopped sleep 700</span><br><br>This display indicates that this process (that is now in the background) has '''stopped'''.<br>In other words, the ''sleep '' command is NOT counting-down to zero to terminate.<br><br>'''NOTE: ''' You need to use the '''bg''' command to run that process that was sent into the background.<br><br>
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">bg</span><br><br>'''NOTE:''' You can use the bg command WITHOUT arguments to specify the most recent process<br>that was placed into the background. From the '''jobs''' command, the process that has a plus sign "+"<br>indicates the most recent process placed into the background.<br><br>
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">jobs</span><br><br>You should see the following output similar that was displayed above:<br><span style="font-family:courier;font-weight:bold">[1]+ sleep 700 &</span><br><br>This display indicates that this process in the background is '''running in the background'''<br>(denoted by the ampersand character "&"). Now the command is counting-down to zero.<br><br># Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">fg</span><br><br>You should notice that the ''sleep '' command is now running in the '''foreground'''.<br><br>You can run commands with ampersand "'''&'''" to run processes in the background.<br><br> # Press the key combination: <span style="color:blue;font-family:courier;font-weight:bold">&lt;ctrl&gt;&lt;c&gt;</span> to stop '''terminate''' the process running in the foreground.<br><br>
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">sleep 500 & sleep 600 & sleep 700 &</span><br><br>
# Issue the '''jobs''' command. What do you notice?<br><br>
# Issue the following Linux commands:<br><span style="color:blue;font-family:courier;font-weight:bold">kill %2</span><br><span style="color:blue;font-family:courier;font-weight:bold">kill %3</span><br><br>
# Issue the '''jobs''' command. What do you notice?<br><br>You can also manipulate processes by their '''PID''' ('''process ID''').<br><br>
# Let's use '''grouping ''' to run several commands in sequence within a single process.<br><br>
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">(sleep 400; sleep 500; sleep 600) &</span><br><br>
# Issue the '''jobs''' command. What do you notice?<br><br>
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">ps</span><br><br>What is the PID of the previously issued Linux command in the background?<br><br>
# Issue the following Linux command (using the PID number instead of "PID"):<br><span style="color:blue;font-family:courier;font-weight:bold">kill PID</span><br><br># Issue the '''jobs''' command. Is the sleep process still running?<br><br>'''NOTE:''' You may need to issue the ''kill -9 PID '' command if the previous commands did not work.<br><br># Issue the following Linux command (using the PID number instead of "PID"):<br><span style="color:blue;font-family:courier;font-weight:bold">kill -9 PID</span><br><br># Issue the '''jobs''' command and '''ps''' commands to confirm there are no processes running in the background.<br><br> 
:In the next investigation, you will learn how to create aliases and view command history on your Matrix server.<br><br>
<br>
=INVESTIGATION 3: ALIASES / COMMAND HISTORY=
<br>
In this section, you will learn how to manage aliases and view and access Linux command history on your Matrix account.
# Make certain that you are logged into your Matrix account.<br><br>
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">alias</span><br><br>Take a few moments to view existing aliases that have previously been declared.<br>Take a few moments to run those aliases to see what happens.<br><br>
# Issue the following to create an alias: <span style="color:blue;font-family:courier;font-weight:bold">alias lslal='ls -al'</span><br><br># Run this the alias '''lal''' to see what it does.<br><br>
# Issue the following to create another alias (lowercase l and h):<br><span style="color:blue;font-family:courier;font-weight:bold">alias lh='ls --human-readable --size -1 -S --classify'</span><br><br>
# Issue the '''alias''' command without any arguments to confirm that it is stored in memory.<br><br>
# Logout of your Matrix account, than log into your Matrix account.<br><br>
# Reissue the '''lh''' alias. What happened? Why?<br><br>
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">history</span><br><br>What do you notice?<br><br># Press an exclamation mark ! immediately followed by the number by one of those commands listed in the history list and press <span style="color:blue;font-family:courier;font-weight:bold">ENTER</span><br><br>What happened?<br><br># Type the following: <span style="color:blue;font-family:courier;font-weight:bold">!unalias</span> and press <span style="color:blue;font-family:courier;font-weight:bold">ENTER</span><br><br>What happened?<br><br># Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">history | grep ln</span><br><br>What happened?<br><br># After you complete the Review Questions sections to get additional practice, then work on your<br>'''online assignment 2 ''' and complete '''section4 ''' labelled: '''Linking files and directories'''.<br><br>
= LINUX PRACTICE QUESTIONS =
'''Review Questions:'''
# Write a single Linux command to create a hard link called '''~/backup/myfile.txt.lnk''' to the existing file called '''~/myfile.txt'''<br>Write a single Linux command to display detailed information for those files above displaying their i-node numbers.<br>In this case, will the inode numbers for those files above be the same or different?<br><br># Write a single Linux command to create a symbolic link called '''~/shortcuts/murray.saul.lnk''' to the existing directory called '''~murray.saul'''<br>Write a single Linux command to display detailed information for those files above displaying their i-node numbers.<br>In this case, will the inode numbers for those files above be the same or different?<br><br>What data is contained in the file called '''~/shortcuts/murray.saul.lnk'''?<br>What would be the size of the file called '''~/shortcuts/murray.saul.lnk'''?<br><br>
# Write a single Linux command to run the program called '''~/clean.sh''' in the background.<br>What command would you issue to place the previously issued program in the foreground?<br>What command would you issue to confirm that this program is running in the background?<br>What key-combination would you issue to send that program again into the background?<br><br>
# Write a single Linux command to display running processes in “real-time”.
# Write a single Linux command to terminal a process that has the following PID: '''22384'''
# Use the following diagram to answer the accompanying questions.<br>Each of the following questions will use the diagram below and are treated as independent situations.<br><br><span style="font-family:courier;font-weight:bold;">[1]&nbsp; Stopped vim a<br>[2]- Stopped vim b<br>[3]+ Stopped vim c</span><br><br>Write a single Linux command to bring the second-recently process placed in the background into the foreground.<br>Write a single Linux command to terminate the '''job #3'''.<br><br>
# 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: <br>'''ln''' , '''ps''' , '''top''' , '''fg''' , '''bg''' , '''jobs''' , '''kill''' , '''alias''' , '''unalias''' , '''history'''
[[Category:ULI101]]
13,420
edits

Navigation menu