Running multiple commands in a batch script for wsl - bash

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"

Related

How to get pkill on git bash

background
years ago I made a Minecraft spigot server and found a bash script to turn it on, in the script, it has a restart function that makes use of pkill to kill the old server and start a new one, I tried using it on my new spigot server to find that bash doesn't contain pkill, any google search I use relating to "how to get pkill on git bash windows 10" doesn't give me anything useful, the script has the following in it, the file is a .sh if that matters:
# /bin/sh
while true
do
java -Xms4G -Xmx6G -jar spigot-1.16.3.jar nogui
pkill -f "spigot" #Not really needed, just there in case of anything.
echo Its off..sleeping for 5
sleep 5
done
My question is,
what can I use to kill the process by name like pkill would, or could I somehow install pkill to just work?
any help is appreciated and I apologise in advance if this is a stupid question.
If the shell is to be executed in a Windows Git bash session, then you do have access to Windows commands, like taskkill, in said bash session.
For example, in my bash session, I can type:
vonc#voncav MINGW64 /d/git/gtarsum (master)
$ taskkill
ERROR: Invalid syntax. Neither /FI nor /PID nor /IM were specified.
Type "TASKKILL /?" for usage.
In your case, a taskkill /F /IM spigot should be enough.
On Windows I use the Git bash in combination with PsTools. So in my opinion you can do a lot of cool things with Windows, too. I will give an easy example. In Linux you can run ps aux | grep spigot. With your Git bash it is ps aux -W | grep spigot. If you would know the <pid> you could kill it by using the pid. In Linux this would be kill -f <pid>. With Git bash it is kill -f -W <pid>. So you need the -W argument to access processes running in Windows. The Git bash sadly does not support a pkill command. An alternative to taskkill /F /IM spigot could be using PsKill from PsTools. You could simple run PsKill -t spigot.

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.

How to start composer-rest-server as background process?

I am trying to start composer-rest-server in background using following command :
composer-rest-server &
Its getting started as background process, but then process is getting stuck after displaying "hyperledger-Composer" banner as :
Is there anyway we can start composer-rest-server as background process?
Run command like this:
nohup composer-rest-server -c <network card name> -n never -w true > rest-server.out 2> rest-server.err < /dev/null &
all standard output will be saved in rest-server.out and errors in rest-server.err
& at the end will put it in background and you will get PID
Next time you connect to server and if you want to kill earlier instance then use ps -A | grep "node" command to get PID.
If you run it with no arguments it will prompt for arguments (reading from the keyboard). You should launch it with arguments. If you run it with no arguments from a terminal, answering all the questions, at the end it will print the command line you can use to launch it with the answers suppplied using command line arguments.
You should be able to use that command line with nohup or screen or similar.

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

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!

bash: script doesn't run in the background

I have a script that performs several linux commands. When I run "./script.sh &" it works fine until I cancel the console. However, when I login again the scritp seems to be running in background but not doing anything.
What can be the problem?
Thanks
You forgot to use nohup when starting it.
nohup {command} 0 2>/dev/null &
should work for you

Resources