LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-11-2005, 12:46 AM   #1
DaneM
Member
 
Registered: Oct 2003
Location: Chico, CA, USA
Distribution: Linux Mint
Posts: 721

Rep: Reputation: 63
Redirecting output of background processes after they've been started (BASH)


Hello everybody.

I'm always running programs from consoles in X, using scripts and otherwise, and I'd like to make a script to silence all the output that results AFTER they've already been run. For example:

Code:
[dane@Orchestrator scripts]$ konqueror &
[3] 4343
<~/scripts>
[dane@Orchestrator scripts]$ konqueror: ERROR: Error in BrowserExtension::actionSlotMap(), unknown action : searchProvider

<~/scripts>
[dane@Orchestrator scripts]$ konqueror: ERROR: Error in BrowserExtension::actionSlotMap(), unknown action : searchProvider
libkonq: WARNING: Could not load wallpaper /opt/kde/share/apps/konqueror/tiles/
kio (KMimeType): WARNING: KServiceType::offers : servicetype text/english not found
kio (KMimeType): WARNING: KServiceType::offers : servicetype text/english not found
I know how to silence things BEFORE running them ( 1> /dev/null 2> /dev/null ) but I want to be able to do so afterwards as well, partly because it would be useful and partly because it would be really cool. I've heard it's possible...


I appreciate any help you can give me.

--Dane
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-11-2005, 11:11 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,276

Rep: Reputation: 170Reputation: 170
good question.
I have no answer but am curious to find out.
 
Old 01-11-2005, 10:52 PM   #3
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
This isn't exactly what you're looking for, but it effectively does the same thing. You can use the disown builtin to detach the job, then to silence the output you can exit and restart the X console and the old jobs will continue to run.
 
Old 01-12-2005, 12:44 AM   #4
DaneM
Member
 
Registered: Oct 2003
Location: Chico, CA, USA
Distribution: Linux Mint
Posts: 721

Original Poster
Rep: Reputation: 63
Thanks for the quick replies, everybody! Randyding's suggestion about disown sounds like it's definately worth trying. I took a stab at starting a program followed by the & symbol and then typing "disown <progname>". It seemed to have no effect other than allowing me to close the terminal window without losing the program, which, I believe is exactly what Randyding said it would do :-) . Great suggestion though.

Any more, anybody?

Thanks a bunch.

--Dane
 
Old 01-12-2005, 05:47 AM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Here is an ugly way to do that

# start konqueror in background and redirect output to a file
konqueror & >> output 2>&1

# now I want no redirected output
fg konqueror
# suspend the process
[ctrl]+z
# save the output
mv output output.bak
# symlink /dev/null
ln -s /dev/null output
# re-run the suspended process in backgroud
bg konqueror

# now konqueror will not output

# I want output again
fg konqueror
[ctrl]+z
rm output
mv output.bak output
bg konqueror

# now konqueror will output

Last edited by Cedrik; 01-12-2005 at 05:48 AM.
 
Old 01-12-2005, 06:27 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Clever

Maybe there's a solution by manipulating the "files" in /proc/<pid>/fd/... I didn't try. I'd be interested in knowing the solution to this question.

Yves.
 
Old 01-12-2005, 02:04 PM   #7
michael_util
Member
 
Registered: Feb 2004
Posts: 47

Rep: Reputation: 15
You could use screen and no have to worry about backgrounding a process.

execute `screen -h 500 -S konqueror -t konqueror konqueror` then type "CTRL-A + d" to detach the screen ... this would start screen with the session name konqueror and title konqueror.

If you did this in a term (example rxvt) you could just detact it. This way if you wanted to look at the output you could simple reattach the screen and look at it. When you close konqueror then the screen would also close.

Michael.
 
Old 01-12-2005, 02:48 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Here is an ugly way to do that

# start konqueror in background and redirect output to a file
konqueror & >> output 2>&1 [1]
# now I want no redirected output
fg konqueror [2]
# suspend the process
[ctrl]+z
# save the output
mv output output.bak [3]
# symlink /dev/null
...
Cedrik, did you test your suggestion before posting it ?
Here are some comments:

[1] Here, you first run konkeror in the background, then run the null command creating an empty file named output here, which is not the standard output and error of konkeror.
A correct syntax could have been:
Code:
konkeror >>output 2>&1 &
[2] Changing a process from background to foreground have no impact on its output descriptors.

[3] Even in the case konkeror standard output would have gone to the file 'output', after this command, it just would have gone in 'output.bak', file descriptors are bound to inodes, not filenames.
Because of that, I'm afraid the idea suggested here just can't work, unless you are using a non standard shell ...
Sorry.
 
Old 01-12-2005, 06:49 PM   #9
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Yes, I just checked the [1], my bad for writting this, for the [2] I was not clear in my comment, it
was supposed comment the 4 folowing lines.

But where I learnt something new is for the [3]
Quote:
file descriptors are bound to inodes, not filenames.
Thanks for clarify that, and I apologize for the bad code I posted, I just tested what I wanted to accomplish with a script like
while true; do date; sleep 1; done
to be sure about the output, you definitivly right, even after symlink /dev/null to the output file name, the output of script goes to output.bak

Last edited by Cedrik; 01-12-2005 at 06:51 PM.
 
Old 01-15-2005, 06:07 AM   #10
DaneM
Member
 
Registered: Oct 2003
Location: Chico, CA, USA
Distribution: Linux Mint
Posts: 721

Original Poster
Rep: Reputation: 63
Thanks for the replies, everybody; very informative!

Still, however, we have not determined how to solve the original problem; perhaps I should clarify.

Let's assume that you want to start a program from a console window, being able to see the output for debugging, etc. I'll use konqueror for an example:

Code:
$ konqueror &
You run the program (konqueror) for a while and then decide that all that output is getting in the way of other things you're doing in that console, so you want to silence the output, but you want to leave konqueror running in the background. I've run into this quite a bit while using a terminal and not wanting to go fishing in the GUI's menus.

Those were some great ideas on how to silence the output from the start. Does anybody have any ideas on how to silence the output AFTER the program's been run?

Thanks for your help.

--Dane
 
Old 01-16-2005, 04:28 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
You problem is clearly explained.
My answer is: you can't do it without a third party software sitting between konqueror and your terminal emulator.
Opening a new terminal alternative is IMHO not that complicate though ...
 
Old 01-16-2005, 06:23 AM   #12
DaneM
Member
 
Registered: Oct 2003
Location: Chico, CA, USA
Distribution: Linux Mint
Posts: 721

Original Poster
Rep: Reputation: 63
You're right, Jlliagre, it would probably be more practical to just open another terminal, but I would find it incredibly convenient to do it in the same one. Also, I've heard that it's possible from a friend who works for a place that does a lot of linux (although he does not). Granted, I don't know if he gave me a complete set of information on it. Still, if it's not possible, it's not possible. Thanks for the help!

--Dane
 
Old 01-17-2005, 09:25 PM   #13
jeorj_euler
LQ Newbie
 
Registered: Jan 2005
Location: MidWest United States
Distribution: Mandrake, Knoppix
Posts: 9

Rep: Reputation: 0
I suspect that this would require the creation of userspace application capable of reading the memory allocated to the process of konqueror. The file descriptor to which konqueror sends its output would have to be changed mid runtime. I am thinking of solution either with mmap or shared memory. There has to be some sort of way to find out and write to the address konqueror 'knows' to be the output file descriptor.
 
Old 01-18-2005, 07:31 PM   #14
DaneM
Member
 
Registered: Oct 2003
Location: Chico, CA, USA
Distribution: Linux Mint
Posts: 721

Original Poster
Rep: Reputation: 63
Wow, that's way over my head, but for as much as I know, it sounds like it would work. :-)
 
Old 01-19-2005, 01:52 AM   #15
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
It can't be a userspace application, as the file descriptors tables and state information are maintained in the kernel, not in user memory.
Mmap or shared memory would be of no help either for this reason.

If you really insist on doing it, that should be doable with a custom system call that would overwrite a process fd #1 table entry by another one corresponding to a /dev/null open.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
redirecting output of compile to a file halfcan Linux - Hardware 1 02-05-2005 10:01 PM
Redirecting mplayer output driptray Linux - Software 1 10-12-2004 07:42 AM
redirecting output in Bourne shell trutnev Linux - Software 2 04-22-2004 03:55 AM
redirecting xmms output durden2.0 Linux - Software 0 03-06-2004 07:29 PM
bash: interaction with background processes ... cdex Programming 5 08-11-2002 02:42 PM


All times are GMT -5. The time now is 06:00 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration