T. Roncoli T. Roncoli - 7 months ago 11
Bash Question

How to launch a shell command on an already opened specific terminal?

I have a script

./runStart.sh
that creates a new xterm and in this xterm launch the
script run.log
command.
Then, to finish the process I launch a script
./runStop.sh
that kills the xterm and do other stuff behind.
The problem I have is that as I kill the process while running, there is no
exit
call, so the
script
command does not save the data in the actual file.

Do you know how I could launch a command in the second script
./runStop.sh
that makes this
exit
call instead of the
kill <pid>
command ?

Answer

You can use the bash builtin trap to catch a signal (except KILL and STOP) and do the required cleanup.

For example, here i am catching the pseudo signal EXIT that would cover any signal that would make the shell to exit:

xterm -e '/bin/bash' -c 'trap "echo ok >/tmp/foobar" EXIT; sleep 5'

sleep 5 is my main task and echo ok >/tmp/foobar is cleanup task upon exiting.

Alternately if are sure about the signal to be sent/received e.g. HUP, INT, you can trap those directly.

Comments