Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to write a script to do the following:

  1. Open a terminal window which runs a long running command
  2. (Ideally) move the terminal window to the top left corner of the screen using xdotool
  3. Close the terminal window only if the long running command exited with a zero return code

To put it in Windows terms, I'd like to have the Linux equivalent of start cmd /c long_running_cmd if long_running_cmd succeeds, and do the equivalent of start cmd /k long_running_cmd if it fails.

What I have so far is a script which starts xterm with a given command, and then moves the window as desired:

#!/bin/bash

# open a new terminal window in the background with the long running command
xterm -e ~/bin/launcher.sh ./long_running_cmd &

# move the terminal window (requires window process to be in background)
sleep 1
xdotool search --name launcher.sh windowmove 0 0

And ~/bin/launcher.sh is intended to run whatever is passed as a command line argument to it:

#!/bin/bash

# execute command line arguments
$@

But, I haven't been able to get the xterm window to close after long_running_cmd is done.

I think something like xterm -e ~/bin/launcher.sh "./long_running_cmd && kill $PPID" & might be what I'm after, so that xterm is launched in the background and it runs ./long_running_cmd && kill $PPID. So the shell in the xterm window then runs the long running command and if it completes successfully, the parent process of the shell (i.e. the process owning the xterm window) is killed, thereby closing the xterm window.

But, that doesn't work: nothing happens, so I suspect my quoting or escaping is incorrect, and I haven't been able to fix it.

An alternate approach would be to get the PID of long_running_cmd, use wait to wait for it to finish, then kill the xterm window using kill $! (since $! refers to last task started in the background, which will be the xterm window). But I can't figure out a nice way to get the PID & exit value of long_running_cmd out of the shell running in the xterm window and into the shell which launched the xterm window (short of writing them to a file somewhere, which seems like it should be unnecessary?).

What am I doing wrong, or is there an easier way to accomplish this?

share|improve this question
    
Does 'exit' not work? works with my scripts. –  captain_G Nov 8 '12 at 23:12

1 Answer 1

HINT! You could verify the exit code of the previous executed command:

if [ $? -eq 0 ]
then
  exit 0
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.