LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Getting the return code of a backgrounded process (https://www.linuxquestions.org/questions/linux-general-1/getting-the-return-code-of-a-backgrounded-process-564923/)

0ddba11 06-27-2007 03:30 PM

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.

rednuht 06-27-2007 04:39 PM

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

rednuht 06-27-2007 04:59 PM

i meant
Code:

tar -cvf /dev/st0 somedir ;status1=$? &
tar -cvf /dev/st1 samedir ;status2=$?


0ddba11 06-28-2007 03:24 PM

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 :D

0ddba11 07-02-2007 04:33 PM

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!!

rednuht 07-02-2007 04:37 PM

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.

0ddba11 07-02-2007 05:30 PM

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.

0ddba11 07-02-2007 05:31 PM

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.


All times are GMT -5. The time now is 05:32 PM.