I'm trying to write a script to do the following:
- Open a terminal window which runs a long running command
- (Ideally) move the terminal window to the top left corner of the screen using xdotool
- 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 commandxterm -e ~/bin/launcher.sh ./long_running_cmd &# move the terminal window (requires window process to be in background)sleep 1xdotool 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?