Exploring UNIX: Session 5 (optional)

Similar documents
Exploring UNIX: Session 3

Linux Tutorial #6. -rw-r csce_user csce_user 20 Jan 4 09:15 list1.txt -rw-r csce_user csce_user 26 Jan 4 09:16 list2.

Multitasking. In this chapter: Running a Command in the Backg round Checking on a Process Cancelling a Process

Introduction to UNIX Shell Exercises

ACS Unix (Winter Term, ) Page 92

Linux Command Line Interface. December 27, 2017

File system Security (Access Rights)

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Sperimentazioni I LINUX commands tutorial - Part II

I/O and Shell Scripting

INSE Lab 1 Introduction to UNIX Fall 2017

Introduction to the Linux Command Line January Presentation Topics

UNIX Tutorial Five

Lab 2: Linux/Unix shell

CRUK cluster practical sessions (SLURM) Part I processes & scripts

Unix basics exercise MBV-INFX410

CSE 391 Lecture 3. bash shell continued: processes; multi-user systems; remote login; editors

The Unix Shell & Shell Scripts

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

The Unix Shell. Job Control

Introduction to Linux

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

Introduction to Linux

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Perl and R Scripting for Biologists

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

S E C T I O N O V E R V I E W

Unix Processes. What is a Process?

CISC 220 fall 2011, set 1: Linux basics

UNIX Quick Reference

Command-line interpreters

B a s h s c r i p t i n g

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

COMS 6100 Class Notes 3

The Unix Shell. Job Control

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Introduction to the shell Part II

Introduction to UNIX Command Line

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

UTA Tech Orientation Spring 2019

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Oxford University Computing Services. Getting Started with Unix

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

bash, part 3 Chris GauthierDickey

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

5/20/2007. Touring Essential Programs

CSC209. Software Tools and Systems Programming.

Shells and Processes. Bryce Boe 2012/08/08 CS32, Summer 2012 B

Operating Systems Lab 1 (Users, Groups, and Security)

commands exercises Linux System Administration and IP Services AfNOG 2015 Linux Commands # Notes

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Introduction To. Barry Grant

Introduction to Linux and Supercomputers

Bash Programming. Student Workbook

Introduction to Unix

CSE 390a Lecture 3. bash shell continued: processes; multi-user systems; remote login; editors

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

Bamuengine.com. Chapter 7. The Process

Chapter 9: Process management. Chapter 9 Process management

CS 460 Linux Tutorial

07 - Processes and Jobs

Introduction: What is Unix?

Removing files and directories, finding files and directories, controlling programs

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

My Favorite bash Tips and Tricks

Process Management forks, bombs, zombies, and daemons! Lecture 5, Hands-On Unix System Administration DeCal

unix intro Documentation

9.2 Linux Essentials Exam Objectives

Please note that CNE 216 is a brand new course that has never been taught on the George campus; it will be taught for the first time in the fall of

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Tutorial 1: Unix Basics

LOG ON TO LINUX AND LOG OFF

S E C T I O N O V E R V I E W

Getting started with Hugs on Linux

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Lecture 3. Unix. Question? b. The world s best restaurant. c. Being in the top three happiest countries in the world.

CS197U: A Hands on Introduction to Unix

Chapter 4. Unix Tutorial. Unix Shell

Scripting Languages Course 1. Diana Trandabăț

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Lab Assignment #1. University of Pittsburgh Department of Electrical and Computer Engineering

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Physics REU Unix Tutorial

Getting started with Hugs on Linux

Simplest version of DayOfYear

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

Lecture-4. Introduction to Unix: More Commands, Boot-up Actions and X Window

Provides ability to perform UNIX commands at some time in the future. At time time on day day, the commands in filefile will be executed.

Chap2: Operating-System Structures

Introduction to remote command line Linux. Research Computing Team University of Birmingham

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

Most of the work is done in the context of the process rather than handled separately by the kernel

Getting Started. Logging In and Out. Adapted from Practical Unix and Programming Hunter College

LeakDAS Version 4 The Complete Guide

Transcription:

Exploring UNIX: Session 5 (optional) Job Control UNIX is a multi- tasking operating system, meaning you can be running many programs simultaneously. In this session we will discuss the UNIX commands for controlling and inspecting the programs you are running. This is known as job control. Suspending and resuming programs When you are running a long running program you might want to make it run in the background while you do something else in the foreground. You can do this by suspending the program by pressing Control-Z (^Z). At that point the program will be paused, and you will get the prompt back. Then, to let that program carry on running in the background you can use the bg command. To bring the command back to the foreground, you can use the fg command. Try this on raptor: cd /courses/projweek/unix/jobs You will notice at this point that the command doesn't give you a prompt back it s still running. To get a prompt back, you can suspend it by holding down the Control key and pressing the Z key. You should then see something like this: ^Z [1]+ Stopped As you can see, this gives you back a prompt and tells you that the program is Stopped, which means it is paused. You can make it start running again by using the bg command. Like this: bg [1]+ & The & symbol tells you the command is now running again, but in the background. Because it is running in the background, you get a prompt back so that you can run more commands. Now let s bring that command back to the foreground. You can do that using the fg command. Try this: fg That program will now be running in the foreground, and so you won't get a prompt back. You can kill this program now by typing Control-C (hold down the Control key and press the C key). You should see something like this: ^C As you can see, you have your prompt back now, and that program has finished.

You can also make a program start up in the background instead of having to suspend it and send it to the background. This is done by adding a & character at the end of the command line you want to run. For instance, try this: & [1] 13559 This will cause long-running-prog to start up in the background. The [1] 13559 line tells you the job number inside the square brackets (1 here, because it is the only background program running), and the process ID. The process ID, often abbreviated to PID, is a unique reference for the command. Every program run by every user will have a unique PID. The job number is just a quick reference for your own programs. Both of these numbers are used to reference this background task in other commands. For instance, if you want to bring that command to the foreground, you can pass the job number to the fg command like this: fg %1 and now that program is running in the foreground again. Suspend it (Control-Z) and send it to the background as you were shown earlier. ^Z [1]+ Stopped bg [1]+ & You can start as many background tasks as you want. For instance, let s start another program in the background:./infinite-prog & [2] 13746 So, now you have two programs running. How do you keep track of which programs you have started? The simplest way is using the jobs command. Try this: jobs -l [1]- 13559 Running & [2]+ 13746 Running./infinite-prog & Here you see that both commands are running, and the output lists the job number, process ID and whether the program is running or suspended. For instance, try bringing infinite-prog to the foreground and then suspend it, and then look at the output of jobs again: fg %2./infinite-prog ^Z [2]+ Stopped./infinite-prog jobs -l [1]- 13559 Running &

And there you can see that infinite-prog is still there, but is now suspended (Stopped). Quite often you will find that programs you run will themselves run other programs on your behalf. For instance, when you start a graphical user interface, lots of programs will be started in the background for the GUI to work. These programs will not be listed in the output of jobs, which only lists jobs you've started explicitly yourself. To see all the programs that are running on your behalf, you can use the ps command. Try this, though be aware that you will probably have lots more entries, and different programs running from those in this example. ps PID TTY TIME CMD 13472 pts/6 0:00 bash 13973 pts/6 0:00 sleep 13559 pts/6 0:00 long-running-pr 13974 pts/6 0:00 ps 13746 pts/6 0:00 infinite-prog 13934 pts/6 0:00 sleep UNIX is very good at scheduling all these multiple programs, so it is quite capable of handling hundreds of programs like this! Killing programs (aka how to use a sledgehammer) Sometimes you want to tell a background program to exit. We can do that using the kill command. kill is quite a powerful command, and so you need to use it with care. Although you cannot kill other users programs (only the administrator user can do that), it is quite easy to accidentally kill one of your own programs - probably all UNIX users have killed the wrong program at some time! To use the kill command, you just pass either the job number (prefixed with a %) or process ID of the program you want to exit. For instance try this: jobs -l [1]- 13559 Running & [2]+ 13746 Stopped (user)./infinite-prog kill %1 jobs -l [1]- 13559 Terminated Here the listing of jobs says that job %1 is terminated, but it is still in the list because it's not actually exited yet. This might be because it is saving some data or something. If you try running it again in a few seconds: jobs -l You can see that it has now finished running, and there is only one background task. Let's kill that too, but use its PID this time. The PID is the number after the job number in the listing - you will have a different number to me, so make sure you check what your PID is When you use the PID, you don t precede it by a % character, you just use it as it is: jobs -l kill 13746 jobs -l

[2]+ 13746 Stopped (user)./infinite-prog Notice that this background task is still suspended, and because it is suspended, it is ignoring the kill command. Now, we could un- suspend the command (using the bg command you learned about earlier), but we will use this opportunity to show you a more powerful kill command. Sometimes you will find a program will crash in such a way that you cannot get it to exit using the kill command in the way we have just shown you. In this case, you can add the "-KILL" flag to the kill command. This should kill any program, but you should not use this all the time, only on those programs that do not respond to the plain kill command, because kill -KILL will mean any unsaved data being used by the program you are killing will be lost. Try this now: jobs -l [2]+ 13746 Stopped (user)./infinite-prog kill -KILL 13746 jobs -l [2]+ 13746 Killed./infinite-prog Again, the program is still showing in the jobs listing, but this time it shows it's been killed. Wait a few seconds, and then run the jobs command again: jobs -l And you see that it has now gone. History - how to repeat yourself A useful part of the UNIX command line is its history mechanism. The history mechanism remembers the commands you have run in the past (by default on raptor, the last 500 commands you have run, but it can be customised to remember more) and allow you to recall them. It even remembers them when you log out. To view your history, you use the history command, like this (note that you will probably have different commands listed in your own history): history 2 vim run1.csv 3 sort run1.data 4 sort run1.csv 5 sort -n run1.csv 6 sort -r run1.csv 7 vim run1.csv 496 ls 497 cd /courses/projweek/unix/jobs 498 499 bg 500 fg 501 history The number on each line is the history number for that command. You can recall and run a command from your history using the! character, followed by the history number. For instance, try this example. You will need to replace 509 with the number from your history listing. Note also how we

pipe the output of history into tail to grab just the last few lines of it (think back to the session on commands and flags): ls /courses/projweek/unix commands jobs navigation permissions cd pwd /home/cur/aa99 history tail -n 5 508 clear 509 ls /courses/projweek/unix 510 cd 511 pwd 512 history tail -n 5!509 ls /courses/projweek/unix commands jobs navigation permissions You can see how when we used!509 it repeated that command from the history. A common usage of the history is to repeat the last command you did. This is so common, that there is a special short cut for it,!! - so let s try that: ls /courses/projweek/unix/jobs infinite-prog long-running-prog!! ls /courses/projweek/unix/jobs infinite-prog long-running-prog ls /courses/projweek/unix/permissions group-stuff public mind-your-own-business top-secret!! ls /courses/projweek/unix/permissions group-stuff public mind-your-own-business top-secret Another common use of the history is to run a new command, but with the same arguments as the last command. You do this by using the! operator after the new command you want to run, followed by a star, like this: cd /courses/projweek/unix/commands/quotes ls airports.txt deadlines.txt live.txt universe.txt universe2.txt cat airports.txt deadlines.txt It is no coincidence that in no known language does the phrase 'As pretty as an Airport' appear. - Douglas Adams I love deadlines. I like the whooshing sound they make as they fly by. - Douglas Adams ls -l!* ls -l airports.txt deadlines.txt -rw-r--r-- 1 aa99 cur 113 Nov 20 14:51 airports.txt -rw-r--r-- 1 aa99 cur 87 Nov 20 14:50 deadlines.txt You can also use!^ and!$ instead of the!* expression to get the first or last argument. Try these: pwd /courses/projweek/unix/commands/quotes wc airports.txt deadlines.txt 3 21 113 airports.txt 2 17 87 deadlines.txt 5 38 200 total

wc!^ wc airports.txt 3 21 113 airports.txt wc airports.txt deadlines.txt 3 21 113 airports.txt 2 17 87 deadlines.txt 5 38 200 total wc!$ wc deadlines.txt 2 17 87 deadlines.txt Command line editing When you are typing commands at the command line, there are a couple of useful key commands that can save you a fair bit of time. The first two of these are related to the history - you can scroll backwards and forwards through your history using the up and down arrow keys on your keyboard. That's probably the most common way most people use their history! Try it now. Two more useful key commands are Control-A and Control-E. If you start typing a command, and realise you have made a mistake at the beginning of the line, you can jump back to the beginning to edit it by pressing Control-A (hold down Control and then press A). You can jump to the end of a line in the same way by pressing Control-E. Remember these key commands when you are trying out the examples - they can be a big time saver in correcting the inevitable mistakes! Further work The sh (or bash) shell is quite good for writing shell scripts. There is a school of thought that believes the C shell (csh) to be better for actual use at a command prompt. Find out about csh. Yes, you can read the manual page first if you wish but it s probably not recommended. Use the Web instead, and in particular find out about command completion. Help others Ask your supervisor if there is anyone in the class who isn t as experienced as you. See if they would welcome some help. Acknowledgement: this document is heavily based on work done by Chris Ryder for theco806 module. Thanks, Chris! Bob Eager November 2014