Execute a command in a new terminal window - linux

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.

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.

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.

How can I run a Linux command that still runs after I close my PuTTY SSH session?

I am connecting to my NAS over putty which is running linux on it.
I wanted to move a big directory from one location to another. Is it possible to keep the process running after I close the putty session ?
I am afraid that if I close putty the files will not be copied to the end ?
Start your task with 'nohup' and put it in the background with '&', e.g.:
$ nohup mv /here /there &
$ exit
and it should continue running.
I would suggest using screen for this.
Start a new screen,
screen -S <name of your screen>
and then you can perform your commands there, detach from the screen and re-attach to it at any time.
Detach by hitting the sequence
ctrl a d
and re-attach by typing
screen -r (or list the screens with screen -l).
Also have a look at Gnu screen survival guide.
You can run it as a background process as follows:
nohup mv source target &
However, you will not be able to interact with the process.
EDIT: nohup is also required to keep it running after the shell exits.
Using nohup would be best solution.
The following command in your terminal may help you out to run the script using nohup and redirect the output in your desired file.
General Syntax
nohup some_command &> nohup_log_file_name.out &
Example
nohup python script.py &> nohup_log_script.out &
So, if you use the above command, you will be able to find the outputs of the command in a log file named nohup_log_script.out

Tomcat script not working when run from Hudson

I'm trying to run a script which stops and starts Tomcat on linux.
When I run it from the command line it works fine. But it does not seem to work when I run the same script from the "Execute Shell" build step in a Jenkins/Hudson job. Jenkins doesn't report any errors but if I try going to the tomcat page then I get a page not found error.
So Jenkins seems able to stop the server, but not bringing it back up.
I'd be grateful for any help.
Try unsetting the BUILD_ID in your 'shell execute' block. You might even not need to use nohup in this case
BUILD_ID=
./your_hudson_script_that_starts_tomcat.sh
Without seeing your script it is difficult to give an exact answer. However you could try adding the following to the start of your script (assuming it is a bash script):
# Trace executed commands.
set -x
# Save stdout / stderr in files
exec >/tmp/my_script.stdout
exec 2>/tmp/my_script.stderr
You could also try adding
set -e
to make the shell exit immediately if a command returns an error status.
If it looks as though Hudson is killing off Tomcat then you might want to run it within nohup (if you're not already doing that):
nohup bin/startup.sh >/dev/null 2>&1 &

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