5

from bash, I'd like to kick off a new process in a new terminal, wait till it finishes, and then recover the exit code.

for example, I'd like to do something like this, but actually recover the useful exit codes;

gnome-terminal -e "bash -c \" sleep 2s ; if [ '1' == '1' ] ; then exit 2 ; else exit 3 ; fi \""
echo gnome terminal returns $?

#gnome terminal returns 0

anyone know how?

user1733212
  • 111
  • 8

3 Answers3

2

I guess I'm a bit late to the party, but for whoever is searching this:
With the --wait option, it waits till the newly opened terminal finishes and you are able to get the exit code.

gnome-terminal --wait -- bash -c 'echo foo; sleep 1; exit 42'; echo $?
Turvaldeon
  • 21
  • 3
1

Did you ever figure this out? I realize this isn't quite the answer you're looking for, but it could work in a pinch:

$ gnome-terminal -e 'bash -c "bash -c \"exit 5\"; echo $? > \$\?"'
$ cat '$?' 
5
Billy
  • 607
  • 2
  • 8
  • 20
  • You're actually writing the exit code to a file? As you said, if you're really in a pinch... – csl Oct 13 '15 at 14:58
0

You probably have to rely on a fifo to communicate between the two shells as gnome-terminal does not seem to propagate exit status. This add the benefit of blocking the calling shell while reading the fifo until the result code has been written.

sh$ TMPDIR=$(mktemp -d)
sh$ F=$TMPDIR/fifo
sh$ mkfifo $F
sh$ gnome-terminal -e 'bash -c "bash -c \"exit 5\"; echo $? > '"$F"'"'
sh$ cat $F
5
sh$ rm -rf $TMPDIR 
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125