Section 5.12. Redirecting Background Processes


[Page 181 (continued)]

5.12. Redirecting Background Processes

To prevent the output from a background process from arriving at your terminal, redirect its output to a file. In the following example, I redirected the standard output of the find command to a file called "find.txt". As the command was executing, I watched it grow using the ls command:

$ find . -name a.c -print > find.txt & 27188                  ...process ID of "find". $ ls -lG find.txt       ...look at "find.txt". -rw-r--r--  1 glass        0 Feb  3 18:11 find.txt $ ls -lG find.txt       ...watch it grow. -rw-r--r--  1 glass       29 Feb  3 18:11 find.txt $ cat find.txt         ...list "find.txt". ./wild/a.c ./reverse/tmp/a.c $ _ 


Another alternative is to mail it to yourself:

$ find . -name a.c -print | mail glass & 27193 $ gcc program.c       ...do other useful work. $ mail                ...read my mail. Mail version 8.1 6/6/93.  Type ? for help. >N  1 glass@utdallas.edu Mon Feb  3 18:12   10/346 & 1 From: Graham Glass <glass@utdallas.edu> To: glass@utdallas.edu ./wild/a.c            ...the output from "find". ./reverse/tmp/a.c & q $ _ 



[Page 182]

Some utilities also produce output on the standard error channel, which must be redirected in addition to standard output. The next chapter describes in detail how this is done, but I'll supply an example in Bash and the Korn shell now, just in case you're interested:

$ man ps > ps.txt &          ...save documentation in background. 27203 $ Reformatting page.  Wait         ...shell prompt comes here. done                               ...standard error messages. man ps > ps.txt 2>&1 &             ...redirect error channel too. 27212 $ _      ...all output is redirected. 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net