How can I make a script that opens terminal windows and executes commands in them?

1

by

JSBach

Last Updated December 06, 2017 21:02 PM

I have three scripts I need to run when I start up my Ubuntu machine, they start services I use in my development environment.

To do that, I manually open three terminals and type in the commands.

Is there any way to create a script that will open three terminals and execute one command in each of these? (Each command should be in a separate terminal window so I can see their output).

Answers 5

gnome-terminal -e command

or

xterm -e command

or

konsole -e command

Pretty much

terminal -e command

To make the terminal stay when the command exits:

In konsole there is a --noclose flag.

In xterm, there is a -hold flag.

In gnome-terminal, go to Edit -> Profile Preferences -> Title. Click the Command tab. Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute with

gnome-terminal...
0 0
2

Instead of hard-coding gnome-terminal, konsole, et cetera, use the Alternatives system. The program that executes the default terminal emulator is:

x-terminal-emulator

On my system, it opens a new instance of Konsole every time I execute this command.

Luckily, the terminals seems to support the -e option for executing a command (I verified it for konsole and gnome-terminal). Arguments after the command are passed to the invoked command. Bash refuses to stay open in my terminal, an additional script is needed to get a terminal:

#!/bin/sh "$@" exec "$SHELL"

If you've saved the previous script as /home/user/hacky and made it executable, you would run your scripts with:

x-terminal-emulator -e /home/user/hacky your-script optional arguments here

The full path is required and /home/user/hacky has to be executable.

My previous attempt to run a script in a new terminal window can be found in revision #2, it was before I realised arguments can be passed to...

0 0
3


Question:

I have three scripts I need to run when I start up my Ubuntu machine, they start services I use in my development environment.

To do that, I manually open three terminals and type in the commands.

Is there any way to create a script that will open three terminals and execute one command in each of these? (Each command should be in a separate terminal window so I can see their output).


Solution:1

gnome-terminal -e command

or

xterm -e command

or

konsole -e command

Pretty much

terminal -e command

To make the terminal stay when the command exits:

In konsole there is a --noclose flag.

In xterm, there is a -hold flag.

In gnome-terminal, go to Edit -> Profile Preferences -> Title. Click the Command tab. Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute with

gnome-terminal...
0 0
4

When you double-click a .command file, Terminal.app will open, and execute /path/to/file.command ; exit; which means it will execute the content of the file.command script, and THEN exit.

If your Terminal is exiting before your executable can receive input, that seems to indicate that either your .command is not calling your executable correctly, or your executable somehow doesn't wait to receive input before exiting.

To be able to help you further, you'd need to provide an example .c/.cpp file to compile, and the content of your .command file.

I myself tried with a .command file that contains only:

$HOME/Downloads/go.sh

And I was able to ask for input in go.sh (using read -p) before the Terminal...

0 0
5

Question

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...

0 0
6

Whether you’re an IT analyst or a regular computer user, there are a lot of tasks you may need to do frequently on your PC. VB Scripts are scaled down Visual Basic programs that serve as Windows scripts that can do anything from pull up important information about your computer hardware, to stopping and starting services or resetting your network card.

It’s possible to learn how to do all of those individual things the normal way, or do them using batch files. But VB scripts are better than batch scripts Better Than Batch: A Windows Scripting Host Tutorial because they’re more flexible. If you store the following scripts in a common place that’s quick to get to when you need it, you can accomplish these tasks in a fraction of the time. You just double-click the script, answer a prompt, and the task is done.

Take a look at the following VB Windows scripts and if you see any you’d like to use, just copy and paste the...

0 0
7

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...
0 0
8

Usually these batch files are written pretty poorly, and I make a habit of re-writing them for the hell of it whenever I root a phone.

That said you can open a CMD "Here" in windows 8.1/10 2012(R2)

To do that click "File" and click "Open Command Prompt" and select if you want a regular otr admin prompt (you can also right click on this and add it to the quick-access bar)

Otherwise you can use this code in a batch file:

start /D "%~dp1" cmd

This will allow you to drag and drop a file or folder onto the cmd file and it will start the cmd window in the directory of the file or folder.

If you want to make the CMD window start in the same directory as some script that you just want to add this line to, then you would use 0 instead of 1.

REM Some code in your current script start /D "%~dp0" cmd REM some code that will execute after the new CMD window is popped up for...
0 0
9

Every day I do the following commands:

[0] Start terminal

Open window -> memcached Open window -> redis-server Open window -> memcached Open window -> devo Open window -> cd /some/path /some/path$ -> rails s --port=3002 Open window -> cd /other/path /other/path$ -> rails s --port=3000

Can these steps be accomplished in a single script? I'm losing my mind doing these steps every time I restart my system

Create an applescript. You will just need to run the applescript and it'll do all that for you :

tell application "Terminal" activate do script "memcached" in window 1 do script "redis-server" -- Each do script opens a new window do script "memcached" do script "devo" do script "cd " do script "rails s --port=3002" in window 1 -- does script in last opened window do script "cd " do script "rails s --port=3000" in window 1 -- does script in last opened window end tell

Save it as an Application so you can double click it to run it. You could...

0 0
10

Sometimes it is not enought to schedule some scripts in the background to run. In this article I will show you how to make a script to open a terminal, execute some commands and allows you to see the output.

This can be done with the virtual terminal you like the most. Add one of the following lines to a startup script. You can open 5 terminals which exec 5 different commands, by pasting one of this lines 5 times and replacing the /path/to/command with the path for your script)

gnome-terminal -e /path/to/command

or:

xterm /path/to/command

or:

konsole -e /path/to/command

If you want to keep your terminal opened after the task is finished, use –noclose for konsole and -hold for gnome-terminal (I don’t remember the flag for xterm right now, sorry.)

$ gnome-terminal -e /path/to/command -hold

$ konsole -e /path/to/command --noclose

Share and...

0 0
11

For examples, this is my bash script

osascript -e 'tell app "Terminal" do script "./process1" end tell' osascript -e 'tell app "Terminal" do script "./process2" end tell'

Basically, it will open two different terminal windows (on mac) and execute defined commands. I try to do this in java by

process1 = Runtime.getRuntime().exec(new String[]{"process1"}); process2 = Runtime.getRuntime().exec(new String[]{"process2"});

The problem is it seems that there is only one terminal is opened (and not visible - it runs in background) and then two command process1 and process2 are executed. But because the process 1 will keep that terminal busy thus process2 cannot run. That's why I want to open different terminal to execute those...

0 0
12

I am making a website which requires a java program to run on the server (it is a ubuntu server). I want to achieve this by executing it from php. I tried 'shell_exec' and 'exec', but they both don't open a terminal window or execute the jar file. It did work on my windows pc, but I want to have it work on my linux server as well. I am using xampp as server.

The command I used that worked on windows:

shell_exec("java -jar PATH/TO/JAR/FILE.jar PARAMETERS");

I fixed it myself by adding '2>&1 &' at the end of the shell_exec() command. This makes the php script wait untill the program is finished.

The new command:

shell_exec("java -jar PATH/TO/JAR/FILE.jar PARAMETERS 2>&1...
0 0
13

I'm not sure if the code is getting run consecutively? I get error message that directory data does not exist for the second command. But it should exist if the first command is executed to change directory. What can I do to fix that?

Applescript:

do shell script "cd /usr/local/mysql"
do shell script "sudo chown -R mysql data/" with administrator privileges
do shell script "sudo echo" with administrator privileges
do shell script "sudo ./bin/mysqld_safe&" with administrator privileges

This won't do what you expect.

What you have to be aware of is that each 'do shell script' command runs in its own shell, completely disconnected from any other do shell script command you have run.

This means that your second shell script (sudo chown -R...) has no concept of the previous shell script that changed to the directory you wanted.

In order to link commands, you need to run them in a single 'do shell script' command, using...

0 0
14

As the title states I have been attempting to spawn a simple OSX terminal window and issue a series of batch commands but to no avail. Here is a summary of a couple things I have tried thus far

1)Firstly I tried the open command:

Process p = Runtime.getRuntime().exec("open -n -F -a /Applications/Utilities/Terminal.app --args ls");

Whilst the preceding code DOES open a terminal window the argument is NOT passed.

2)Next I dropped the "open" command and attempted to execute the terminal app directly passing in the arguments as part of the statement:

Process p = Runtime.getRuntime().exec(new String[]{"/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal","ls"});

Again, this opened the terminal window but the the list files "ls" bash command was not executed.

3)Finally I tried executing the commands directly without specifying the Terminal app:

Process p = Runtime.getRuntime().exec("ping -c 3 google.com;");

Here the reverse was the case...

0 0
15


Hey, Scripting Guy! How can I get the command window to stay open when I right-click a .VBS file and choose Open With Command Prompt??

-- BB

Hey, BB. You know, according to BB’s email signature, he’s 9 years old. If that’s true, well, that’s absolutely amazing. A 9-year-old who knows VBScript? At age 9 the Scripting Guys could barely feed themselves or tie their own shoes.

Which, in their case, simply means that the more things change the more they stay the same.

Of course, we have to admit that, just because BB says he’s 9 years old doesn’t mean he really is 9 years old. After all, the Scripting Editor continues to insist that she’s 23 years old. But how, then, can she explain the photographs that clearly show her attending Franklin Delano Roosevelt’s Inauguration Ball in 1937? The truth is, she can’t.

Instead, she just throws things any time the subject is broached.

Anyway, we can’t be sure that BB really is 9 years old, but...

0 0
16

Here's an interesting problem: Using the AppleScript method to launch a new command in a Terminal window fails if Terminal is "busy"; more precisely, it will open a new window but fail to run the command. For example, try copy+pasting this line in:

osascript -e 'tell application "Terminal" to do script "foo"'; osascript -e 'tell application "Terminal" to do script "bar"'; osascript -e 'tell application "Terminal" to do script "baz"';

When I run this (I'm on Snow Leopard), I get three windows: In the first, the command foo is run, as expected; but in the other two, I just get an empty prompt. And I get empty prompts in all three windows if I've just pasted the command in; apparently Terminal is still busy processing the "paste" operation.

Now, AppleScript might not be the best way of doing this, but I really want to write a script that launches a bunch of Terminal windows, in order, and runs a command in each. Maybe the best way to do that is with a bash script. As long...

0 0
17

is a terminal emulation application.

Synopsis

gnome-terminal [-e, --command=STRING] [-x, --execute ] [--window-with-profile=PROFILENAME] [--tab-with-profile=PROFILENAME] [--window-with-profile-internal-id=PROFILEID] [--tab-with-profile-internal-id=PROFILEID] [--role=ROLE] [--show-menubar] [--hide-menubar] [--geometry=GEOMETRY] [--disable-factory] [-t, --title=TITLE] [--working-directory=DIRNAME] [--usage] [-?, --help]


add an example, a script, a trick and tips

examples

source

gnome-terminal --command "/home/rush/rfid.py"

source

gnome-terminal -e "gdb ./simplefrac"

source

How can I turn off font-antialiasing only for gnome-terminal, but not for other applications?

Not sure if this actually works, but you could try to use .Xdefaults to configure this. Make a file named .Xdefaults in your home dir. Put in the following:

Xft*antialias: false

Effects everything!!! Just find the right...

0 0
18

Advertisement

Linux is the oft-ignored third wheel to Windows and Mac. Yes, over the past decade, the open source operating system has gained a lot of traction, but it’s still a far cry from being considered popular. Yet though that may be true, Linux still earns new converts every day. Will you join them?

The learning curve of Linux is what deters most users from even trying it in the first place. It can be a traumatic experience having to go from a GUI-based operating system like Windows or Mac to one that requires command line fiddling. But if you can get over that initial hump of difficulty, you may find that Linux is surprisingly robust.

If you want a crash course on all that is Linux, we’ve got a great Linux newbie’s guide that will teach you all you need to know. For the rest of you who just want a brief overview of some important commands you ought to know, the following list is all you’ll need.

Newbie Commands

cd – Changes the current...

0 0
19
...
0 0