Changes

Jump to: navigation, search

Tutorial8: Links / Process Management

3,988 bytes added, 09:55, 6 February 2020
Linking Files
:* 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>
* [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.
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 therefore are considered to be the same file in the Unix/Linux OS treats them all as the same filesystem.
'''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).
[[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 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 &''')
[[Image:process-diagram-1.png|thumb|right|300px|(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]]
UNIX processes are '''hierarchical''':
<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 usage<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 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 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===
<span style="font-family:courier;font-weight:bold;>alias dir=ls<br>alias ls='ls -al'<br>alias clearfile='cat /dev/null >'</span>
'''<span style="font-family:courier;font-weight:bold;>unalias alias-name</span>''' (removes alias from memory)
# 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>
# 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>
# 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>
# 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 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 the process running in the foreground.<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?'''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># After :In the next investigation, you complete the Review Questions sections will learn how to get additional practice, then work create aliases and view command history on yourMatrix server.<br><br>online assignment 2 and complete section4 labelled: '''Linking files and directories'''.
<br>
<br>
In this section, you will learn how to ..manage aliases and Linux command history on your Matrix account.
'''Perform the Following Steps:'''
# xMake 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 lal='ls -al'</span><br><br># Run 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># Run the '''lh''' command to see what happens. What do you think this command does?<br><br># Logout of your Matrix account, than log into your Matrix account.<br><br># Reissue the '''lh''' alias. What happened?<br><br># Issue the '''alias''' command without any arguments to see if it is stored in memory.<br><br># Reissue the command to create the '''lh''' alias in '''step #5'''.<br><br># Run the '''lh''' alias to confirm that it is properly set in memory.<br><br># Issue the following Linux command to edit your ~/.bashrc startup file:<br><span style="color:blue;font-family:courier;font-weight:bold">nano ~/.bashrc</span><br><br># Add the following line at the bottom of this file:<br><span style="font-family:courier;font-weight:bold;>alias lh='ls --human-readable --size -1 -S --classify'</span><br><br># Save your editing changes and exit your text editor.<br><br># Logout of your Matrix account, than log into your Matrix account.<br><br># Reissue the '''lh''' alias. What happened?<br><br># Issue the following Linux command: <span style="font-family:courier;font-weight:bold;>unalias lh</span><br><br># Run the '''lh''' command to see what happens.<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