Executing string sent from one terminal in another in Linux pseudo-terminal
No; terminals don't execute commands. They're just channels for data.
You can sort of run a command and attach it to another terminal like this, though:
ls </dev/pts/2 >/dev/pts/2 2>/dev/pts/2
It won't behave exactly like you ran it from that terminal, though, as it won't have that device set as its controlling terminal. It's reasonably close, though.
Open terminal, run command, return to SAME terminal later and execute another command
In order to write commands to a terminal from another program or terminal you must use a system input-output control system call (ioctl). (This may not always be the case but is is the solution I have found). I will also be presenting a solution in Python but I have cited other resources including a method in c below.
First, you need the process identifier (PID) of the terminal instance you wish to send commands to for it to execute. This can be determined in a few ways but the easiest way I found was via the following command:
ps -A | grep bash --color=always
This will output a list of open terminals and their PIDs and pts numbers. The easiest way I find to know which is the one you want is to open a terminal via your program, run the aforementioned command and the recently opened terminal will be the last on the list. I'm sure you can get more fancy with it if you need to be certain but that isn't the point of this question. You will see something like this, where the pts/# is what you're after
108514 pts/2 00:00:00 bash
Next use the following code and simply save it to a .py file of your choice, (credit for this code goes to the answer in the first link below, the Python one). Note that the example below is hard coded to send the "ls" command. Again, either change the hard coded command or make it not hard coded depending on your own preference and use case.
import fcntl
import sys
import termios
with open(sys.argv[1], 'w') as fd:
for c in "ls\n":
fcntl.ioctl(fd, termios.TIOCSTI, c)
Then, simply call the new function and pass it the following path based on the pts number found previously like so:
python <your_fcn_name_here).py /dev/pts/#
Worked fine for me on Ubuntu 14.04. I'll be trying it on CentOS soon. Didn't have to install any python libraries to do it.
Other Resources
This question has been posed differently here:
- In Python: https://stackoverflow.com/a/29615101/7590133
- In C: https://stackoverflow.com/a/7370822/7590133
For more good information regarding IOCTLs:
- IOCTL Linux device driver
Open another terminal to execute a command with ongoing aoutput in bash script - how continue the bash script?
You can achieve this by putting an &
on the end of the command:
konsole --noclose -e blockchain start &
This flags to the shell that it should execute the command in the background in a subshell. The effect is that the shell doesn't wait for the command to finish, and continues immediately.
How to write a shell script to open four terminals and execute a command in each?
You could use a "for" loop, and a "&" to run xterm in background:
#!/bin/bash
# some older test, doesn't work and complains and I get this message on command line: "QApplication::qAppName: Please instantiate the QApplication object first"
# I also can't enter text after command executes
#echo "Hello World!"
#exec konsole --noclose -e cat ~/.aliases
for i in 1 2 3 4
do
# opens terminal but then I can't control terminal afterwards
xterm -hold -e "echo Hello My World" &
done
# didn't do anything
#exit 0
# didn't do anything except make me type exit an extra time where I executed my shell script
#$SHELL
How to invoke another terminal for output programmatically in C in Linux
It is rather unusual to spawn another terminal the way you seem to be doing. A cleaner approach would be to use a file (or a named pipe) to receive the output from your chat program, then run tail -f
(or another program to properly format the output) on another terminal to display it's contents. The first terminal would be used for input (possibly from stdin
), and the second terminal would receive the output of tail
.
A sample command-line usage would be:
Run the chat client, sending any output to a file named "output":
$ ./client [parameters] > output
In another terminal, display the output by reading from this file:
$ tail -f output
Remember that your chat program should be able to handle two different sources of input simultaneously (incoming messages both from the server and from the user), probably using select()
.
How to run a command, which executes another terminal running a script, in the backgroud
Thanks to epx (first person to comment the question) there is actually a very simple way to run 10 different terminals in the background:
screen -d -m ./script
if u run this in a for loop you can create multiple terminals in the background.
Run multiple commands at once in the same terminal
This bash script is for N parallel threads. Each argument is a command.
trap
will kill all subprocesses when SIGINT is catched.wait $PID_LIST
is waiting each process to complete.
When all processes have completed, the program exits.
#!/bin/bash
for cmd in "$@"; do {
echo "Process \"$cmd\" started";
$cmd & pid=$!
PID_LIST+=" $pid";
} done
trap "kill $PID_LIST" SIGINT
echo "Parallel processes have started";
wait $PID_LIST
echo
echo "All processes have completed";
Save this script as parallel_commands
and make it executable.
This is how to use this script:
parallel_commands "cmd arg0 arg1 arg2" "other_cmd arg0 arg2 arg3"
Example:
parallel_commands "sleep 1" "sleep 2" "sleep 3" "sleep 4"
Start 4 parallel sleep and waits until "sleep 4" finishes.
Related Topics
Run Command 'At ' 5 Seconds from Now
Bash Print Stderr Only, Not Stdout
Installing Qt on Linux, Cannot Find -Lgl
Command to Insert Lines Before First Match
Alsa Cannot Set Sample Format[Ffmpeg]
Prepend Data from One File to Another
Mpi_Send Takes Huge Part of Virtual Memory
How to Change the Default Gcc Compiler in Ubuntu
How to Configure the Linux Kernel Within Buildroot
How to Remove Warning About Storing Unencrypted Password After Committing File in Svn
How to Replace Single Quotes with Another Character in Sed
How to Load a Specific Version of R in Linux
How to Copy File to Stopped Docker Container
Open O_Creat | O_Excl on Nfs in Linux
How to Catch the L3-Cache Hits and Misses by Perf Tool in Linux