linux bashrc alias to open terminals and run individual scripts - linux

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
}

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.

Run SH script in a new terminal window

I am currently using crontab to run a SH script at boot which navigates to the path of my python script, switches to a different python environment and runs my python script, although it works perfectly fine it runs hidden without a terminal for me to monitor whatever the python interpreter prints like errors, how could I make it so the python interpreter points at a newly opened terminal window?
Here is my SH script (runs with the bash interpreter, not sh):
#!/bin/sh
cd /
cd /home/pi/Desktop/Juvia-py
source defenv/bin/activate
python3 juvia.py &
and my crontab entry:
#reboot bash /home/pi/launcher.sh
Thank you
If you just want to record errors, you could pipe STDOUT and STDERR to files, something like
python3 juvia.py >stdout.log 2>stderr.log &
But if you wanted to open it in a separate window so you could interact you would need to manage STDIN more creatively.

Linux (Raspbian) - Running script at login(auto) and keeping window in foreground for interaction

I have tried everything to make this work. login scripts, LXDE-pi autostart, cron task #reboot, init.d, and I cannot get my script running with a terminal window running in the foreground so that I can interact with it. I can get it to run but only in background. Is there any way I can get a script that simply runs: "python /home/pi/myscript.py" at startup and leaves the terminal window open with the script running for my keyboard inputs? I would rather not use the /dev/input/event if at all possible. Thanks
Simply running python /home/pi/myscript.py at startup will run your script without any terminal. So there is no window that can be kept open. The behavior you want can be achieved by starting an terminal application and let it execute your script.
e. g. using xterm:
xterm -e "python /home/pi/myscript.py"
or lxterminal:
lxterminal --command "python /home/pi/myscript.py"
I was missing a simple flag.. what I did was edit ~/.config/lxsession/LXDE-pi/autostart with
#lxterminal -e /home/pi/autoscript.sh
and in that file, I added
cd /home/pi/
python -i 2Trackmain.py
I wasn't using the -i flag, so every time I pressed Enter to move through the interactive py script, it exited the terminal, using -i will keep the window open for your interaction. And I only had to add the change directory part b/c the script called other scripts in the same directory.

Opening new terminal in shell script

I am writing a shell script in Linux. I want to run some sh files in terminal and they all needs terminal themselves.
How can I open a new terminal to run another scrips?
xterm -e script arguments... &

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