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?

Samveen
  • 3,482
  • 35
  • 52
jarhead
  • 1,821
  • 4
  • 26
  • 46
  • 1
    Don't background your commands. Is that an option? – Noufal Ibrahim Jun 01 '12 at 06:24
  • no, i'm running a few together – jarhead Jun 01 '12 at 06:26
  • 1
    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. – Samveen Jun 01 '12 at 06:38
  • I'll keep it in mind for next time, I'm a blink away from finishing a big project :) – jarhead Jun 01 '12 at 06:40
  • 4
    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. – Samveen Jun 01 '12 at 06:41
  • 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. – egabs Mar 10 '17 at 16:02
  • 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? – Ben Farmer Nov 07 '19 at 12:23

5 Answers5

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.

imwilsonxu
  • 2,942
  • 24
  • 25
13

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.

Community
  • 1
  • 1
Gid
  • 393
  • 3
  • 8
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:~$ 
Samveen
  • 3,482
  • 35
  • 52
  • tnx,wait ${!}, solved the problem , it wait until the last job at the background ends – jarhead Jun 01 '12 at 06:53
  • 3
    `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 running – Samveen Jun 01 '12 at 06:57
  • @jarhead please accept the answer if it solved your problem :) – Samveen Jan 29 '19 at 05:57
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"
Dan
  • 69
  • 1
  • 1
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.

Chris Wong
  • 403
  • 4
  • 11