Sperimentazioni I LINUX commands tutorial - Part II

Similar documents
Unix Processes. What is a Process?

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

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

Linux System Administration

Introduction to UNIX Shell Exercises

5/20/2007. Touring Essential Programs

CPSC 457 OPERATING SYSTEMS MIDTERM EXAM

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

Exploring UNIX: Session 5 (optional)

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.

07 - Processes and Jobs

Command-line interpreters

Managing Processes Process: A running program

Programs. Program: Set of commands stored in a file Stored on disk Starting a program creates a process static Process: Program loaded in RAM dynamic

bash, part 3 Chris GauthierDickey

System Programming. Introduction to Unix

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

The Unix Shell & Shell Scripts

Bamuengine.com. Chapter 7. The Process

System Administration

HW 1: Shell. Contents CS 162. Due: September 18, Getting started 2. 2 Add support for cd and pwd 2. 3 Program execution 2. 4 Path resolution 3

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.

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

CSC UNIX System, Spring 2015

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

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

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

I/O and Shell Scripting

Advanced Unix Concepts. Satyajit Rai

UNIX Essentials Featuring Solaris 10 Op System

Lab 2: Linux/Unix shell

High Performance Computing Lecture 11. Matthew Jacob Indian Institute of Science

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

Linux Command Line Interface. December 27, 2017

Chapter 9: Process management. Chapter 9 Process management

Chap2: Operating-System Structures

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

Processes. System tasks Campus-Booster ID : **XXXXX. Copyright SUPINFO. All rights reserved

Redirection and Pipes

The Unix Shell. Job Control

Lab 2: Implementing a Shell COMPSCI 310: Introduction to Operating Systems

Useful Unix Commands Cheat Sheet

The Unix Shell. Job Control

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

Introduction to UNIX. Introduction EECS l UNIX is an operating system (OS). l Our goals:

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

Processes and Shells

Checking Resource Usage in Fedora (Linux)

Assignment 1. Teaching Assistant: Michalis Pachilakis (

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

How to Implement a Web-based Terminal with Docker

Module 8 Pipes, Redirection and REGEX

Perl and R Scripting for Biologists

The input can also be taken from a file and similarly the output can be redirected to another file.

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Review of Fundamentals

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Introduction to the Linux Command Line January Presentation Topics

Utilities. September 8, 2015

Operating Systems. Lecture 07. System Calls, Input/Output and Error Redirection, Inter-process Communication in Unix/Linux

File system Security (Access Rights)

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

CST Algonquin College 2

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

Introduction to Linux Organizing Files

UNIX Tutorial Five

Introduction to Linux

6 Redirection. Standard Input, Output, And Error. 6 Redirection

Chapter 4 Controlling Processes

Common File System Commands

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Introduction: What is Unix?

Introduction to UNIX Part II

Introduction to Linux

ACS Unix (Winter Term, ) Page 92

CS 213, Fall 2001 Lab Assignment L5: Writing Your Own Unix Shell Assigned: Oct. 25, Due: Fri., Nov. 2, 11:59PM

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

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

CS 4410, Fall 2017 Project 1: My First Shell Assigned: August 27, 2017 Due: Monday, September 11:59PM

Part I. UNIX Workshop Series: Quick-Start

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Mills HPC Tutorial Series. Linux Basics II

Introduction to the Shell

Design Overview of the FreeBSD Kernel CIS 657

Mid Term from Feb-2005 to Nov 2012 CS604- Operating System

UNIX Quick Reference

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

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

UNIX Kernel. UNIX History

find starting-directory -name filename -user username

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

Introduction To. Barry Grant

Software I: Utilities and Internals. What is UNIX?

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

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

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

CSC374, Spring 2010 Lab Assignment 1: Writing Your Own Unix Shell

Transcription:

Sperimentazioni I LINUX commands tutorial - Part II A. Garfagnini, M. Mazzocco Università degli studi di Padova 24 Ottobre 2012 Streams and I/O Redirection Pipelines Create, monitor and kill processes

The BASH I/O Streams All the UNIX shells use three standard I/O streams: stdout, the standard output stream which displays output from commands. stderr, the standard error stream which displays error output from streams. stdin, the standard input stream which provides input to commands. Stream File descriptor stdin 0 stdout 1 stderr 2 Input streams provide input to programs, usually from terminals. Output streams print text characters, usually to terminals. Redirecting output There are two ways to redirect output: n> redirects output from file descriptor n to a file. You must have write permission to the file. If the file does not exist, it is created. If the file exists, the existing content is lost without any warning. n >> redirects output from file descriptor n to a file. You must have write permission to the file. If the file does not exist, it is created. If the file exists, the output is appended to the existing file.

Redirecting output streams - examples No redirection $ ls a* z* /bin/ls: z*: No such file or directory a2 a3 a4 a5 a.out* a.ps stderr redirection $ ls a* z* 2>errori.txt a2 a3 a4 a5 a.out* a.ps $ cat errori.txt /bin/ls: z*: No such file or directory stdout redirection $ ls a* z* >stdout.txt /bin/ls: z*: No such file or directory $ cat stdout.txt a2 a3 a4 a5 a.out* a.ps both output streams redirected $ ls a* z* >stdout.txt 2>>errori.txt $ cat errori.txt /bin/ls: z*: No such file or directory /bin/ls: z*: No such file or directory Redirecting both streams to the same file Sometimes both output streams have to be redirected to one file The procedure is often used for automated processes or in background jobs. Three possibilities: 1. command 1> output.log 2> output.log 2. command > output.log 2>&1 3. command &> output.log To append output to a file, replace > with >> Q: How to ignore output streams? A: redirect the appropriate stream to /dev/null Ex: Ignoring error output stream $ ls a* z* 2>/dev/null a2 a3 a4 a5 a.out* a.ps

Input redirection stdin stream can be redirected from a file, using the < operator stdin/stdout redirect example $ ls -1 > list.out Redirect stdout to list.out $ cat list.out a2 a3 $ sort -r < list.out a3 a2 All entries in ascending order Process input from file with sort giving a reverse ordering The cat command The cat command, short for catenate, allows to display the contents of a file on stdout: $ cat list.out... a3 The cat command takes input from stdin if you do not specify a file name; it keeps reading from stdin until the end-of-file. (ctrl-d is used to signal end-of-file). Creating a file with cat $ cat >list2.out Ctrl-d is pressed... a3 $

Input redirection with here-document The here-document is another form of input redirection It uses the << along with a word, such as END for a marker to indicate the end of input. [agarfa]$ cat«end>ex-here.sh > cat «-EOF > apple > EOF > END [agarfa]$ cat ex-here.sh cat «-EOF apple pear EOF [agarfa]$. ex-here.sh apple pear source the script, i.e. run in the current shell context It is often used in script files where there is no other way to indicate which lines should be treated as input. Input redirection summary > file Direct standard output to file < file Take standard input from file >> file Direct standard output to file; append to file if it exists << label Here-document n> file Direct file descriptor n to file n< file Take file descriptor n from file n >> file Direct file descriptor n to file; append to file if it exists n>& Duplicate standard output to file descriptor n n<& Duplicate standard input from file descriptor n n>&m File descriptor n becomes a copy of the output file descriptor n<&m File descriptor n becomes copy of the input file descriptor &>file Directs standard output and standard error to file

Pipelines Text filtering is the process of taking an input stream of text and performing some conversion before sending it to an output stream Filtering is done constructing a pipeline of commands where the output from one command is piped or redirected as input to the next: command1 command2 [ command3...] [agarfa]$ ls *a *z sort /bin/ls: z*: No such file or directory a2 [agarfa]$ ls *a *z 2>&1 sort a2 /bin/ls: z*: No such file or directory N.B. pipelines only pipe stdout to stdin! if stderr has been redirected to stdout, both streams will be piped. One advantage of pipes on LINUX and UNIX is that there is no intermediate file involved: the stdout of the first command is sent directly to the second command. head or tail... and wc While cat allows to concatenate files and print them on the standard output head output the first part of files (10 lines by default) head -n K head -c B prints the first K lines of a file; prints the first B bytes of a file; tail output the last part of files (10 lines by default) tail -n K tail -c B prints the last K lines of a file; prints the last B bytes of a file; wc prints newline, word and byte counts for each file wc -c wc -l wc -w print the byte counts; print the newline counts; print the word counts;

Finding text in files The grep command print lines matching a pattern grep [OPTIONS] PATTERN [FILE...] grep searches the named input FILEs, or standard input if no files are named, for lines containing a match to the given PATTERN. grep -i grep -n input file. ignore case distinctions in both the PATTERN and the input files. prefix each line of output with the 1-based line number within its [agarfa]$ grep iostream *.cxx primo.cxx:#include <iostream> secondo.cxx:#include <iostream> [agarfa]$ grep -n iostream *.cxx primo.cxx:1:#include <iostream> secondo.cxx:1:#include <iostream> [agarfa]$ ls -l grep cxx -rw-r-r- 1 agarfa fisica 100 Oct 21 12:27 primo.cxx -rw-r-r- 1 agarfa fisica 100 Oct 19 22:54 secondo.cxx Foregroud and background jobs When you run a command in a terminal window, you are running it in the foreground. The Bash shell has a suspend key, Ctrl-z. Pressing this key combination, you get the terminal prompt again. The clock is still on your destop but has stopped running. It can be restarted with the fg command fg brings the job right back to the foreground, but you no longer have a shell prompt. The bg command continues running your job in backgorund and giving back the terminal prompt [agarfa]$ xclock -d -update 1 [2]+ Stopped xclock -d -update 1 [agarfa]$ fg %2 xclock -d -update 1 [1]+ Stopped xclock -d -update 1 [agarfa]$ bg %1 [1]+ xclock -d -update 1 &

Using & To start a new job in background, add a & character at the end of the command The message shows a job number and a process id (PID). With the jobs command is possible to find out what jobs are running The -l option prints PIDs and the plus sign (+) beside the job number indicates that is the current job [agarfa]$ xclock -d -update 2 & [1] 29696 [agarfa]$ jobs -l [1]+ 29696 Running xclock -d -update 1 & ps: inspecting process status information The ps command accepts zero or more PIDs as argument and displays the associated process status Using ps with no options will list all processes that have our terminal as their controlling terminal [agarfa]$ jobs -l [1] 29696 [agarfa]$ ps 29696 29696 pts/6 S 0:00 xclock -d -update 1 [agarfa]$ ps PID TTY TIME CMD 27082 pts/6 00:00:00 bash 29696 pts/6 00:00:00 xclock 30170 pts/6 00:00:00 ps

More process status information Several options give control of how much information is displayed with the process status: -f : full -j : jobs -l : long --forest : display the commands in a tree hierarcy [agarfa]$ ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 S 8266 422 8479 0 80 0-1519 - pts/1 00:00:00 bash 0 R 8266 822 422 0 80 0-607 - pts/1 00:00:00 ps [agarfa]$ ps -j PID PGID SID TTY TIME CMD 2388 2388 27082 pts/6 00:00:00 ps 27082 27082 27082 pts/6 00:00:00 bash [agarfa]$ ps --forest PID TTY TIME CMD 422 pts/1 00:00:00 bash 764 pts/1 00:00:00 \_ ps Displaying other processes The -x option displays processes without a controlling terminal The -e option displays dinformation for every process Many options possible, get a brief summary with ps --help [agarfa]$ ps afx # Use BSD syntax 2? S< 0:00 [kthreadd]... 1? Ss 0:01 /sbin/init... 5852? Ss 0:00 /usr/sbin/gdm 5855? S 0:00 \_ /usr/sbin/gdm 8057 tty7 SLs+ 24:26 \_ /usr/bin/x :0 -br -audit 0 -auth /var/lib/gdm/:0.xauth -nolisten tc 8087? Ss 0:00 \_ /bin/sh /etc/xdg/xfce4/xinitrc -- /etc/x11/xinit/xserverrc 8175? Ss 0:00 \_ /usr/bin/seahorse-agent --execute startxfce4 8191? S 0:50 \_ xscreensaver -no-splash 8196? Sl 0:35 \_ /usr/bin/xfce4-session 8253? S 0:02 \_ /usr/lib/xfce4/[...] socket_i 8479? Ssl 0:27 \_ gnome-terminal 8485? S 0:00 \_ gnome-pty-helper... 26523 pts/4 Ss 0:00 \_ bash

top The top command displays a continuosly updated process list, along with useful summary information the -p option allows to control a single process [agarfa]$ top -p 29696 top - 18:58:50 up 10:32, 7 users, load average: 0.18, 0.28, 0.27 Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie Cpu(s): 9.9%us, 1.8%sy, 0.2%ni, 87.2%id, 0.6%wa, 0.1%hi, 0.3%si, 0. Mem: 2074216k total, 2018076k used, 56140k free, 54776k buffers Swap: 3903784k total, 23580k used, 3880204k free, 1501260k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 29696 alberto 20 0 7692 3400 2808 S 0 0.2 0:03.36 xclock kill To Ctrl-c sequence terminates a running program. The sequence sends a SIGINT or interrupt signal to the process. The kill command sends a signal to a job or process. Type man kill to get a table of all available signals. The SIGTSTP and SIGCONT signals stop and resume a background job [agarfa]$ xcalc & [2] 6080 [agarfa]$ ps 6215 6215 pts/6 S 0:00 xcalc [agarfa]$ kill -s SIGCONT 6215 [agarfa]$ ps 6215 6215 pts/6 S 0:00 xcalc [agarfa$ kill -s SIGTSTP 6215 [2]+ Stopped xcalc [agarfa]$ ps 6215 6215 pts/6 T 0:00 xcalc [agarfa]$ kill -s SIGINT 6215 [agarfa]$ ps 6215 [agarfa]$