Shell script to run each command from script in different terminal - linux

As I am new to Linux and using ubuntu and I want help to write a script which will be having multiple commands and for each command a respective terminal should be opened and execute the command.
As I have written a script which having commands like:
mvn tomcat:run -Dmaven.tomcat.port=8081
mvn tomcat:run -Dmaven.tomcat.port=8181
So all these two command should be run in different terminal.

Depending on your Linux flavor there are a number of different terminals available, each of which has a potentially different mechanism for specifying a command.
However, it's pretty likely your Linux will have an xterm which will use a '-e' flag and take a command
e.g.
xterm -e "mvn tomcat:run -Dmaven.tomcat.port=8081"
For Ubuntu you can try
gnome-terminal -e "mvn tomcat:run -Dmaven.tomcat.port=808"

Related

linux "enable -n xxx" command works in terminal but not when put into a script

I've found a very strange issue, when in linux terminal I type "enable -n trap", it would disable the trap linux builtin command. But if I put it into a script like
#!/bin/bash
enable -n trap
and then run the script, there's no error but the command is also not disabled. Really appreciate if someone could share what is happening and how to run it in some file instead of directly in the terminal. Thank you!
The enable command only affects the current shell. When you run a script, that script is executed in a new process, so:
A new shell starts
The enable command runs and disables the trap command in that shell
The shell exits
If you want to affect the current shell, your only option is to source the script using the . (or source) command. If the script is named disable-trap.sh and is in your $PATH, you can run:
. disable-trap.sh
You can also provide a full path to the script:
. /path/to/disable-trap.sh
Sourcing a script like this is largely equivalent to typing the same commands in at the command line: it executes the instructions in the script in the current shell, rather than spawning a new process.

How to make script file to run several commands on several terminals in ubuntu?

I have to open three terminals and each terminal should go to three directories and need to run three files on those three terminals. Now I need speed up my task. Beacuse of that need to do it by running a single script file.How can I do that?
As a example I have to run those three commands on three terminals. But I need to do those thing using one single file running.
cd /home/xyz/Desktop/project/components/core;
python3 Main.py
aria2c --enable-rpc
cd /home/xyz/Desktop/project/ui;
gulp serve
Try to put your commands in one bash file like those below and run it from terminal.
gnome-terminal -e command_1
gnome-terminal -e command_2
gnome-terminal -e command_3
Look here for more details https://askubuntu.com/questions/46627/how-can-i-make-a-script-that-opens-terminal-windows-and-executes-commands-in-the
One of my friends suggested this as an answer and it worked for me.I can run those terminal commands with & operator and then those commands run on background and they will run on the same terminal without any failure.I created a single file using below commands and when I run it, that file prompt to run those commands in same terminal.
!/bin/bash
cd /home/xyz/Desktop/project/components/core;python3 main.py &
aria2c --enable-rpc &
cd /home/kaveesh/Desktop/project/ui;gulp serve &

Execute a command in a new terminal window

I'm on ubuntu 17.04, and I'm trying to execute some commands in sequence, so I have written this shell script:
#!
sudo java -jar ~/Desktop/PlugtestServer.jar
sudo /opt/lampp/lampp start
sudo node httpServer.js
The problem is that after the first command, it execute PlugtestServer and then stop on it, because it is a server and continue to execute it. There is a command in order to automatically open a new terminal and execute PlutestServer in it?
There is way to open a new terminal window and execute command in it using gnome-terminal
The sample format for command is:
gnome-terminal -e "command you want to execute"
gnome-terminal -e "./your-script.sh arg1 arg2"
Hope that helps!!
Your script stays on the first command showing output, you can make the shell move on by adding "&" to the end of your lines. However this might still not do what you want, if you want PlugTestServer to remain running when you log out. For that you should include "nohup" which will keep the command running while piping output to a file.
So, an example:
#!/bin/sh
nohup java -jar ~/Desktop/PlugtestServer.jar > plugtest.out & #Pipes output to plugtest.out, use /dev/null if you don't care about output.
/opt/lampp/lampp start
node httpServer.js
Notice I removed sudo from the script. It's generally better to invoke the script with "sudo" unless you have specific reason to, at the very least it simplifies the commands.
I'm not sure if your second and third command "fork" or "block", so add "nohup" and "&" if you need to.

How to Run Unix command in Matlab in Windows?

In this MATLAB code, command is identified for UNIX platforms. However, I am using Win7. How can I run that command in Matlab in Windows?
command = ['Code/ExternalCode/kmeans/./kmeans_clustering.sh -i ' 'TemporaryResults/Features_ForKmeans' ' -p 2 -d -n ' num2str(k)];
system(command);
labels_kmeans = dlmread('TemporaryResults/Features_ForKmeans.membership');
labels_kmeans(:,1) = [];
You have at least two options, both assume the commands which are executed within the script are runnable on Windows, i.e. the programs that are executed exist and are compiled for Windows.
1.) Try to run the unmodified shell/bash script on Windows:
You need to install an interpreter which can run your script on windows, have a look at this SO question: Is there a way to run Bash scripts on Windows?
2.) Re-write the script to Windows batch format
This depends on the actual script you are running and involves finding the batch equivalent commands which correspond to the ones contained in your .sh script.

Using putty -m option gives 'command not found'

If I open a shell into a machine with: putty -load session_name and then execute a command to add a job to a Grid queue on a linux system (qsub -cwd -b hostname), everything works fine.
But if I add the command to a text file, and then do putty -load session_name -m file.txt, I get qsub: command not found
If I back out and simplify the text file to be only the command hostname and use the -m option, it also works fine.
If I use the Connection->SSH->Remote command, and do something similar as the -m command, I get the same results as from the command line.
I'm very much a novice at linux systems, and this seems like it should be a simple fix to tell something that 'qsub' exists somewhere. Either that or there are some restrictions on these remote access things...
Edit:
Ok, so the initial question was how to run it--and I figured that out (add an absolute path), but there are other environment variable issues as well. It appears that qsub requires the SGE_ROOT variable to be set, but that isn't set for the remote commands window either.
So, a better question is, how do I get the putty remote commands shell (using -m) to open with the same properties and setup as a manual command line shell?
qsub is on your path when you log in interactively, but in the non-interactive shell it is not. Give the full path in the script, or set PATH in the script, and you ought to fix your problem.
It seems you need to run your command in the context of an interactive session, but the sshd protocol doesn't directly do that. So try invoking the command indirectly through /bin/sh.
/bin/sh -i -c "qsub -cwd -b hostname"
The -i makes the shell initialize itself like an interactive one, so it will load all the environment variables in your .profile or .bashrc that are loaded in a real interactive shell. The -c provides a command to run within that interactive shell.
You shouldn't have to explicitly set any paths this way since it works in an interactive session.

Resources