kiarash_kiani
kiarash_kiani

Reputation: 27

print in new terminal window

i wrote an script that have multiple threads and in this threads i used "print()" code to print log activity about that thread, but the problem is i don't want to print all these logs at one terminal window. I found this code on ubuntu forum but it's not seems to be an standard way that simply could run on any OS including mac, ubuntu, fedora, ... Is there any standard way to put out data in multiple terminal window or any better ida than this code?

import subprocess

pid = subprocess.Popen(args=["gnome-terminal", "--command=python test.py"]).pid
print pid

Upvotes: 0

Views: 3392

Answers (2)

Chris
Chris

Reputation: 685

a shell (i.e. Bash) isn't really aware of multiple windows. You could use screen to manage multiple shell sessions.

For example to create a new screen session and execute ifconfig inside:

#create a unique name for the screen session (timestamp + random muber)
timestamp_random=my_$(date +%s)_$RANDOM
#create new screen session in detached mode
screen -S "$timestamp_random" -d -m
#stuff (write) command into that screen session + execute (by hitting newline/ENTER)
screen -r "$timestamp_random" -X stuff $'ifconfig\n'

You can then list all screen sessions with:

screen -list

And connect to each session to see the output:

screen -R [sessionname]

Upvotes: 1

Mark Setchell
Mark Setchell

Reputation: 207415

It's hard to understand what you mean from your question, but the following may help. Start a Terminal on OSX and press Command-N to get a second one - so you have 2 terminals open. Now click in either one and type:

tty

and it will tell you the name of the terminal associated with that window, e.g. /dev/ttys000.

Then go in the other open Terminal window and type:

echo Hello > /dev/ttys000        # or whatever the other Terminal was called

You should see the output of the echo command appear in the other Terminal's window and I think that is what you mean from your question.

Upvotes: 0

Related Questions