run xterm -e without terminating
Asked Answered
S

5

21

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?

Stammel answered 1/6, 2012 at 6:21 Comment(7)
Don't background your commands. Is that an option?Jowl
no, i'm running a few togetherStammel
May I suggest you explore GNU screen as a terminal manager instead of using a graphical terminal manager? Take a look at www.gnu.org/s/screen/. Packages are available for all flavours of linux (in their default package repos). You can then connect screen to the terminal manager of your choice. Not the solution you're looking for, but a better option in the long run.Massif
I'll keep it in mind for next time, I'm a blink away from finishing a big project :)Stammel
Also explore wait shell builtin. It waits for and reports the commands sent to the background with & . You'll need to use at the end of your script. It'll keep your script from terminating till all background jobs are finished.Massif
I think the correct answer is: xterm -e "cd /etc; bash" but it has less votes than "-hold" option which leaves the xterm unusable. Note that I cannot comment or modify the vote display due my limited privileges.Dirac
Neither the ";bash" or "-hold" options work for my particular case. I am trying to run gdb in several windows because my MPI application is throwing an error of some sort, but whatever the error is, it is killing my xterm windows. Perhaps some signals are getting sent? How can I stop the windows getting killed?Irreverent
M
7

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:~$ 
Massif answered 1/6, 2012 at 6:47 Comment(3)
tnx,wait ${!}, solved the problem , it wait until the last job at the background endsStammel
wait without the ${!} waits for all jobs. However wait ${!} waits for the last background PID. if the last job terminates before the older jobs, you'll exit even if older jobs are runningMassif
@Stammel please accept the answer if it solved your problem :)Massif
M
32

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.

Marler answered 9/11, 2012 at 6:52 Comment(0)
W
12

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.

Waterworks answered 5/5, 2014 at 22:56 Comment(1)
"xterm -fullscreen -hold -e <command>" is even worse: ^C and exit don't work. Only ALT+F4 if you don't panic.Smuggle
M
7

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:~$ 
Massif answered 1/6, 2012 at 6:47 Comment(3)
tnx,wait ${!}, solved the problem , it wait until the last job at the background endsStammel
wait without the ${!} waits for all jobs. However wait ${!} waits for the last background PID. if the last job terminates before the older jobs, you'll exit even if older jobs are runningMassif
@Stammel please accept the answer if it solved your problem :)Massif
S
6

Building on a previoius answer, if you specify $SHELL instead of bash, it will use the users preferred shell.

xterm -e "cd /etc; $SHELL"
Slippy answered 7/2, 2017 at 22:46 Comment(0)
G
1

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.

Geologize answered 6/2, 2021 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.