Send Foreground job to background redirecting output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Send Foreground job to background redirecting output
# 1  
Old 03-10-2010
Send Foreground job to background redirecting output

I have many CPU intensive processes running and sometimes I run them in the foreground so that I can see what the output is.

I want to send that foreground process to the background, but also have it direct the output to a logfile.

I know to send something to the bg I do

Ctrl-z on the FG job and then
Code:
 bg %1

is there a way to send the output of that process to the background...

for instance (and this does not work)
Code:
 bg %1 > somelogfile.log &


thanks
# 2  
Old 03-10-2010
The shell does redirection before it creates a process, not after, so you're out of luck there. You can't redirect a running process unless you're using something like screen which uses socket or tty tricks to do so.
# 3  
Old 03-11-2010
Just a small thought...

Assume a process(test.sh) is running with a pid xxx

If we give the SIGSTP signal, say
Code:
kill -17 xxx

, it will signal the process

Code:
[1] + Stopped (SIGSTOP)        sh test.sh

now, why cant we give bg which will change it to running in bg?
# 4  
Old 03-11-2010
@corona688 ok so it creates the output at the initial process call. what if i were to start it in the background with a logfile...is there a way to bring it into the FG?

@dennis.jacob - yea it can be sent into the background by just doing bg, but then I can't log the output in a logfile
# 5  
Old 03-11-2010
Quote:
Originally Posted by jhullbuzz
@corona688 ok so it creates the output at the initial process call. what if i were to start it in the background with a logfile...is there a way to bring it into the FG?
I repeat: The shell cannot redirect output for a process that already exists. You can't just reach in and change a completely different process' file table for it.

The shell has to do all redirection for a new process before the new process is run -- it does this by creating a subshell, which does all redirection, then replaces itself with the new program but keeping I/O redirections intact. Once that's done, the new process controls its own output like any other program.

If you want to view the logfile as its written to, try 'tail -f logfile'. This will watch logfile for new lines and print them to standard output.

There's also the screen terminal/utility/shell/thing, which uses network sockets to do something similar to you want. It keeps sessions that you can connect and disconnect to without killing or halting them.
# 6  
Old 03-11-2010
ok. just thought id see if the opposite was possible. the tail -f logfile approach seems to be the best option here. thanks big guy
# 7  
Old 03-11-2010
Quote:
Originally Posted by jhullbuzz
Code:
 bg %1 > somelogfile.log &

This will send the standard output of bg to somelogfile.log, not %1.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I send output of a background process to a file other than nohup.out?

I have a question. I will be running a background process using nohup and & command at end. I want to send output to a file say myprocess.out. So will this command work? nohup myprocess.ksh > myprocess.out & Thanks in advance guys !!! :) (3 Replies)
Discussion started by: vx04
3 Replies

2. UNIX for Dummies Questions & Answers

Send job to Background after input redirection

Hi, I am having issues with syntax when I am trying to send a job to the background after a input redirection. I have this script which sends some files to different servers after zipping them. Once I execute it, it will ask for user input as of which server the files need to go to. (The... (3 Replies)
Discussion started by: grep_me
3 Replies

3. Shell Programming and Scripting

Capturing the output of a background job

Hello, I unfortunately have a process that does two things, it returns an answer to me and then does a bunch of work that I would like to wait on. Here is a simple example: #!/bin/bash function p_w { echo "poopy" sleep 10 echo "scoop" } foo=$(p_w &) sleep 1 echo "1... (7 Replies)
Discussion started by: brsett
7 Replies

4. Shell Programming and Scripting

Move shell script from foreground to background

Hi, Need an urgent help. I have a program executing in foreground. I need to execute it in background and also to remove terminal dependency. Thanks In advance. 116@434 (7 Replies)
Discussion started by: 116@434
7 Replies

5. Shell Programming and Scripting

How can put a background process to the foreground

Hi, guys: I am working on my own shell using c. When I put a process into the background, how can I put it back to the foreground using tcsetpgrp? Thanks (3 Replies)
Discussion started by: tomlee
3 Replies

6. UNIX for Advanced & Expert Users

what is the diff b/w background and foreground process

What are all the difference between a Background and Foreground processes ?! A Background process does not have access to STDIN and OUT.. What else ? Is there any detailed description available somewhere ? (5 Replies)
Discussion started by: onequestion
5 Replies

7. Shell Programming and Scripting

Background and Foreground of a process within a script

I'm not sure if it is even possible but I figured if it was someone here would know how to do it... I am running a script which starts a bunch of processes in the background but there is one process I would like to bring back to the foreground when complete. Unfortunately the process that I... (2 Replies)
Discussion started by: ctruhn
2 Replies

8. Shell Programming and Scripting

how to get background job to foreground

hi, i am just wondering that wen we give the following code we make a process run in background...can the viceversa be performed?i.e can this be made foreground again # sleep 75& 21751 # (4 Replies)
Discussion started by: sandilya
4 Replies

9. UNIX for Dummies Questions & Answers

set background/foreground color in .profile

I am using a telnet session (VT100) and need to modify my .profile so that it will set the color of the telnet session. I am not using Xterm (ie: can't use .Xdefaults). I am able to change the colors via menu's but need to preset in .profile. Is this possible??? Can't find anything at all on how... (3 Replies)
Discussion started by: dvella
3 Replies

10. UNIX for Dummies Questions & Answers

problems with ctrl-z, to switch foreground, background

my shell is /sbin/sh. i added stty susp '^Z' with the intention of being able to switch between foreground and background. but the result was strange. i had 2 servers. one is sun the os is 8 and the other is hpux v11. both of them had the same shell. but on hpux, it works perfectly fine while... (9 Replies)
Discussion started by: yls177
9 Replies
Login or Register to Ask a Question