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

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.

Related

-echoctl not being ackonowledged in forked program

Please,
I have a terminal application that requires no echoing of control characters back to the terminal.
I can happily issue 'stty -echoctl' at a terminal, run my application and obtain the result I am after. Further, I can include 'stty -echoctl' in .bashrc and everything is fine. (I have also added it to .profile but that seems to bring in .bashrc anyway)
I can then open another terminal (type 'konsole/gnome-terminal/xterm' in the original console and again I get the result I expect.
The problem I have (and this is in preparation of forking the program form another application) is that if i try
$ xterm -e ./V2.13
or even
$ xterm -e bash -c ./V2.13
the control characters are in fact echoed back to my app.??
EDIT:Additionally is there any need (benefit) in executing bash to execute my application ?

Bash shell is locked after running script

I made my own script to make testing a little faster to execute on my .NET projects.
The name of the script is TestCoverage and is located in /home/user/bin folder with executable permissions and also added to my PATH for convenience.
#!/bin/bash
echo "Collecting TestCoverage..."
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
The script runs as expected but after it's done the terminal stops taking input. I can only press the Enter key and the cursor is blinking as usual.
Bash version 4.4.20 running on Ubuntu 18.04
I've made some testing and it seems to take input but it isn't showed to the console. The problem is not consistent, it comes and goes through different terminal sessions.
Your script (and likely the dotnet process it invokes, because echo is a shell intrinsic that usually returns within milliseconds) is probably still running and hasn't returned.
You can open another console and type ps aux -H to view the running processes - look out for some adjacent lines with TestCoverage, bash, and dotnet.
In those lines, you can see what state the process(es) are in
(RTM for further detail).
If you are running a console under X, you can first look around whether you accidentally pushed some running dotnet test ... window behind your console window. :-)

Is there a way to launch Terminal with a prefilled statement?

I want to open a terminal window with a statement. Is there a process I can run that will do this?
For example, I want to run a script that 1. Opens Terminal and 2. has the following statement:
java -version
The user then can press the Enter key to run the statement
One way to do this is by wrapping the command in a .command file that contains a command to read something, such as:
#!/bin/bash
command="java -version"
echo $command
read input
exec $command
...then, make the script file executable by running chmod +x filename.command on the file that you put this in, and run it (e.g. from the Finder or by using open filename.command). This should launch the terminal, print the command, and wait for the user to press Enter before running it.
You will note that since this is a script, you can customize any of the steps above to do whatever you want, e.g. printing more stuff or running other commands.

Running shell commands from Xcode breakpoint

I'm trying to run a shell script from a breakpoint in Xcode 4.5 DP 3. I set the breakpoint as such using the Choose button.
However, upon hitting the breakpoint I get the following message:
Error in shell command for breakpoint "(selector name)". The command "/Users/Max/Developer/saveToLog.sh" does not exist.
I've tried to put regular shell commands, like "say test" but Xcode prints the same message (with a different command name, of course). The shell script works fine when I run it from terminal.
Ok, first check to make sure you have the shell permissions set correctly. In terminal type:
chmod u+x saveToLog.sh
And I would also locate the shell script inside the project folder (didn't seem to work correctly for me when the script was located on my desktop).
This is the script I tested on:
#!/bin/bash
touch ~/Desktop/ItWorks.txt
echo "This actually works!" > ~/Desktop/ItWorks.txt

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