0

I'm writing a script that runs a process in the background while shows a progress bar until the mentioned process ends. my question is, how can I check the exit code of that process??

thanks in advance

juanp_1982
  • 917
  • 2
  • 16
  • 37

2 Answers2

4

The wait builtin will return the exit code of the waited process if you specify a PID or job spec. (Note that it returns zero if you don't specify either of those.)

Here's how you could use it:

#! /bin/bash

(exit 10)&
pid=$!
wait $pid
echo $?

This will print 10.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • but is this true if I'm using sleep instead and a bunch of echo's while the process runs in the background? – juanp_1982 Feb 10 '13 at 18:50
2

You can use the $? variable:

command & #run command in background
pid=$!    #get pid
wait $!   #wait for the sucker to finish
status=$? #exitcode for pid   
Forhad Ahmed
  • 1,761
  • 13
  • 18