How to run gnome terminal child processes in differnet terminal - shell

I am writhing shell script. I want three script to run in different terminal. I wrote like this in shell script,
gnome-terminal -x 1.sh
gnome-terminal -x 2.sh
gnome-terminal -x 3.sh
Then parent terminal is waiting to finish for execution of gnome-terminal -x 1.sh. It wont proceed to next script while first script running. If I run these 3 script as background process, they run in 3 different terminal window but, I m not able to kill these 3 process.
I have to manually find there process id's and kill them. I don't want to do this. Is there any better way to do it?

You can get their process id from the command line that launched them:
gnome-terminal -x 1.sh & pid1=$!
gnome-terminal -x 2.sh & pid2=$!
gnome-terminal -x 2.sh & pid3=$!

Related

Process not running in background

I am trying to make an apple script that launches Alacritty and tmux. I have all the parts except the script runs while I'm running Alacritty and I would like it to exit soon after running (in both cases, where a tmux session exists and when it doesn't).
set t to (time of (current date))
do shell script "nohup /Applications/Alacritty.app/Contents/MacOS/alacritty -e /usr/local/bin/tmux attach || tmux new -s general > /dev/null 2>&1 &"
if (time of (current date)) < t + 1 then
do shell script "nohup /Applications/Alacritty.app/Contents/MacOS/alacritty -e /usr/local/bin/tmux new -s general > /dev/null 2>&1 &"
end if
This works as I'd hoped when there isn't a tmux session, but it runs until I quit Alacritty.
I think the solution will be related to job control in AppleScript, but I can't figure it out. I essentially need a way to wait for a second or two and then test if it is running or something.
Okay I figured out another way:
do shell script "nohup /Applications/Alacritty.app/Contents/MacOS/alacritty -e /usr/local/bin/tmux new -A -s general > /dev/null 2>&1 &"
This combines both.

Using [ nohup ksh ] to run a script cannot see the sleep process

I am using CentOS 7.6 and bash shell inside VirtualBox .
I came across a script named MotherScript in my site having this statement :
nohup ksh MyScript &
Both MotherScript and MyScript have a shebang line
#!/bin/ksh
After login, I just fire the MotherScript.
In my previous companies, usually, I used this statement :
nohup MyScript &
Is there any functional/strange/usage difference between these 2 statements ?
Inside MyScript, there is a sleep command to sleep 1 hour repeatedly in a while loop for 7 days.
However, very strange, I cannot see this sleep process using :
ps -ef | grep sleep
Therefore, I cannot kill the sleep process.
Does this nohup ksh play the trick ?
Anyone know the reason behind ?
Thanks.
Alvin
nohup ksh MyScript &
This will run the script using ksh(korn) shell.
nohup MyScript &
This will pick your default shell which you can check using below command
echo $SHELL
you can also change default shell using chsh Utility

BASH get error code in parent terminal from child terminal?

In a bash script I start a new terminal with a command that gives an error. However I don't seem to be able to grab that error code:
#! /bin/bash
gnome-terminal -x bash -c "cat dksdamfasdlm"
echo $?
Output:
0
So I get the error code of the gnome-terminal command instead of the cat one. One suggestion I got, was to make a file with the code and read that from the parent bash. The problem is that I still seem to not be able and read the error code even that way:
#! /bin/bash
gnome-terminal -x bash -c "cat dksdamfasdlm; echo $?; sleep 2"
Output (on new terminal):
cat: dksdamfasdlm: No such file or directory
0
Why is that? Some suggestion on how to solve this? I just want to somehow grab the error in the new terminal from the parent bash.
It seems GNOME Terminal exits immediately after starting, which is obvious if you run for example gnome-terminal -x sleep 10. Since it doesn't wait for the command to finish, there's no way the return code will be that of the command. I could find no option in gnome-terminal --help-all to keep the process in the foreground.
Regarding your second question, you've double-quoted the command, so $? is expanded before running it. This should work:
gnome-terminal -x bash -c 'cat dksdamfasdlm; echo $?; sleep 2'
PS: The -x option is not documented in GNOME Terminal 3.8.4's gnome-terminal --help-all, various references don't help much, and there's no good explanation for why there's a -e option with identical semantics and different syntax.

Bash script to wait for gnome-terminal to finish before continuing script, only works for first instance of script

I have a bash script that opens a new gnome terminal with two tabs that runs more scripts. After the scripts in the two tabs finishes, the main script in the parent terminal continues to run.
When I run multiple instances of this bash script, it no longer waits for the additional gnome-terminals to finish before continuing the parent terminal script.
How do I fix it so that the additional instances of the script runs just like the first one?
Here is the bash script that I'm running. I run additional instances of this by typing sh scriptname.sh in a new terminal.
gnome-terminal --tab --command="expect launchneuron.exp" --tab --command="expect launchmpj.exp"
echo "Simulation Complete"
echo "Plotting Results"
expect -c "
set timeout -1
spawn ssh $username#server
expect \"password\"
send \"$password\r\"
expect \"$ \"
send \"qsub -I -q abc -A lc_tb -l nodes=1 -l walltime=24:00:00 -d .\r\"
expect \"$ \"
send \"sh plotgraph.sh\r\"
expect \"$ \"
send \"exit\r\"
"
#!/bin/bash
date
bash -c "sleep 7" &
bash -c "sleep 5" &
wait
date
As you can see while running this script, both sleep commands will run in parallel, but main thread stalls, while they are running.
Sat. Jule 27 01:11:49 2013
Sat. Jule 27 01:11:56 2013
Replace sleep 7 with expect launchneuron.exp
and sleep 5 with expect launchmpj.exp
and add your plot commands after calling "wait":
echo "Simulation Complete"
...(your code to plot results)

run xterm -e without terminating

I want to run xterm -e file.sh without terminating.
In the file, I'm sending commands to the background and when the script is done, they are still not finished.
What I'm doing currently is:
(cd /myfolder; /xterm -ls -geometry 115x65 -sb -sl 1000)
and then after the window pops up
sh file.sh
exit
What I want to do is something like:
(cd /myfolder; /xterm -ls -geometry 115x65 -sb -sl 1000 -e sh file.sh)
without terminating and wait until the commands in the background finish.
Anyone know how to do that?
Use hold option:
xterm -hold -e file.sh
-hold Turn on the hold resource, i.e., xterm will not immediately destroy its window when the shell command completes. It will wait
until you use the window manager to destroy/kill the window, or if you
use the menu entries that send a signal, e.g., HUP or KILL.
I tried -hold, and it leaves xterm in an unresponsive state that requires closing through non-standard means (the window manager, a kill command). If you would rather have an open shell from which you can exit, try adding that shell to the end of your command:
xterm -e "cd /etc; bash"
I came across the answer on Super User.
Use the wait built-in in you shell script. It'll wait until all the background jobs are finished.
Working Example:
#!/bin/bash
# Script to show usage of wait
sleep 20 &
sleep 20 &
sleep 20 &
sleep 20 &
sleep 20 &
wait
The output
sgulati#maverick:~$ bash test.sh
[1] Done sleep 20
[2] Done sleep 20
[3] Done sleep 20
[4]- Done sleep 20
[5]+ Done sleep 20
sgulati#maverick:~$
Building on a previoius answer, if you specify $SHELL instead of bash, it will use the users preferred shell.
xterm -e "cd /etc; $SHELL"
With respect to creating the separate shell, you'll probably want to run it in the background so that you can continue to execute more commands in the current shell - independent of the separate one. In which case, just add the & operator:
xterm -e "cd /etc; bash" &
PID=$!
<"do stuff while xterm is still running">
wait $PID
The wait command at the end will prevent your primary shell from exiting until the xterm shell does. Without the wait, your xterm shell will still continue to run even after the primary shell exits.

Resources