Yes, all you need to know is the process id (PID) of the process. You can find this with the ps
command, or the pidof
command.
kill $(pidof ping)
Should work from any other shell. If it doesn't, you can use ps and grep for ping.
When you run a new terminal emulator, that creates a new terminal (/dev/pts/NUMBER
on Linux). A program doesn't have to be started by that terminal to output there (try running tty
in a terminal, then in another terminal run echo hello >/dev/pts/NUMBER
). So you can tell your Python script to read and write from the terminal; all you need is to find out the path to the device file.
With xterm, it's pretty easy: run xterm -e 'tty >&3; myCommand'
with file descriptor 3 connected to a pipe from which your program reads the path to the terminal device.
p = subprocess.Popen("xterm -e 'tty >&3; exec sleep 99999999' 3>&1",
shell=True, stdout=subprocess.PIPE)
tty_path = readline(p.stdout)
tty = open(tty_path, 'r+')
Your Python program can now read and write to tty
. When you've finished, kill the terminal emulator (os.kill(p.pid, signal.SIGTERM)
).
With a terminal emulator that's based on a single process for multiple windows such as xfce4-terminal, what you want to do is considerably harder. Running xfce4-terminal
breaks links between the parent process and the child of the terminal emulator, and doesn't give you a nice PID that you can kill. You could use a named pipe to communicate but it's a bot harder to set up.
Best Answer
Cloning the path is easy if you can run your terminal program from the command line. Assuming you're using
xterm
, just runxterm &
from the prompt of the terminal you want to clone. The new xterm will start in the same directory, unless you have it configured to start as a login shell. Any exported environment variables will also carry over, but un-exported variables will not.A quick and dirty way to clone the whole environment (including un-exported variables) is as follows:
If you've set any custom shell options, you'll have to reapply those as well.
You could wrap this whole process into an easily-runnable script. Have the script save the environment to a known file, then run
xterm
. Have your .bashrc check for that file, and source it and delete it if found.Alternately, if you don't want to start one terminal from another, or just want more control, you could use a pair of functions that you define in .bashrc:
EDIT: Changed
putstate
so that it copies the "exported" state of the shell variables, so as to match the other method. There are other things that could be copied over as well, such as shell options (seehelp set
) -- so there is room for improvement in this script.