LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-07-2010, 02:53 PM   #1
toredo
Member
 
Registered: Feb 2009
Posts: 83

Rep: Reputation: 25
save error output of a running app


hello,

i started a command, it needs very much time. and i have to save the (error-)output. but i forgot it.

is there a way to save the output of a running process? because the process runs over this night and i want to know what did the process do.

thx for the help

best regards toredo
 
Old 01-07-2010, 03:01 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Assuming your application uses STDERR, then you should be able to run it like:

Code:
./myapp 2>/tmp/myapp_error.log
If it uses STDOUT then adjust as follows:

Code:
./myapp >/tmp/myapp.log 2>&1

cheers
 
Old 01-07-2010, 03:42 PM   #3
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 129Reputation: 129
You can also clear up things and see only STDERR live and have a file log too

Code:
myCommand > /dev/null 2>&1 | tee file.txt
 
Old 01-07-2010, 05:15 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A known technique is by means of the GNU debugger, gdb. You can redirect file descriptors 1 (standard output) and 2 (standard error) from the terminal to a file and leave the process running to its completion.

Here is an example. First, check the PID of the running process, then use the command:
Code:
gdb -p PID
to attach a gdb session to the process. In the following I will put an example session, highlighting my entries in blue (note that the first part is made of the licensing stuff and of the output messages of gdb itself):
Code:
$ gdb -p 11220
GNU gdb (GDB; openSUSE 11.1) 6.8.50.20081120-cvs
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i586-suse-linux".
For bug reporting instructions, please see:
<http://bugs.opensuse.org/>.
Attaching to process 11220
Reading symbols from /bin/bash...(no debugging symbols found)...done.
(no debugging symbols found)
Loaded symbols for /bin/bash
Reading symbols from /lib/libreadline.so.5...(no debugging symbols found)...done.
Loaded symbols for /lib/libreadline.so.5
Reading symbols from /lib/libdl.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/libdl.so.2
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/libncurses.so.5...(no debugging symbols found)...done.
Loaded symbols for /lib/libncurses.so.5
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
(no debugging symbols found)
0xffffe430 in __kernel_vsyscall ()
(gdb) p creat("/home/alex/foo.stdout",0600)
$1 = 3
(gdb) p dup2(3,1)
$2 = 1
(gdb) p close(3)
$3 = 0
(gdb) p creat("/home/alex/foo.stderr",0600)
$4 = 3
(gdb) p dup2(3,2)
$5 = 2
(gdb) p close(3)
$6 = 0
(gdb) q
The program is running.  Quit anyway (and detach it)? (y or n) y
Detaching from program: /bin/bash, process 11220
After that, stdout and stderr are redirected to /home/alex/foo.stdout and /home/alex/foo.stderr respectively, until completion of the process.

And here is a little explanation:

a) p creat("/home/alex/foo.stdout",0600)
$1 = 3
this creates a file with rw permissions and gdb assigns file descriptor 3 to it. This may differ for you, depending on the actual number of file descriptors kept open by the process. The important thing is that you must use this value in the subsequent commands.

b) p dup2(3,1)
this duplicates fd 3 to fd 1, actually redirecting the standard output to the newly created file.

c) p close(3)
this closes the file descriptor 3. Steps a), b), c) are repeated for standard error (you can see the difference in the filename and in the dup2 statement, which redirects fd3 to fd2).

d) q
quit the gnu debugger and detach the process (you have to answer y to the final question). At this point check the two files and see how they will be populated as soon as the process "spits" some more output/error.

Note that no lines of output are lost during the gdb session, since gdb attaches itself to the running process and pick up the standard output and the standard error. I hope this is what you're looking for.

Last edited by colucix; 01-08-2010 at 02:58 AM. Reason: Grammar and typos corrected... hopefully...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
running iptables commands gives me input/output error ziba Linux - General 4 05-11-2009 04:51 AM
input output error while running 'diff' on a ntfs disk nass Linux - Software 1 12-14-2008 08:56 AM
Check if an app is running and if it's not launch this app Coume Linux - General 3 07-28-2008 02:34 AM
Running Telnet in a script, want to save Telnet output, howtodothis??? anil3 Linux - Software 2 03-08-2006 04:01 PM
error in running a java app in linux milo36472 Linux - Newbie 16 02-09-2005 11:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration