Changes

Jump to: navigation, search

Tutorial8: Links / Process Management

1,299 bytes added, 03:11, 17 May 2021
no edit summary
:* Define the term '''i-node''' as it relates to the Unix/Linux File System
:* Issue the '''ls -i''' command to view '''i-node''' ID (index) numbers associated with Unix/Linux files
:* Define the terms '''Hard''' and '''Symbolic''' Links
[[Image:inode-number-3.png|thumb|right|450px|The '''i-node number''' is like a '''finger-print''', and is considered to be '''unique''' for each file on the Unix / Linux file system.]]
An '''i-node''' is a '''database''' containing information (e.g. ''file type'', ''owner'', ''permissions'', etc.) for all files that are created on the Unix/Linux filesystem.<br><br>The '''i-node number''' is like a '''finger-print''', and is considered to be '''unique''' for each file on the Unix / Linux file system.<br><br>Referring to the diagram on the far right, issuing the '''<span style="font-family:courier;font-weight:bold;">ls''' </span> command with the '''<span style="font-family:courier;font-weight:bold;">-i''' </span> option displays the i-node number for each file. You can see that <u>each</u> file (whether it is a directory or regular file) has its own unique<br>i-node number.
===Hard Links===
'''Advantages:''' of hard links are that if If only one hard link remains (even if original file has been removed), the '''data in that hard linked file is NOT lost''', as well as . The data in hard linked files will automatically be updated to that of the original file since they share the same are '''i-node numberautomatically updated'''when original file are updated.
'''Disadvantages:''' of hard Hard links are that they '''take-up extra space''',<br>you '''cannot hard link directories''', <br>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).
''Examples:''
<span style="font-family:courier">'''touch myfile.txt<br>ln myfile.txt myfile1.hard.lnk<br>ln myfile.txt myfile2.hard.lnk<br>ln myfile.txt ~/backups/myfile.hard.lnk<br>'''</span>
===Symbolic Links===
'''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 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").
''Examples:''
<span style="font-family:courier">'''touch otherfile.txt<br>ln -s otherfile.txt otherfile1.sym.lnk<br>ln -s otherfile.txt otherfile2.sym.lnk<br>ln -s otherfile.txt ~/backups/otherfile.sym.lnk<br>'''</span>
<br><br>
'''Characteristics of Processes:'''
* Each process has an owneran '''owner'''* Each process has a unique ID ('''PID''')* Processes keep their PID for their ''PID'' for their '''entire life'''.* Usually a parent sleeps (i.e. suspended''suspends'') 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.<br>Processes that run in the terminal are known as '''foreground''' processes. You can run or send processes currently running<br>in the ''foreground'' to the '''background''' to free-up your terminal (e.g. issue other Linux commands).<br><br>Below are a listing of common '''Linux commands''' and '''keyboard shortcuts''' to manage foreground and background processes:
 ''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 /<br>Key Combination</th><th style="border-bottom: 1px solid black;text-align:left;">Purpose</th></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">ps</span></td><td>Displays snapshot information about processes.<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>'''<span style="font-family:courier;font-weight:bold;">top</span>'''</td><td>The '''top''' command provides a realtime status of running processes.<br>'''NOTE:''' You can press '''ctrl-c''' to exit<br></td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">fg'''ctrl-c'''</span></td><td>Moves a background job from the current environment into the foreground.<br>''Example: 'Terminates'''<span style="font-family:courier;font-weight:bold">fg %job-number</span>a process running in the foreground</td></tr><tr valign="top"><td><span style="font-family:courier;">'''ctrl-cz'''</span></td><td>Sends a process running in the foreground into the '''Terminatesbackground''' a process running in the foreground.</td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">'''ctrl-z'''fg</span></td><td>Sends Moves a process running in background job from the foreground current environment into the foreground.<br>''Example: 'background'''.<span style="font-family:courier;font-weight:bold">fg %job-number</span></td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">bg</span></td><td>Runs (starts) 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><span style="font-family:courier;font-weight:bold;">jobs</span></td><td>The '''jobs''' utility displays the '''status''' of jobs that were started in the current shell environment. ''Example:<br>''<span style="font-family:courier;">jobs<br>[1]+ Stopped vim a <span style="font-size:8pt;">&nbsp; &lt;-- Job #1 (+ most recent process / 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 (- second recent process / background)</span><br><br></span></td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">kill</span></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===
<br>'''AliasAliases:'''<br><br>Assigns An '''alias''' is a '''nickname ''' to an existing command or group of commands.<br><br>An alias existing in '''system memory''' and will be '''lost''' when your current Linux session ends,<br>unless the alias is set in a '''start-up file''' (e.g. <span style="font-family:courier;font-weight:bold;">~/.bashrc</span>. You will learn about using start-up files <u>later</u> in this course.<br><br>
''Examples:''
'''Command History:'''
<br><br>The filename '''<span style="font-family:courier;font-weight:bold;>~/.bash_history''' </span> stores recently executed command lines
<br><br>
''Examples of commands that use command history:''
<table width="80%"><tr valign="top"><td width="30%"><span style="font-family:courier;font-weight:bold;>up arrow</span> or <span style="font-family:courier;font-weight:bold;>down arrow</span> </td><td>move to '''previous''' command or '''next''' command within 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 (determined from ''history'' command)</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>!xxx</span></td><td>'''re-executerun''' an a most recent previously-issued command beginning with string "xxx"</td></tr></table><br><br>
=INVESTIGATION 1: LINKING FILES=
<span style="color:red;">'''ATTENTION''': Depending on your ULI101 instructor, you may be required to complete this tutorial for '''marks''' in this course.<br>Please refer to your instructor's course notes and lecture notes regarding evaluation for this course.<br><br>The due date for successfully completing this tutorial (i.e. '''tutorial 8''') is by '''Friday by midnight''' next week (i.e. '''Week 8''').<br>If your instructor has NOT assigned marks for completing this tutorial, you can perform it for '''practice'''.</span><br><br> In this sectioninvestigation, you will learn how to create '''hard links''' and '''symbolic links''' on your Matrix account,<br>and observe the <u>advantages</u> and <u>limitations</u> of using both types of links.
# 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 following Linux command to compare all file's ''i-node'' numbers:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links/data-file.txt ~/links/data-file.hard.lnk ~/data-file.hard.lnk</span><br><br>What do you notice about all of those file's ''i-node'' numbers?<br><br>
# Issue the following Linux command to check that you created those hard links: <br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts~uli101/week8-check-1</span><br><br>If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.<br>If you receive a congratulation message that there are no errors, then proceed with this tutorial.<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>
# Save your editing session and exit your text editor.<br><br>
# Issue the following Linux command to view the contents of the '''symbolic''' linked file:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/links2/text-file.sym.lnk</span><br><br>What did you notice? Again, when you view the contents of the symbolic-linked file,<br>you are redirected (via ''pathname'') to the <u>original</u> file.<br><br>
# Issue the following Linux command to check that you created those symbolic links: <br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts~uli101/week8-check-2</span><br><br>If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.<br>If you receive a congratulation message that there are no errors, then proceed with this tutorial.<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>
# Issue a Linux command to confirm that the '''~/links2''' directory has been removed.<br><br>
# Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">ls -l ~/text-file.sym.lnk</span><br><br>This output indicates a "'''broken link'''" and indicates this is not an effective method of backing up files.<br><br>
# Issue a command to delete the <span style="font-weight:bold;font-family:courier;">~/text-file.sym.lnk</span> file which is a ''broken link''.<br><br>
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~murray.saul/myscripts checking-scripts myscripts</span><br><br>[[Image:symbolic-link-inode2.png|thumb|right|500px|'''Symbolic links''' can be used to point to '''directories''' as well as regular files. Symbolic links can also point to files on '''other''' Unix/Linux filesystems.]]# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -ld myscriptschecking-scripts</span><br><br>What do you notice? Symbolic links are good for creating "short-cuts" to <u>both</u> '''regular files''' and '''directories'''.
=INVESTIGATION 2: MANAGING PROCESSES =
In this sectioninvestigation, you will learn how to '''manage processes''' on a Unix / Linux server.
# 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 Linux commands.<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>[[Image:process-jobs2.png|thumb|right|300px|Using the '''bg''' command to '''run recent process''' that was placed into background from using '''ctrl-z''' keys.]]
# 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 run recent process that was placed into in the background. From the '''jobs''' command, the process that has a plus sign "+" 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>(indicated by the ampersand character "'''&'''"). Now this command has resume pausing until '''700 seconds'''.<br><br>[[Image:process-jobs3.png|thumb|right|300px|Using the '''ampersand''' character '''&''' to run a '''series of processes''' in the background.]]
# 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>
<br>
In this sectioninvestigation, you will learn how to '''manage aliases''' and '''Linux command history''' on your Matrix account.
# Reissue the '''lh''' alias. What happened? Why?<br><br>
# Reissue the '''lal''' alias. Why '''didn't''' this alias work?<br><br>The checking script below is designed to act as a '''filter''' with a '''pipeline command'''.<br>This will allow to check if your '''lh''' alias exists when it is checked in this program.<br><br>
# Issue the following Linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">alias | bash /home/murray.saul/scripts~uli101/week8-check-3</span><br><br>If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.<br>If you receive a congratulation message that there are no errors, then proceed with this tutorial.<br><br>We will complete this investigation by learning to execute p'''reviously issued commands''' by using '''command history'''.<br><br>
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">history | grep "lh"</span><br><br>What do you notice?<br><br>
# Type an exclamation mark '''!''' followed by the number by one of those commands<br>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>
13,420
edits

Navigation menu