How to create a shell script to launch 3 terminals and execute a set of commands in each? - shell

Currently, I open 3 terminals ('openocd session','telnet session' & 'gdb session') and execute 'a set of commands' in each terminal every time I flash my image on target.
Is is possible to put this in one shell script file and run at once? (Commands on each terminal are NOT dependent on others; except that the terminals should be opened in the order mentioned above)
Thanks in advance !

Which terminal are you using?
A terminal like xterm can start a program instead of a shell. Simply run
xterm -e 'command args ...'
I use this to start my email client:
xterm -e mutt
If you use a different terminal, consult its manual page. I'm pretty sure there's an equivalent for -e.

Related

Create a script to open multiple terminal windows with some commands on Ubuntu

I want to create a script to open multiple terminals and each terminal calls a different command. For example one terminal calls dmesg and a few other terminals call another command. I also want to specify the size of the terminal window. (ie. the terminal displaying dmesg is on the left, the next one is next to the dmesg terminal and so fourth)
The example screenshot is below.
Thanks,
You can use
gnome-terminal --geometry=GEOMETRY1 -- PROGRAM1 [ARG1...] &
gnome-terminal --geometry=GEOMETRY2 -- PROGRAM2 [ARG2...] &
gnome-terminal --geometry=GEOMETRY3 -- PROGRAM3 [ARG3...] &
see https://manpages.ubuntu.com/manpages/bionic/man1/gnome-terminal.1.html for details

WSL closes abruptly when I execute a windows command after sourcing a bash script containing the option `set -e`

Steps to reproduce.
Please note, I am using WSL (Ubuntu 18) version 1.
Windows version 1909 (OS Build 18363.900).
Create a dummy bash script foo.sh containing the option set -e.
#!/bin/bash
set -e
pwd
Execute the following commands in WSL.
$ source foo.sh
$ explorer.exe .
After I execute the second command explorer.exe . WSL closes abruptly.
I just tried it out, and indeed: When I run explorer, the explorer Windows opens in the background, and the process which starts the Explorer returns with exit code 1. I don't know why Microsoft in their infinite wisdom decided to implement it in this way.
Of course, having set -e in an interactive shell is insane. I suggest to unset it. If, the scenario is not an interactive shell (this is not entirely clear from your question), but you run the commands from inside a script, and you do want to stick with -e, start the explorer with
explorer.exe || true
to keep the shell running.

opening multiple terminals with gnome-terminal

I would like to run a .sh file that launches several terminals in specific folders, each of these executing a specific command.
How would I do this using "gnome-terminal" ?
I checked this page "http://manpages.ubuntu.com/manpages/zesty/man1/gnome-terminal.1.html" but I'm still stucked.
Thank you
You can have multiple lines like this in your script:
#!/bin/bash
gnome-terminal --working-directory=/path/to/dir1 --command './some-command1' &
gnome-terminal --working-directory=/path/to/dir2 --command './some-command2' &
very similar to this answer.
Note the use of & to run the terminal in background. Other options should be self-explanatory.

Check if there are any running processes in the current tab of terminal

I have a script that opens up as many terminal tabs as are devices plugged in, then it runs tests on those devices. I would like to clean up all the terminal tabs after my tests are done. I run some things in the background, and I don't know when each process will be done.
How can I check if there are process running in the current tab of terminal?
I plan to do a Command W in AppleScript to kill each terminal command after each tab of terminal has no running processes.
Thanks!
If you use AppleScript, you can check the busy property:
tell application "Terminal"
repeat with t in tabs of windows
if busy of t is false then
do script "exit" in t
end if
end repeat
end tell
exit closes a tab if you set "Preferences > Settings > Shell > When the shell exits" to "Close the window".
One simple solution would be to take each command that you're running in a terminal and append "; exit" (Without the quotes) to it.
For example, if one of your commands was "ls", you would change it to "ls; exit".
Unfortunately, this doesn't work if you want to leave the terminal windows up to see results of what's being displayed. That can be solved by outputting the results of the first commands to some file, though.
Again using the example of ls, you could run "ls >> testfile.txt; exit" to output the results of ls to a file, and then have the terminal window close after it finishes executing.
You can use "jobs" to check if there are any processes running in the background.

Linux equivalent of the DOS "start" command?

I'm writing a ksh script and I have to run a executable at a separate Command Prompt window.
xdg-open is a similar command line app in linux.
see https://superuser.com/questions/38984/linux-equivalent-command-for-open-command-on-mac-windows for details on its use.
I believe you mean something like xterm -e your.sh &
Don't forget the final &
maybe it´s not a seperate window that gets started, but you can run some executables in background using "&"
e.g.
./myexecutable &
means your script will not wait until myexecutable has finished but goes on immediately. maybe this is what you are looking for.
regards
xdg-open is a good equivalent for the MS windows commandline start command:
xdg-open file
opens that file or url with its default application
xdg-open .
opens the currect folder in the default file manager
One of the most useful terminal session programs is screen.
screen -dmS title executable
You can list all your screen sessions by running
screen -ls
And you can connect to your created screen session (also allowing multiple simultaneous/synchronized sessions) by running
screen -x title
This will open up the emulated terminal in the current window where executable is running. You can detach a screen session by pressing C-a C-d, and can reattach as many times as you wish.
If you really want your program started in a new terminal window, you could do something like this:
xterm yourtextmodeprogram
or
gnome-terminal -e yourtextmodeprogram
or
konsole -e mc
Trouble is that you cannot count on a particular terminal emulator being installed, so (again: if you really want to do this) you would need to look for the common ones and then execute the first one encountered.
As Joachim mentioned: The normal way to do this is to background the command (read about shell job control somewhere, if you want to dig deeper).
There are also cases where you want to start a persistent shell, i.e. a shell session which lives on when you close the terminal window. There are two ways to do this:
batch-oriented: nohup command-to-run &
interactive: screen
if you want a new windows, just start a new instance of your terminal application: in kde it's
konsole -e whatever
i'm sure the Gnome terminal has similar options
Some have recommended starting it in the background with &, but beware that that will still send all console output from the application you launch to the terminal you launched it from. Additionally, if you close the initial terminal the program you loaded will end.
If you're using a desktop environment like KDE or GNOME, I'd check the alt+f2 launching apps (gnome-open is the one for GNOME, I don't know the name of the KDE app) and see if you can pass them the command to launch as an argument.
Also, if your intention is to launch a daemon, you should check the nohup documentation.
I used nohup as the following command and it works:
nohup <your command> &
then press enter and enter!
don't forget the last &
for example, I ran a python code listening to port 5000:
nohup python3 -W ignore mycode.py &
then I made sure of running by netstat -tulnp | grep :5000 and it was ok.

Resources