T. Roncoli T. Roncoli - 2 years ago 68
Bash Question

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

I have a script

that creates a new xterm and in this xterm launch the
script run.log
Then, to finish the process I launch a script
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
call, so the
command does not save the data in the actual file.

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download