Git Bash closes shell window on Ctrl+C. How to enable standard behavior - exit running operation - bash

On Windows 8.1.
I have written a simple .sh script to start up my dev environment. I know, I can use Windows native batch script (and it works fine, has no given problem), but I prefer Git Bash. The problem is that every Git Bash window opened by my script is closed on Ctrl+C. And I don't want them to get closed but only exit running processes.
Here is my script. It opens four Git Bash windows and starts processes within them. And when I strike Ctrl+C in one of those four windows, the window just closes. Kills the process (except nginx; nginx continues working) and closes. And I only want to stop the process, not terminate the window:
#!/bin/bash
cd /c/nginx
start sh.exe --login -i -c "nginx"
cd /c/Users/user/app
start sh.exe --login -i -c "NODE_ENV='development' nodemon"
start sh.exe --login -i -c "NODE_ENV='development' gulp mytask"
start sh.exe --login -i -c "NODE_ENV='development' compass watch"
How to do it?

If you use a wrapper like Console2 or ConsoleZ around git bash with it's shell pointed to "C:\Program Files\Git\usr\bin\sh.exe" --login -i or "C:\Windows\SysWOW64\cmd.exe /c ""C:\Program Files\Git\usr\bin\sh.exe" --login -i"" it works fine. I'm not sure how to do it without having that wrapper, but it's pretty cool to use one anyway so you could try it out!

Related

Windows bash script to run something from WSL

I am trying to write down a Windows bash script to:
Start Windows Subsystem for Linux (WSL) and
cd within WSL to "../myfolder/"
run ./foo first_parameter second_parameter
Wait until finished and exit WSL
cd within Windows to "../myWinFolder/"
run Foo.exe parameter
wait until finished
This is my attempt:
bash -c "cd ../myFolder/ && ./foo first_parameter second_parameter"
cd ..
cd myWinFolder
START /WAIT Foo.exe parameter
But sadly CMD does not wait for WSL to finish before running the EXE.
Anything I can do?
I'm happy that the interop between dos and WSL does in fact wait for commands to finish. Try the following:
In a file called runit.bat
echo bat01
bash -c "echo bat02; cd ./bash/; ./runit.sh; echo bat03"
echo bat04
In a sub-folder called ./bash/ paste the following in a file called runit.sh
echo sh01
sleep 2s
echo sh02
When you run runit.bat from within dos you will see a wait of 2 seconds
You have not specified what is inside your ./foo script. I suspect that it is running a task in the background or running something that returns immediately. This can be simulated by putting & after the sleep so that it runs in the background within wsl sleep 2s &. When you do this you see that there is no pause in the execution of the script.
So I would check ./foo maybe add some echo debug statements around inside it and run it from within WSL first to make sure that it does indeed wait until all the commands are finished before it exits.

Running multiple commands in a batch script for wsl

I have a batch script which is used to launch 2 bots in WSL at Windows login.
C:\Windows\System32\bash.exe -c "/home/roughnecks/go/bin/irchuu"
C:\Windows\System32\bash.exe -c "node /home/roughnecks/bots/Birba/birba.js"
My problem is that only the first line runs, I guess because it outputs stuff in the terminal and "birba.js" doesn't fire unless I "ctrl-c" in terminal, exiting irchuu.
I already tried different combinations, like using "nohup command &" or "command 2>&1 &" but nothing is working as expected and I need help.
Thanks
Windows bash does not seem to support & to fork a command and continue.
From this Windows-related question START /B was suggested which seems to work:
START /B C:\Windows\System32\bash.exe -c "/home/roughnecks/go/bin/irchuu"
C:\Windows\System32\bash.exe -c "node /home/roughnecks/bots/Birba/birba.js"

Start jobs in background from sh file in gitbash

I need to start a couple of processes locally in multiple command-prompt windows, to make it simple, I have written a shell script say abc.sh to run in git-bash which has below commands:
cd "<target_dir1>"
<my_command1> &>> output.log &
cd "<target_dir2>"
<my_command2> &>> output.log &
when I run these commands in git bash I get jobs running in the background, which can be seen using jobs and kill command, however when I run them through abc.sh, I get my processes running in the background, but the git-bash instance disowns them, now I can no longer see them using jobs.
how can I get them run through the abc.sh file and also able to see them in jobs list?

Open Multiple XTerm Windows Simultaniously

I am working on a Raspberry Pi powered Magic Mirror project and to start the program I execute a shell script that runs in the background continuously. To make the AI part of my project work I need to open a second shell script in the background that also runs continuously. My problem occurs when I try to execute my Xterm commands it waits for the first script to complete before it starts the second script. Since both scripts have no designated end point I am stuck. Is there a way to make both Xterm commands execute at the same time?
Here is my current code to start the Xterm sessions:
cd ~/MMStartAll
xterm -e "cd ~/MMStartAll; ./AssistantStart.sh"
xterm -e "cd ~/MMStartAll; ./MMStart.sh"
$SHELL
Your script should end with a &. This means that both the xterms will run in a sepperate process id (pid).
cd ~/MMStartAll
xterm -e "cd ~/MMStartAll; ./AssistantStart.sh" &
xterm -e "cd ~/MMStartAll; ./MMStart.sh" &
$SHELL

How to prevent PuTTY shell from auto-exit after executing command from batch file in Windows?

I have written a batch file like this:
Start putty.exe -ssh 172.17.0.52 -l root -m dummy.txt
Then in dummy.text I have written this command:
avahi-daemon --no-drop-root -D
export XVHMI_USERCONFIG_PATH=/home/UserProfileConfig
export XDG_RUNTIME_DIR=/tmp
cd /opt/bosch/airis/bin
When I run the .bat file, PuTTY starts, commands execute (hopefully, not sure) and it exits.
How to keep that window open?
I have googled for the same, but no solid help. I read on stack overflow itself that we need to define something in txt file, but what and most importantly how?
The SSH session closes (and PuTTY with it) as soon as the command finishes. Normally the "command" is shell. As you have overridden this default "command" and yet you want to run the shell nevertheless, you have to explicitly execute the shell yourself:
avahi-daemon ... ; /bin/bash
Also as use of -m switch implies a non-interactive terminal, you probably want to force an interactive terminal back using -t switch.
Though, I'm not really sure if you want to execute shell or if you just want to see your command output. If the latter, did you consider using plink? It's console terminal client from PuTTY package. Being console application, it inherits console of parent batch file, and you can pause the batch console from closing using pause command, if needed.
Another option (both for PuTTY and plink) is to pause on remote side. E.g. Using read command.
avahi-daemon ... ; read
As suggested by Martin I tried this step:
putty.exe -ssh 172.17.0.52 -l root -m dummy.txt -t
added /bin/bash at the end of commands in dummy.txt
It worked for me. Please note, you have to follow both the steps as mentioned above.
This way you can keep the session alive and can manually execute further commands.

Resources