rajat
rajat

Reputation: 3553

Run commands in seperate terminal using shell script (bash)

I want to launch several applications using "./application_name" and each in separate terminal , how do i do that in bash script.

Upvotes: 0

Views: 6595

Answers (2)

Search Surfer
Search Surfer

Reputation: 1

Try specifying the full path to the command. There are two ways depending on what you are trying to do.

terminal -e "cd ~/dir ; ./application" ; sleep 999 ;"

== or ==

terminal -e "~/dir/application ; sleep 999 ;"

The first will set the current directory before executing the commands. The second will execute the commands without changing the current directory. The "terminal" must be replaced with the desired terminal emulator.

i.e.: xterm -e "~/bin/MyScript.sh ; sleep 999 ;"

The other changes are example changes as well. Check the manpage for the terminal emulator to make sure that -e will work with it or what is needed to do the job of -e ( I do not use xterm myself ). 999 is 999 seconds. The sleep command is set to keep the terminal window open for 999 seconds after the MyScript.sh has finished executing.

The & is missing so the terminal will run in the foreground, not the background. As long as this is the only command executed, the trailing ; is not needed either. In a script, the trailing ; can also be omitted if you wish to run other commands simultaneously with this command. If the commands must be run in a specific order such that one command completes before the next command runs, then the trailing ; is required. A command running in the background also allows other commands to be run simultaneously. The trailing ; is not required when the command is entered into a command-line prompt. It does help to have a space on both sides of a ; between commands ( i.e.: command ; command ). In the examples given, the terminal should remain open while the command is running.

Upvotes: 0

Celada
Celada

Reputation: 22261

I am not sure I understand what you are asking for, but this is probably what you need:

x-terminal-emulator -e "./application_name [arguments]" &

This will start a new terminal emulator in the background which will be running the given command instead of a shell.

If you system does not have a x-terminal-emulator alias, substitute the name of an actual terminal emulator, like xterm or gnome-terminal. They (pretty much) all support the -e option.

Of course this requires that your bash script be running from inside an X11 session in the first place (not from a cron job or something like that), else there will be no $DISPLAY where the new terminal emulators can appear.

EDIT: Whether or not the argument to -e is executed under a shell or directly seems to depend on which terminal emulator is used. For example, xterm runs it under a shell but gnome-terminal doesn't. The upshot of this is that you may or may not be able to supply compound shell commands like cd foobar; ./something & wait as the argument to -e. As a workaround for those terminal emulators that don't run the command under a shell, you can use -e 'sh -c "actual command"'. Proper quoting of special characters gets gets complicated because you have two levels of quoting, but it can be done.

Upvotes: 4

Related Questions