New iTerm tab with command from bash script - bash

I am trying to automate a few things in the morning when I get to work, like start the docker env, start the front end stuff etc...
I want to type in a terminal command that will open up a terminal tab (iterm) for each of these tasks.
I have managed to get the tabs to open, but I cannot figure out how to make them run commands.
#!/bin/bash
open -a iTerm . "start_docker.sh"
However, this just opens up a black terminal window.
Ideally, I would like to not also have separate script files.

Related

WSL - Launch bash from Powershell in new terminal and pass a command to it

I am trying to make a simple Powershell script to quickly setup my dev environment. For that I need a few instances of WSL programs running on bash terminals.
From Powershell, I am trying to:
Open a new terminal window
Start bash
Run a command with bash - In my use case I just want to run a simple npm start within bash.
A plus is if I can do all of this in one script line.
I think I am close. If I use start powershell I can start a new terminal. That inmediately opens a new PowerShell terminal.
Then, I can pass PowersShell commands to it like so:
start powershell{bash}
This opens a new terminal window and immediately opens bash.
A way to pass commands to bash in PowerShell is like this:
bash -c "npm start"
This works well. It opens bash in the same terminal and then runs the command I am passing to it. npm start works just as if I was calling it directly from bash. The problem comes when I want to pass the npm start to the new terminal. This is what I am trying:
start powershell{ bash -c "npm start"; Read-host}
This opens the new powershell terminal and it seems to be opening bash. ; Read-host is added so that the terminal doesn't close immediately. However, instead of running the npm command, it reacts by showing me information about the npm command instead of actually running it.
Is there a workaround so that I can actually get the command to run in the new terminal window after opening bash?
I believe that the recommended way to start commands in the WSL environment is to use wsl.exe now. (https://learn.microsoft.com/en-us/windows/wsl/reference#wslexe)
Try start powershell{wsl -- npm start; Read-Host}

Is there a way to revert back to the command line after opening an application through it?

I want to be able to open an application on the command line, but instead of switching to the application, I want to stay on my terminal emulator. Is there a way of accomplishing this? I am using OS X.
Use the -g flag of open, which avoids bringing the app to the foreground.
$ open -g /Applications/TextEdit.app
$
open will start the app, and then return to the command prompt.
After you run the program hit ctrl+z and type bg. You will return to your terminal CLI.
Whenever you want to go back to your program, just type fg.
You can background the job. In the Bash shell, this is done with the &. For example:
some_script_or_application &
Note that some daemons and processes background themselves. For example, on OS X, running open some.pdf will preview the PDF in a GUI while returning the command prompt immediately without needing to do anything special.
See the GNU Bash Manual for more on job control for background jobs.

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.

How do you run a command in iTerm when it launches?

I'm working on configuring the iTerm terminal emulator for the Mac to do what I want. Apparently everything is done through what they call "bookmarks." OK, fine. I'm trying to create a bookmark that will open a tab, cd to a certain Rails project, and run the command script/server. What's supposed to happen is that this will launch the server daemon ("Mongrel") and I'll see the output scrolling by every time I look at that tab.
In the config dialog, under "command" I put script/server and under "working dir" I put the project directory.
What happens is that the tab appears for 1/10th of a second then vanishes.
Recalling a similar problem I had with the Unix screen command, I tried putting a "command" of bash -c 'script/server' but the result was identical.
You're running into that problem because your script runs and then terminates. All you need to do is put a read or something equally sophisticated to say "Press any key to complete script and close window...." at the end of the script.
update
I wrote this test script:
$ cat echoscript
#!/bin/bash
echo "Hello world"
read text
$
I created a bookmark so:
name: test
command: /Users/chasrmartin/echoscript
Working directory: /Users/chasrmartin
When I open the bookmark test, I see my "Hello world", and it waits until I type return. When I type return, it goes away.

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