UNIX Processes & Redirection
Announcements
- Collect HW#2
- Why are man and info better than other
alternatives?
- Comments about labs?
Man is OS-specific; both are installation-specific. Google will usually
get you a reference for a different OS or at least a different version of
the OS.
Managing Processes
- Normally, running a program in a shell will stop the shell while the other program runs.
- To allow both to run, start the program and put it into the background.
- Pressing CTRL-Z in a running program will suspend that process.
Managing Processes
- The jobs command will show you what processes are
running or stopped from this shell:
io:~% jobs
[1]+ Stopped more /etc/termcap
- The fg and bg commands move the
currently
stopped program into the foreground or background (and change status of other jobs currently stopped or running).
Can also give arguments with %jobid to the fg and bg and kill commands.
Killing Processes
- Sometimes, you get a program that you can't stop with CTRL-C.
- To kill your process that won't otherwise stop, type
or if needed,
- The killall command can also be used to kill a process
using its program name (instead of process identifier).
Actually, killall will kill all processes with that name (that are owned
by the current user).
Redirection
- One of the great features of UNIX is the ability to connect multiple
processes that read from stdin and stdout.
- cat, for example, writes to standard out
- In UNIX, stdin and stdout (and stderr) can be redirected.
- "ls -l > ls-l.txt" will take the output of "ls -l" and write it to
a new file called ls-l.txt
- "more < file1" will use file1 as the standard input to more
- "prog1 >> file1" will append prog1 stdout to file1 (not replace)
- "prog1 2> errorfile" will only write stderr messages to errorfile
- "prog1 &> bothfile" will write both stdout and stderr to
bothfile
- "sort < numfile > sortednums" redirects both stdin and stdout
It's useful to point out that every process has three standard file
descriptors associated with it --- standard input, standard output, and
standard error (0,1,2)
Pipes
- Programs can be chained together using "pipes"
- "cat longfile | wc" sends the output of "cat longfile" to the input of
"wc"
- "cat numfile | sort > sortednums" sends the output of "cat numfile"
to the input of "sort", whose output is written to "sortednums"
(wc is a program that counts the number of lines,
words, and characters sent to it.)
Example of Pipes
- A more complex (realistic) example:
cat airweb-access_log airweb-access_log.2005Jan* | grep -v airweb |
cut -d' ' -f11 | sort | uniq -c | sort -nr | more
takes web server logs with entries such as:
192.114.107.4 - - [23/Jan/2005:09:20:10 -0500] "GET / HTTP/1.1" 200 3515
"http://www2005.org/tutorials/accept.html" "Mozilla/4.0 (compatible; MSIE
6.0; Windows NT 5.0; .NET CLR 1.0.3705)"
and generates reports like:
1410 "-"
24 "http://www.cse.lehigh.edu/~brian/"
21 "http://www2005.org/tutorials/"
18 "http://timconverse.com/blog/"
17 "http://www2005.org/tutorials/workshops.html"
16 "http://nike.psu.edu/dbevents/"
8 "http://www.kdnuggets.com/news/2004/n24/28i.html"
8 "http://blog.searchenginewatch.com/blog/050121-124259"
If sufficient time...
- Continue with mycat today (if time)
- We can extend mycat to incorporate line numbers (like cat -n)