bash: terminal: command not found - linux

I am trying to write a Node.js script that will start a Node.js server in a new process, in a new command window.
I believe I am close. I have this:
var n = cp.spawn('sh', [ 'start-server.sh' ]);
the contents of start-server.sh are like so
#!/usr/bin/env bash
node bin/www
this starts the server successfully, but it doesn't open a new terminal window, so I can't see any of the stdio of the spawned process.
So I am thinking that I should open a new terminal window in the bash script and then run the node command, so then the bash script contents would become
#!/usr/bin/env bash
terminal -e "node bin/www"
the problem is that "terminal" is not recognized at the command line. Why is that? I believe the "terminal" command should default to whatever program is being used as your default terminal application.
Please advise if this is the best way to do this and why "terminal" might not be recognized at the command line in OSX, thanks!
this is what is in my path
echo $PATH
/Users/amills001c/.rvm/gems/ruby-2.2.1/bin:/Users/amills001c/.rvm/gems/ruby-2.2.1#global/bin:/Users/amills001c/.rvm/rubies/ruby-2.2.1/bin:/Users/amills001c/google_app_engine_stuff/google-cloud-sdk/bin:/usr/local/bin:/usr/local/bin/meteor:/usr/local/redis/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/Users/amills001c/golang/bin:/Users/amills001c/apache-maven-3.3.3/bin:/Users/amills001c/.rvm/bin

In OS X you would normally run the command like so:
open -a Terminal.app /path/to/script.sh
Which would open a new terminal window and execute your script.

Check the real name of the "terminal" command in your system, to check it, for example, in Ubuntu do "/usr/bin/env | grep terminal", in my case is "gnome-terminal", then use "gnome-terminal -e XXX" should work. Hope it helps J.

What worked for me was this:
var cp = require('child_process');
cp.spawn('sh', [ 'start-server.sh' ], {
detached: true,
env: {
NODE_ENV: process.env.NODE_ENV
}
});
#!/usr/bin/env bash (start-server.sh)
open "./run-node.sh"
#!/usr/bin/env bash (run-node.sh)
node ./bin/www.js
the open command will open the .sh file with the default app, which happens to be iterm2 for me, not terminal.app
the next problem I have is I have to pass paths as arguments to .sh files and I don't know how to do that yet

Related

Ubuntu Shell Script opening two table and run two command using one shell script

I use two separate shell scripts to run my two server, one is Django and another is npm.
Django Command is: python3 backend/manage.py runserver and npm command is: npm start
I write shell script to run my Django server:
This is my startserver.sh file to run Django server
#!/bin/bash
python3 backend/manage.py runserver $1
and this is my startnode.sh file to run npm server
#!/bin/bash
npm start $1
both are working fine.
I want, when I run ./startserver.sh in terminal, it should run this python3 backend/manage.py runserver command in the current tab of the terminal, in the same time, the script should open another tab in the terminal and run this command: npm start
I mean, in one shell script, I want to run two script in same windows two tab of the terminal.
I will just run ./startserver and it should run above two command in two different tab.
I think this command will be help you.
xterm -e [your_args]
maybe your_args will be startnode.sh
In detail, you can script your startserver.sh
#!/bin/bash
python3 backend/manage.py runserver $1
xterm -e “./startnode.sh”
like this.
[Reference]
Opening new terminal in shell script
Start xterm with different shell and execute commands
Solution 1:
tmux
tmux new-session -d 1.sh \; split-window -h 2.sh \; attach
Solution 2:
gnome terminal
Read : Opening multiple terminal tabs and running command
for i in 1 2; do
options+=($tab -e "bash -c '${cmds[i]} ; bash'" )
done
gnome-terminal "${options[#]}"
Update
Read Terminal Multiplexers
Let's say you have both scripts saved under your home directory called a.sh and b.sh. Then make another script combined.sh with the following
sh ~/a.sh &
sh ~/b.sh
Basically, you're just calling both scripts, but you background the first one to allow continuing execution to the next script.

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.

linux bashrc alias to open terminals and run individual scripts

I am trying to make a shortcut alias that does the following for me:
opens a new terminal and inside that new terminal it cd's to a directory and runs a script.
At the same time, opens another new terminal and inside that new terminal it cd's to a directory and runs a script.
I have tried the following:
alias launchmystuff='cd /path/to/directory1/ && gnome-terminal && ./myscript1.sh; cd /path/to/directory2/ && gnome-terminal && ./myscript2.sh'
There are two problems with this:
It opens 1 terminal and is waiting for myscript1.sh to finish BEFORE launching the second terminal.
The terminal that opens and runs the script does not have "possession" of the script that is running. The original terminal that the alias is typed into has "possession" of the scripts that are running. So if I close any of the newly created terminals, the script will not die.
Ideally I would like two terminals to open at once, each running their unique scripts, and for each of them to kill the script when I close their respective terminals.
Does anyone know how to accomplish this?
Thank you
You need to pass the script as an argument to gnome-terminal, rather than running it in the current shell after starting gnome-terminal. As usual, you should use a shell function instead of an alias here.
launchmystuff () {
cd /path/to/directory1/ && gnome-terminal -e ./myscript1.sh
cd /path/to/directory2/ && gnome-terminal -e ./myscript2.sh
}

Execute a command in a new cygwin console

I want to do something similar to this using cmd in windows:
start dir c:\
A new console should open with the output of dir c:.
For Cygwin i tried this:
cmd /c start bash 'ls c:\\'
The first part will open a new console, but I dont know how to output the result in the new console.
You can use cygstart to start a program in a new console. Or run it in one of Cygwin's other terminals: mintty, rxvt(-unicode), xterm.
cygstart is good, but not really meant for what is being asked. You can think of cygstart <filename> as doing whatever would happen if you double-clicked something in Windows Explorer -- which means it can open files in the default program as well as start executables. However, both cygstart ... and cmd /c start ... will lose your nice terminal environment, so I'd recommend using something that starts a new terminal window, such as run mintty.
For example:
man ()
{
run mintty --title="man $*" bash --norc -c "command man $#"
}
would open man pages in new windows so that you can view them while still working in your current window.

Avoid gnome-terminal close after script execution?

I created a bash script that opens several gnome-terminals, connect to classroom computers via ssh and run a script.
How can I avoid that the gnome-terminal closes after the script is finished? Note that I also want to be able to enter further commands in the terminal.
Here is an example of my code:
gnome-terminal -e "ssh root#<ip> cd /tmp && ls"
As I understand you want gnome-terminal to open, have it execute some commands, and then drop to the prompt so you can enter some more commands. Gnome-terminal is not designed for this use case, but there are workarounds:
Let gnome-terminal run bash and tell bash to run your commands and then start a new bash
$ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"
or if the commands are in a script
$ gnome-terminal -- bash -c "./scripttorun; exec bash"
The first bash will terminate once all the commands are done. But the last command is a new bash which will then just keep running. And since something is still running gnome-terminal will not close.
Let gnome-terminal run bash with a prepared rcfile which runs your commands
Prepare somercfile:
source ~/.bashrc
echo foo
echo bar
Then run:
$ gnome-terminal -- bash --rcfile somercfile
bash will stay open after running somercfile.
i must admit i do not understand completely why --rcfile has this behaviour but it does.
Let gnome-terminal run a script which runs your commands and then drops to bash
Prepare scripttobash:
#!/bin/sh
echo foo
echo bar
exec bash
Set this file as executable.
Then run:
$ gnome-terminal -- ./scripttobash
for completeness
if you just want to be able read the output of the command and need no interactivity
go to preferences (hamburger button -> preferences)
go to profiles (standard or create a new one)
go to command tab
when command exits -> hold the terminal open
i recommend to create a new profile for just for this use case.
use the profile like this:
gnome-terminal --profile=holdopen -- ./scripttorun
Every method has it's quirks. You must choose, but choose wisely.
I like the first solution. it does not need extra files or profiles. and the command says what it does: run commands then run bash again.
All that said, since you used ssh in your example, you might want to take a look at pssh (parallel ssh). here an article: https://www.cyberciti.biz/cloud-computing/how-to-use-pssh-parallel-ssh-program-on-linux-unix/
Finally this one works for me:
gnome-terminal --working-directory=WORK_DIR -x bash -c "COMMAND; bash"
Stack Overflow answer: the terminal closes when the command run inside it has finished, so you need to write a command that doesn't terminate immediately. For example, to leave the terminal window open until you press Enter in it:
gnome-terminal -e "ssh host 'cd /tmp && ls'; read line"
Super User answer: Create a profile in which the preference “Title and Command/When command exits” is set to “Hold the terminal open”. Invoke gnome-terminal with the --window-with-profile or --tab-with-profile option to specify the terminal name.
Run with -ic instead -i to make terminal close bash proccess when you close your terminal gui:
gnome-terminal -e "bash -ic \"echo foo; echo bar; exec bash\""
As of January 2020, the -e option in gnome-terminal still runs properly but throws out the following warning:
For -e:
# Option “-e” is deprecated and might be removed in a later version
of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to
execute after it.
Based on that information above, I confirmed that you can run the following two commands without receiving any warning messages:
$ gnome-terminal -- "./scripttobash"
$ gnome-terminal -- "./genericscripttobash \"echo foo\" \"echo bar\""
I hope this helps anyone else presently having this issue :)
The ideal solution would be to ask for a user input with echo "Press any key".
But if double-click in Nautis or Nemo and select run in a terminal, it doesn't seem to work.
In case of Ubuntu a shell designed for fast start-up and execution with only standard features is used, named dash I believe.
Because of this the shebang is the very first line to start with to enable proper use of bash features.
Normally this would be: #!/bin/bash or similar.
In Ubuntu I learned this should be: #!/usr/bin/env bash.
Many workarounds exist to keep hold of the screen before the interpreter sees a syntax error in a bash command.
The solution in Ubuntu that worked for me:
#!/usr/bin/env bash
your code
echo Press a key...
read -n1
For a solution applicable to any terminal, there is a script that opens a terminal, runs the command specified and gives you back the prompt in that new terminal:
https://stackoverflow.com/a/60732147/1272994
I really like the bash --rcfile method
I just source ~/.bashrc then add the commands I want to the new startrc.sh
now my automated start.sh work environment is complete... for now 😼
If running a bash script just add gedit afile to the end of the script and that will hold gnome-terminal open. "afile" could be a build log which it was in my case.
Did not try just using gedit alone but, that would properly work too.
Use nohup command.
nohup gnome-terminal -e "ssh root# cd /tmp && ls"
Hope this will help you.

Resources