1 / 23

Agenda

Agenda. The Bourne Shell – Part I Redirection ( > , >> , < , << ) Pipes Controlling Processes Creating Subshells, ps, ps -l, $!, $$ Managing Processes (Jobs)

Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Agenda • The Bourne Shell – Part I • Redirection ( >, >>, < , <<) • Pipes • Controlling Processes • Creating Subshells, ps, ps -l, $!, $$ • Managing Processes (Jobs) • Command Grouping, Running jobs in background (bg), bringing jobs to foreground (fg), job status (jobs), Suspending jobs (CTRL Z), Restarting jobs in background, Displaying/Killing processes or jobs

  2. The Bourne Shell • The Bourne Shell was the first shell developed for the UNIX operating system. Since many other “new and improved” shells incorporate the Bourne shell, we will be learning about this shell first. • When you login to PHOBOS, you can access the Bourne shell by issuing the command: sh (although you can use Bourne shell commands in the default shell for PHOBOS – which is the Korn Shell) • There is no Bourne Shell in Linux…

  3. Standard Input, Standard Outputand Standard Error • Three terms are used to indicate the direction of information when issuing UNIX / Linux commands: • Standard Input (stdin) – The default is the keyboard • Standard Output (stdout) – The default is the screen • Standard Error (stderr) – The default is the screen

  4. Redirection • Since everything in UNIX / Linux operating system is a file, standard input, standard output or the standard error can be directed from/to different files: • 1>, > Redirects standard output to a file. The 1> indicates standard output is redirected, but “>” is considered “1>” by default. This will overwrite previous contents of a file unless you use “>>”

  5. Redirection • 2> Redirects the standard error to a file. This can be used to view error messages in a file for later reference. If you want to “throw-away” error messages, redirect to /dev/null commonly referred to as the “bit-bucket” or “trash can”Example: • cat a b c 2> /dev/null

  6. Redirection • < Command takes input for a command from a file. Example:grep pattern < file 1 • Can You explain the following redirections? • cat < file1 > file2 • grep msaul < file3 > file1 2> file7 • grep dward < file4 > file 5 2>/dev/null

  7. Pipes ( | ) • Pipes are used to send the standard output of one command into another command as the standard input • Pipes have the advantage of modifying standard output to achieve a task (possibly without having to create temporary files which need to be removed later)

  8. Controlling Processes • A process in UNIX simply represents the running of a command • The process structure in UNIX is similar to the hierarchical structure of directories: • The beginning process is root • Parent processes can create child processes • Processes can spawn other processes • When a child process is finished, execution returns to its parent process

  9. Controlling Processes • It is important to learn how UNIX processes work, since administering a UNIX system requires knowledge of how to start, stop and monitor processes. • The UNIX system is built on a series of processes and sub-processes. If you don’t understand processes, you could accidentally shut down the UNIX system.(not a very smart or popular thing to do!)

  10. Processes • For your account, the basic process is the shell (although for the entire system - from root downwards there are many processes). • When you type in a UNIX command and press ENTER, a child process is created to allow execution of command. During this time the parent process “sleeps” and resumes when child process is completed. There is even a command called sleep to suspend the process for a specific number of seconds!

  11. Process Identification (PID) • Process Identification Numbers (PIDs) are used to keep track of separate processes • To view a compact listing of process ID numbers, you can enter ps • To view a detailed listing of process ID numbers (relating child (PID)& parent processes(PPID)) enter ps -l • To view listing of processes for the entire system, enterps -ef

  12. Read-only Variables for PIDs • The shell can store the PID number as a read-only shell variable • echo $$ displays the PID number that is currently executing • echo$!displays the PID of the last process that was run in the background • The next slide shows a practical example of using these read-only variables

  13. Creating Filenames using $$ • The PID number can be used to create unique filenames (that need to be different everytime they are run) • To create temporary files within a script: • cp file_name file_name.$$ • This will create a temporary file with the PID as the extension. This is useful when removing all temp files (eg rm *.$$)

  14. Managing Processes (Jobs) • Command Grouping • Running jobs in background (bg) • brings jobs to foreground (fg) • Displaying job status (jobs command) • Suspending jobs (CTRL Z) • Killing processes or jobs (kill)

  15. Command Grouping • You can run a set of commands on the same command line by typing such commands as: • ls ; who ; date <ENTER> • You can also, combine commands or files to be run in both the foreground or background (eg ls ; who or a & b ; c)

  16. Command Grouping • The UNIX OS has the ability to run programs in the foreground and in the background. This feature allows users of the UNIX system to run lengthy processes without interfering with their other tasks. • To run a command or script in the background, type the symbol & after the command or filename (eg. who &). Note that spaces are allowed...

  17. Command Grouping • It is important to note that although these commands appear to be in a sequence, the shell may execute these in a different order may schedule jobs in a order • For example, you may want to run both commands a & b in the background, and run command c in the foreground. Here is the command:(a;b)&c

  18. Bringing processesto foreground • When processes are run in the foreground, the shell will wait for the command to finish before allowing the user to enter another command (thus background processes are useful) • If a process or processes are in the background (or suspended), you can bring it to the foreground by typing fg

  19. Displaying job status (jobs command) • You can display the status of jobs that are running in the background by typing the UNIX command jobs • jobs commands give a listing of jobs running in the background: • jobs with a plus sign “+” indicates default job to bring to foreground by entering fg • Can bring numbered job to foreground by entering fg%(job#) - Note space between fg& %

  20. Suspending jobs (CTRL Z) • You can suspend or stop the execution of processes while they are running in the “foreground” by pressing <CTRL><Z> • When you press these keys, the process is suspended and placed into the background in order to free up the shell for other operations. • You can resume process by bringing it to the foreground or have it run in background

  21. Restarting Suspended Processesin the Background • A process may take such a long time that it is better to suspend the job (i.e. send it to the background, and then have it run in the background). This will leave you to operate in the foreground • If a process or processes are suspended in the background, you can have it continue running in the background by entering bg(rules such asbg %(job#) also apply)

  22. Terminating processes or jobs (kill) • Depending on your permissions, you may be able to abort or “kill” a process. • Only root is allowed to kill other user’s processes, but as a user, you can processes that you have created. • Killing a process is also useful to halt processes that are running in the background since <CTRL><C> or DELETE keys in foreground won’t work

  23. Procedure to “Kill” a Process • Procedure: • We are assuming that you are killing one of your own process (you are logged in…) • View process by typing ps -l • Carefully locate PID to kill • Type kill %(job#) to kill that job number (can also use kill PID or kill -9 PID to kill process. The kill command by itself kills more recent stopped job

More Related