How can I implement the following scenario in Ubuntu Linux?
I want to go to my console, then execute ./startdev.sh and then
1) Terminal 1 pops up, starts /home/foobar/bla1.sh
2) Terminal 2 pops up, starts /home/foobar/bla2.sh
3) Terminal 3 pops up, starts /home/foobar/bla3.sh
I already figured that the command "gnome-terminal & disown" starts a new terminal.
However, until now, I don't know how to run a command in that terminal.
I accept any answer that gives me either the entire implementation for startdev.sh or a list of commands that I can use.
Thank you!
Try this content for startdev.sh:
#!/bin/bash
gnome-terminal --command=/home/foobar/bla1.sh & disown
gnome-terminal --command=/home/foobar/bla2.sh & disown
gnome-terminal --command=/home/foobar/bla3.sh & disown
But it is not clear for me why you need to disown the launched processes.
Try this script
If you need to simultaneously pop all terminals
#!/bin/bash
gnome-terminal -e "bash -c '/home/foobar/bla1.sh; sleep 10'" | gnome-terminal -e "bash -c '/home/foobar/bla2.sh; sleep 10'" | gnome-terminal -e "bash -c '/home/foobar/bla3.sh; sleep 10'"
else if you need to run commands one by one terminal then .,
#!/bin/bash
gnome-terminal -e "bash -c '/home/foobar/bla1.sh; sleep 10'"
gnome-terminal -e "bash -c '/home/foobar/bla2.sh; sleep 10'"
gnome-terminal -e "bash -c '/home/foobar/bla3.sh; sleep 10'"
this script will open multiple terminals and execute the command given within the quotes and i have used sleep to hold the terminal from exiting if not added gnome-terminal will execute command and exit immediately.
Related
I have a script called start.sh:
#!/bin/bash
gnome-terminal --tab -- bash -c "./application1; bash"
gnome-terminal --tab -- bash -c "./application2; bash"
gnome-terminal --tab -- bash -c "./application3; bash"
This script opens three new tabs in the current terminal window and runs a unique application in each one.
Now I want to write stop.sh, which will kill the three application processes and then close the three tabs they were running in. So the first three commands in the script will probably be:
pkill -9 application1
pkill -9 application2
pkill -9 application3
But how do I then close the gnome-terminal tabs I had previously opened in the other script?
Each application has a bash as the parent process, so you should kill the parent as well.
You may find the parent with ps command:
APP1PID=`pgrep application1`
APP1PPID=`ps j $APP1PID | awk 'NR>1 {print $1}'`
kill -9 $APP1PPID
It is sufficient to kill the parent process, it will kill all children as well.
I want to know the PID of the last executed command.
I saw this a lot:
$ command &
$ pid=$!
But I'm searching for the same thing without running the command in the background.
You can use the following construction in scripts:
PID=`sh -c "echo $$; exec your_command -with-arguments"`
echo $PID
So I'm trying to create a shell script to do open up four terminal windows (konsoles preferably) and run a command in each and then keep each of those terminals open so I can continue to execute commands in them if desired.
I tried following the instructions listed here:
How to create a shell script to launch 3 terminals and execute a set of commands in each?
and
How can I make a script that opens terminal windows and executes
commands in them?
and after trying those details the best I have is the following:
#!/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
# opens terminal but then I can't control terminal afterwards
xterm -hold -e "echo Hello My World"
# 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
EDIT:
Using Roberto's answer I get four terminals like this, but I can't enter additional commands, notice how there is no prompt like "mycomputername> ":
EDIT 2:
I found an even better way to do what I want. The script below will execute the commands listed in the cmds array in a separate terminal. So echo 'hello1' will run in one terminal, and echo 'hello2' will run in another terminal. This will continue for as many commands listed in the cmds array
!/bin/bash
# Shell script to open terminals
# and execute a separate command in each
# Commands to run (one per terminal)
cmds=('echo 'hello1'', 'echo 'hello2'')
# Loop through commands, open terminal, execute command
for i in "${cmds[#]}"
do
xterm -e "$i && /bin/tcsh" &
done
Konsole
multiple windows
#!/usr/bin/env bash
konsole --noclose -e echo Hello terminal 1! &
konsole --noclose -e echo Hello terminal 2! &
konsole --noclose -e echo Hello terminal 3! &
konsole --noclose -e echo Hello terminal 4! &
multiple tabs
#!/usr/bin/env bash
konsole --noclose --new-tab -e echo Hello terminal 1! &
konsole --noclose --new-tab -e echo Hello terminal 2! &
konsole --noclose --new-tab -e echo Hello terminal 3! &
konsole --noclose --new-tab -e echo Hello terminal 4! &
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
I found this to be quite easily:
#!usr/bin/env bash
echo "Enter the value of n:"
read n
for ((i = 0; i < n; i++ ))
do
xterm -hold -e <enter command> &
# In my case, I used :
# xterm -hold -e sar -P $i 2 5 &
done
And that's pretty much it! Have a good day :)
Note : For those who are newbies, we save this with a file name '.sh'. Also, please note that this will execute n different commands on n different terminals. If you want, you can execute the same command on every terminal, just remove $i from the in do .... done part ;)
On a Linux Mint mate distribution, this will run <commands> in 3 separated terminal windows:
$ cat START.sh
mate-terminal --execute bash -c "<command1>"
mate-terminal --execute bash -c "<command2>"
mate-terminal --execute bash -c "<command3>"
Killing START.sh won't terminate children <commands>.
I want to execute a C program in separate terminal, I've tried this command,
gnome-terminal -x ./test
and
gnome-terminal -e test
But it opens a new terminal and before giving me output, it just vanish.
How could I solve this issue using gnome-terminal?
Thanks in advance
This might be what you search:
gnome-terminal -e "bash -c \"!!; exec bash\""
or (shortly):
gnome-terminal -x sh -c "!!; bash"
It opens gnome-terminal with your last command (!!) executed and it stays open with the command output in the shell, even with an interactive command like top or less...
In your case its:
gnome-terminal -e "bash -c \"./test; exec bash\""
or
gnome-terminal -x sh -c "./test; bash"
When I use the following command, everything works as expected (a new gnome-terminal is open, the current working directory is changed and the terminal remains open):
gnome-terminal -e "bash -c 'cd /';$SHELL"
# ↑
# no space character here
But when I use:
gnome-terminal -e "bash -c 'cd /'; $SHELL"
# ↑
# note the space
I can see a terminal that opens, but I can't see if the current working directory is changed or not because the terminal it closes immediately.
My question is: why this is happening; how can be wrong if I put a space in the second case?
In the first case, gnome-terminal executes
bash -c "cd /;/bin/bash"
in the second case, it runs
bash -c "cd /;" /bin/bash
The first case means "evaluate cd /;/bin/bash", which will cd and run an interactive shell.
The second case means "evaluate cd /; with $0 set to /bin/bash", which will run cd and then exit.
gnome-terminal does its own parsing of the command given to it, using g_shell_parse_argv, which apparently doesn't consider ; to be a word separator, so if a ; is adjacent to a non-whitespace character, it is considered to be part of that non-whitespace character's word.
This can result in surprising behavior if the command you pass to gnome-terminal has shell metacharacters in it.
I used strace on the gnome-terminal process to see what it does. The first command
gnome-terminal -e "bash -c 'cd /';$SHELL"
results in gnome-terminal running the following command
execve("/bin/bash", ["bash", "-c", "cd /;/bin/bash"])
The second command
gnome-terminal -e "bash -c 'cd /'; $SHELL"
results in gnome-terminal running the following command
execve("/bin/bash", ["bash", "-c", "cd /;", "/bin/bash"])
That being said, you should also note that a command such as bash -c 'cd /' will not have a lasting effect on the working directory of any command run after it. So I think that the first command you typed got the desired result only because of the gnome-terminal parsing misfeature, and a more robust way of writing it would be
gnome-terminal -e "bash -c 'cd /;$SHELL'"