Capturing the output of a background job


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing the output of a background job
# 1  
Old 09-06-2011
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:

Code:
#!/bin/bash

function p_w {
  echo "poopy"
  sleep 10
  echo "scoop"
}

foo=$(p_w &) 
sleep 1
echo "1 $foo"

wait
echo "2 $foo"

What I would like to have happen, is the output of foo would end with scoop after the wait. That doesn't happen of course. Assume all file systems are read only. Any advice?
# 2  
Old 09-06-2011
Code:
Interactive-commands-displays
 
nohup ksh -c 'slow_stuff 2>&1|mail -s slow_stuff you@there &'

# 3  
Old 09-06-2011
I don't understand. It appears to me that nohup just appends stdout to a file, nohup.out, which can't really work on my read only file systems. But maybe mkfifo is what I want . . .

The problem in the above program is that the line:

Code:
foo=$(p_w &)

Doesn't run p_w in the background. And if I move the & outside the parens, it doesn't produce any output.
# 4  
Old 09-06-2011
Why not use bash coprocesses:

Code:
#!/bin/bash
function p_w {
  echo "poopy"
  sleep 10
  echo "scoop"
}
 
coproc bar { p_w ; }

sleep 1
read foo <&${bar[0]}
echo "1 $foo"
 
read foo <&${bar[0]}
echo "2 $foo"

# 5  
Old 09-07-2011
Yeah, coprocs look like they would do it perfectly. I'm just not certain if some of the older AIX systems will have a version greater than 4.0. Why doesn't this work:

Code:
read foo <<EOF &
$(p_w)
EOF

Edit: Oh, of course, foo is set in the subshell, but not in the parent shell. Arrgh. Maybe there is no old portable way to do this.

Last edited by brsett; 09-07-2011 at 12:15 PM..
# 6  
Old 09-08-2011
If you redirect stdout and stderr, nohup has nothing to write. If you do not want later output in file or mail, where do you want it? A new xterm?
# 7  
Old 09-08-2011
Quote:
Originally Posted by DGPickett
If you redirect stdout and stderr, nohup has nothing to write. If you do not want later output in file or mail, where do you want it? A new xterm?
In a variable so that I can use it in a later computation. This is all a huge script that uses ssh to do a bunch of remote work. I need a single remote invocation to print some junk to stdout, and start a long running process that I will wait on. The goal is to pull the junk out of stdout, and on the basis of that, figure out what data I will send the long running process (it is listening on a seperate port).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capturing the return code from background process

Hi All, I was out not working on unix from quite sometime and came back recently. I would really appreciate a help on one of the issue I am facing.... I am trying to kick off the CodeNameProcess.sh in PARALLEL for all the available codes. The script runs fine in parallel. Let say there are... (1 Reply)
Discussion started by: rkumar28
1 Replies

2. Shell Programming and Scripting

Background job issue

How to bring a backgroud job say sample_script.sh to foreground (4 Replies)
Discussion started by: rafa_fed2
4 Replies

3. Shell Programming and Scripting

Background Job

Hello Everyody, Having a doubt. sort file1 & when we sent a job to the background it returns Job Number PID again if we want to ... (1 Reply)
Discussion started by: knroy10
1 Replies

4. Shell Programming and Scripting

Capturing the exit status of the script running in background

Hi All, I have a scenario where I am executing some child shell scripts in background (using &)through a master parent script. Is there a way I can capture the exit status of each individual child script after the execution is completed. (2 Replies)
Discussion started by: paragkalra
2 Replies

5. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: jhullbuzz
6 Replies

6. Shell Programming and Scripting

capturing output from top and format output

Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command isecho date `top -b -n1 | grep -e Cpu -e Mem` I get the output in 3 separate lines.Tue Feb 24 15:00:03 Cpu(s): 3.4% us, 8.5% sy .. .. Mem: 1011480k total, 226928k used, ....... (4 Replies)
Discussion started by: new2ss
4 Replies

7. Shell Programming and Scripting

Cannot submit a background job

Hi all, I am currently facing a problem when i am submitting a script to run in the background to collect statistics round the clock on an AIX box. I don't have root authority nor can I set it in cron. So when i submit the job, it runs fine, but won't let me signoff. It prompts me that... (2 Replies)
Discussion started by: tansha
2 Replies

8. UNIX for Dummies Questions & Answers

background job

on gnome i open a terminal and run wget http://soommmething & in the background. because wget shows me downloading progress percentage and download speed continuously, I exit the gnome-terminal after a while i want to see the download percentage but dont know how. my ps -u myname shows that... (3 Replies)
Discussion started by: babayeve
3 Replies

9. UNIX for Dummies Questions & Answers

Background job

Hiya, Recently I've run a few scripts in the foreground, but have realised later they should of been better nohup'd and placed in the background. I understand how to change a foreground job into a background one, but how would put the job into the nohup state? Thanks (1 Reply)
Discussion started by: rdbooth
1 Replies

10. UNIX for Dummies Questions & Answers

background job

I try to run a script as background job. script: #!/usr/bin/csh /usr/bin/date +20%y-%m-%d > ~/datsql.txt If I start it I got this output: tac> ./datermitteln& 293 + Stopped (SIGTTOU) ./datermitteln& I insert the following line inside my script, but without any... (3 Replies)
Discussion started by: joerg
3 Replies
Login or Register to Ask a Question