LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-27-2007, 03:30 PM   #1
0ddba11
Member
 
Registered: Nov 2004
Location: Derby - UK
Distribution: Ubuntu at Home, RedHat Enterprise at Work
Posts: 46

Rep: Reputation: 15
Question Getting the return code of a backgrounded process


Hello all...

Been a while since I've posted here, but nice to be back.

I'm writing a C shell script that will write a directory to two tape drives at the same time as part of an archive system (one of the tapes goes offsite, the other stays local).

In the script, I want to get the return code from tar which I know I can do by something like:

Code:
tar -cvf /dev/st0 somedir
if ( $status == 0 ) then
    echo "Tape 1 written sucessfully"
else
    echo "An error occurred".
endif
However, as I said, I want to write both tapes at once so I need to background the first tar:
Code:
tar -cvf /dev/st0 somedir &
tar -cvf /dev/st1 samedir
Now, how can I get the return code for both runs to make sure both tapes were written sucessfully, and even better, if there was an error, output the actual error from the run?

Any help much appreciated.
 
Old 06-27-2007, 04:39 PM   #2
rednuht
Member
 
Registered: Aug 2005
Posts: 239
Blog Entries: 1

Rep: Reputation: 31
do want the exit code or just if it failed or not ?
Code:
tar -cvf /dev/st0 somedir && status1=0 &
tar -cvf /dev/st1 samedir && status2=0
then check your to statuses vars I guess you could use
Code:
tar -cvf /dev/st0 somedir && status1=$? &
tar -cvf /dev/st1 samedir && status2=$?
but I am not sure if $? is bash
 
Old 06-27-2007, 04:59 PM   #3
rednuht
Member
 
Registered: Aug 2005
Posts: 239
Blog Entries: 1

Rep: Reputation: 31
i meant
Code:
tar -cvf /dev/st0 somedir ;status1=$? &
tar -cvf /dev/st1 samedir ;status2=$?
 
Old 06-28-2007, 03:24 PM   #4
0ddba11
Member
 
Registered: Nov 2004
Location: Derby - UK
Distribution: Ubuntu at Home, RedHat Enterprise at Work
Posts: 46

Original Poster
Rep: Reputation: 15
You're right, $? is Bash, but $status should work in csh.

Never thought of simply concatenating the commands together DOH!!! Sometimes you miss the obvious when you've been staring at code all day

Many thanks, I'll give it a try
 
Old 07-02-2007, 04:33 PM   #5
0ddba11
Member
 
Registered: Nov 2004
Location: Derby - UK
Distribution: Ubuntu at Home, RedHat Enterprise at Work
Posts: 46

Original Poster
Rep: Reputation: 15
Question

Close but no cigar...

The problem with doing it like that (and it makes sense when you think about it) is that the two tar commands won't run at the same time, it executes one at a time.

Since I'm writing 200GB data to each tape, I want to make full use of my twin LTO drive unit.

Any more suggestions?

PS. On another note, I've switched all my scripts to bash as it seems a lot more predictable than C!!
 
Old 07-02-2007, 04:37 PM   #6
rednuht
Member
 
Registered: Aug 2005
Posts: 239
Blog Entries: 1

Rep: Reputation: 31
what if you make each command a bash script and run each in the back ground ?
output the status to a file or the screen.
 
Old 07-02-2007, 05:30 PM   #7
0ddba11
Member
 
Registered: Nov 2004
Location: Derby - UK
Distribution: Ubuntu at Home, RedHat Enterprise at Work
Posts: 46

Original Poster
Rep: Reputation: 15
After lots of searching on the net, it seems that getting exit codes of multiple forks is no mean feat!! So, I've come up with a workaround to have my original script call another script that stores the exit code in a file.

Here's an example:

Code:
#!/bin/bash
# Do all the other stuff in the script here
# archiver.sh - this will call tape_writer.sh
tape_writer.sh st0 &
tape_writer.sh st1 &

wait

if [ `cat /tmp/st0_status.txt` != "0" -o `cat /tmp/st0_status.txt` != "0" ]
then
     # Ouput an error
else
     # Output success
fi

rm -f /tmp/st?_status.txt
And now the code for tape_writer.sh:

Code:
#!/bin/bash
# tape_writer.sh
tar -cvf /dev/$1 /some/dir &> /tmp/$1.log
echo $? /tmp/$1_status.txt
Not very elegant though. I guess you could also get archiver.sh to create tape_writer.sh if you really wanted, that might make it a little bit more elegant!!

I'm still open to suggestions...

Gaz.
 
Old 07-02-2007, 05:31 PM   #8
0ddba11
Member
 
Registered: Nov 2004
Location: Derby - UK
Distribution: Ubuntu at Home, RedHat Enterprise at Work
Posts: 46

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by rednuht
what if you make each command a bash script and run each in the back ground ?
output the status to a file or the screen.
LOL - Great minds think alike!

We must have been posting at the same time.

Gaz.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
backgrounded process siezes shell paulyche Linux - General 1 10-02-2006 05:44 PM
request_region(...) return code BrooklynOutlier Programming 0 03-07-2006 09:22 PM
request_region return code BrooklynOutlier Slackware 0 03-06-2006 10:26 PM
dd return code barefootdoctor Linux - General 2 10-13-2005 12:54 PM
Return code from main() using gcc Meatwad Programming 13 01-27-2004 06:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:46 PM.

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