0

I have a problem with bash programming. I have this code:

#!/bin/bash
tmpfile="temp.txt"
./child.sh &
sleep 4s

but I want to get the exit code of child.sh. I know that is possible with the costructor wait. There are any constructor with wait+timeout?

Thanks

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
user3694151
  • 37
  • 1
  • 5
  • How do you know `child.sh` is complete after 4 seconds? If it's guaranteed to be complete, then just run it in the foreground within the script. If it might still be running, then you'll need to check for that case. You might want to look at this question: http://stackoverflow.com/questions/1570262/shell-get-exit-code-of-background-process. – lurker Aug 11 '14 at 12:42
  • 1
    What do you want to do after 4 seconds if the child is not done? Do you want to send it a signal, or just be aware of the fact that it is not complete? – William Pursell Aug 11 '14 at 13:03
  • I wrote a answer for a similar problem over here: http://stackoverflow.com/a/41108021/771740 – JonatasTeixeira Dec 12 '16 at 19:39

2 Answers2

4

You can use wait to wait for your forked child to finish

#!/bin/bash
tmpfile="temp.txt"
./child.sh &
# next line wait for just previously forked process
wait $!
# next line exit with the status of previously returned command
exit $?

I suppose you want a timeout on child, the options you need depends on the timeout version you have:

#!/bin/bash
tmpfile="temp.txt"
timeout 4 ./child.sh &
wait $!
exit $?
0
cat file | xargs ./prova.sh; echo $?

this command return the exit code of xargs.can i get the prova.sh exit code?

user3694151
  • 37
  • 1
  • 5