Run node server in background and start karma from npm script - bash

I'd like to run a node server in background and start karma (on win7). Writing a bash script like the following (and run it with git bash) appears to work, but it reports to a separate window instead of the WebStorm terminal:
#!/bin/bash
node test/server/index.js &
karma start karma.conf.js
package.json
"scripts": {
"test": "test.sh"
},
If I try it with git bash and bash test.sh then it reports to the same window.
I tried to do something similar in npm, but it cannot run background processes.
"scripts": {
"test": "node test/server/index.js & karma start karma.conf.js"
},
No matter how I try it can run things only in a single process, so it waits for the node server to exit, and thus the karma server never starts.
Any idea how to solve the bash reporting to WebStorm terminal or the npm parallelization?
update:
I think I have found the reason: https://github.com/npm/npm/issues/8358 This seems to be a Windows related issue. On Linux it would work properly. So it is not possible to fix the npm script. I think instead of bash I'll move the karma server and the node server to a node script and create a child process for the node server to be Windows compatible. I hope that way the karma logs will show up in the WebStorm terminal.

Cross-platform shell parallelization solution
I had a little time to search more in the topic. Actually there are parallelization tools available for npm and shell scripts, which are cross-platform:
https://github.com/mysticatea/npm-run-all
https://github.com/kimmobrunfeldt/concurrently
https://github.com/royriojas/shell-executor
There was an initiative to merge all of these projects along with others, which was more or less successful: https://github.com/mysticatea/npm-run-all/issues/10. According to one of the contributors npm-run-all is great now, on the other hand the npm-run-all repo does not seem to be that active nowadays, so probably it is better to use concurrently or shell-executor instead.
WebStorm settings / Git bash solution
I set the WebStorm terminal to git bash instead of cmd.exe:
File/Settings > Tools/Terminal > Shell path: "C:\Program Files\Git\bin\bash.exe" > Ok
And I changed the npm script to run with bash:
"scripts": {
"test": "bash -c \"node test/server/index.js & karma start karma.conf.js\""
},
Hopefully the bash commands work the same on Linux too, I have to check with Travis, but there is a very good chance.
Using the bash command for the sh file works too:
"scripts": {
"test": "bash test.sh"
},
Is npm shell configuration a possible solution?
It is interesting that without using the bash command the upper solution did not work. Probably npm started it with cmd.exe and that opened bash.exe in a new window when it checked the header and realized that it is a bash script. And yes, I checked and it uses the cmd.exe by default:
$ npm config ls -l | grep shell
shell = "C:\\Windows\\system32\\cmd.exe"
So another option might be to set the npm shell to git bash and after that I don't have to use the bash in my scripts.
npm config set shell "C:\Program Files\Git\bin\bash.exe"
Well I did exactly that, but nothing changed. I still have to use bash in my scripts and the sh file still opens in a new window. It does not make a real difference, we still need the Webstorm settings to run the script with bash, so it is not a solution.

Related

Bash script to run firebase functions [duplicate]

This question already has an answer here:
Run two shell commands
(1 answer)
Closed 6 months ago.
I'm a complete noob with bash scripting so maybe a stupid question: I have 3 scripts to auto run a react web app along with firebase cloud functions, the project structure is something like this:
-general:
|_start.sh
|_start-frontend.sh
|_start-backend.sh
-backend
|_functions
-frontend
|_my_app
please note that general is an npm project with a package.json file:
{
"name":"my_app",
"version":"0.1.0",
"workspaces":[
"backend/functions",
"frontend/my_app"
],
"author":"Giulio Serra",
"license":"ISC",
"scripts": {
"start": "sh ./start.sh"
}
}
so when I run npm start start.sh is executed:
#!/bin/bash
open -a Terminal "`sh start-backend.sh`"
open -a Terminal "`sh start-frontend.sh`"
here are the content of start-frontend:
#!/bin/bash
cd ../frontend/my_app
npm start
and start-backend:
#!/bin/bash
cd ../backend/functions
kill $( lsof -i:8085,9000 -t )
firebase emulators:start --import /Users/giulioserra/Documents/Applicazioni/my_app/backend/dev_res
I just want to open 2 terminals: one with react and another with the emulators suite with the functions, but all I managed to do is to open one terminal with just the emulator suite (basically start-frontend.sh gets ignored) without any logs.
if I switch the instructions on the file: start-backend it's ignored and start-fronted.sh is executed again without any logs (so I don't have any clues about the port used and compiled warnings).
Any hints on how to fix the scripts such as both start-frontend.sh and start-backend.sh are executed on two different terminal instances with the proper logs?I'm on Mac Monterey btw Thanks.
Turns out that it was simple as:
sh start-backend.sh & sh start-frontend.sh
thanks to triplee for the help.

How to launch WSL as if I've logged in?

I have a WSL Ubuntu distro that I've set up so that when I login 4 services start working, including a web API that I can test via Swagger to verify it is up and working.
I'm at the point where what I want to do now is start WSL via a script - that is, launch my distro, have all of the services start, and do it from Python. The problem is I cannot even figure out the correct syntax to get WSL to start from PowerShell in a manner where my services start.
Side note: "services" != systemctl (or similar) calls, but just executing bash CLI commands from either my .bashrc or .profile at login.
I've put the commands to execute in .profile & .bashrc. I've configured it both for root execution and non-root user execution. I've taken the commands out of those 2 files and put it into a script in the Windows file system that I pass in on the start of wsl. And I've put that shell script in the WSL file system as well. Nothing seems to work, and sometimes the distro starts and then stops after about 30 seconds.
Some of the PS CLI commands I've tried:
Start-Job -ScriptBlock{ wsl -d distro -u root }
Start-Job -ScriptBlock{ wsl -d distro -u root 'bash -i -l -c /root/bin/start.sh'
Start-Job -ScriptBlock{ wsl -d distro -u root 'bash -i -l -c .\start.sh'
wsl -d distro -u root -- bash -i -l -c /root/bin/start.sh
wsl -d distro -u root -- bash -i -l -c .\start.sh
wsl -d distro -u root -- /root/bin/start.sh
Permutations of the above that I've tried: replace root with my default login, and turning all of the Start-Job bash options into a comma-separated list of single-quoted strings (Ex: 'bash', '-i', '-l', ... ). Nothing I launch from the CLI will allow me access to the web API that is supposed to be hosted on my distro.
Any advice on what to try next?
Not necessarily an answer here as much as troubleshooting tips which will hopefully lead to an answer:
First, most of the forms that you are using seem to be correct. The only ones that absolutely shouldn't work are those that attempt to run the script from the Windows filesystem.
Make sure that you have a shebang line starting your script. I'm assuming you do, but other readers may come across this as well. For the moment, try this form:
#!/usr/bin/env -S bash -li
That's going to have the same effect as the bash -li you tried -- It will source both both interactive startup files such as ~/.bashrc as well as login profiles such as ~/.bash_profile (and /etc/profile.d/*, etc.).
Note that preferably, you won't need the -li. Best practice would be to move anything necessary for the services over from the startup scripts to your start.sh script, and avoid parsing the profile and rc. I need to go update some of my answers, since I just realized I've been guilty of giving some potentially bad advice ...
Specifically, though, I'm wondering if your interactive Bash config has something truly, well, "interactive" in it that might be preventing the automatic running of the script itself. Again, best practice would be for ~/.bashrc to only hold configuration that is needed for interactive shell sessions.
Make sure the script is set as executable (chmod +x start.sh). Again, I'm assuming this is the case for you.
With a shebang line and an executable script, use something like:
wsl -d distro -u root -e /root/bin/start.sh
The -e tells WSL to launch the script directly. Since it has a shebang line, it will be parsed by Bash. Most of the other forms you use above actually run Bash twice - Once when launching WSL and another when it finds the shebang line in the script.
Try some basic troubleshooting for your script like:
Add set -x to the top (right under the shebang line) to turn on script debugging.
Add a ps -efH at the end to show the processes that are running when the script completes
If needed, resort to quick-and-dirty echo statements to show where things have progressed in the script.
I'm hopeful that the above will at least show you the problem, but if not, add the debugging info that you gain from this to your question, and we can troubleshoot further.

Bash script behaving different than when manually executing commands into remote server

I have a remote server and I can ssh into it and when I execute npm install it works just fine. I can see that it's installed by calling which npm and i see:
/home/ubuntu/.nvm/versions/node/v15.11.0/bin/npm
Great.
However, when I do this via a bash script, it says
bash: line 1: npm: command not found
My script:
#!/bin/bash
ssh he-int 'npm install'
Why the discrepancy between the two ? It's the same commands...
Sounds like maybe npm is added to your PATH in one of the login scripts (.bashrc, .profile, etc). I especially think this is true because the npm path indicates you are using nvm to manage your npm environment. There may be a call to nvm to add npm to the path in one of those login scripts.
When you run an ssh command like your second command, it doesn't run as a login shell, so it doesn't run the login scripts. You can either:
Try and force it to run as a login shell (ssh -t maybe? not sure).
Try to initialize your npm environment inside your script, probably by calling nvm.
npm may not be in the PATH of the user you're sshing in as using the bash script.
Use this to see what's in your PATH variable:
ssh he-int 'echo $PATH'
You can also just try to use the path directly and see if it works.
#!/bin/bash
ssh he-int '/home/ubuntu/.nvm/versions/node/v15.11.0/bin/npm install'

How to launch a new process in a new terminal in Linux

I'm using Linux Mint 19 Cinnamon. I've an Angular project and I want to do the following things. All these in a separate terminal:
Navigate into project folder and run code . so that I can see the code on Visual studio.
Navigate into project folder and run ng serve to launch my application and keep it running.
Navigate into project folder and run node server.js to start backend server and keep it running.
this is very painful as the project location is-
home/xpert/Documents/social-coder
and it contains two servers:
social-coder/application The angular server that runs on localhost:4200
social-coder/server/server.js The backend server that runs on localhost:3000
Both the servers need to be constantly running for the application to work.
Navigating again and again with a new terminal is what wasting my time. I decided to write a shell script so that I can do all at once with one single click. This is what my .bashrc file contains:
alias first='gnome-terminal | npm start --prefix Documents/social-coder/application'
alias second='node Documents/social-coder/server/server.js'
alias third='code Documents/social-coder'
This is what I've thought. SO, all of the above 3 commands works perfectly If I manually copy-paste them in separate terminals. But again, I have to manually open 3 different terminals and make them run. I'm coming from:
How to write practical shell scripts - Like Geeks
Run command on another(new) terminal window
How to Use GNOME Terminal App
gnome-terminal opens a new terminal but still runs npm start from the same terminal itself.
I admit that I'm a newbie and there are gaps in my knowledge. Please correct me.
For now I've created a command using pipe. It is working but I'm not sure whether this is the preferred way or not:
gnome-terminal -- /bin/bash -c 'npm start --prefix Documents/social-coder/application' | gnome-terminal -- /bin/bash -c 'node Documents/social-coder/server/server.js' | gnome-terminal -- /bin/bash -c 'code Documents/social-coder'
Please feel free to edit this answer.

How to set shell for npm run-scripts in Windows

I'm running npm on Windows and would like to use & style parallel operations in run-scripts
but running in parallel in cmd is kind of messy
in my package.json file I'd like to write-
scripts: { "go": "cmd1 & cmd2"}
but npm executes the script under cmd.exe which does not know about ; I could change this to
scripts: { "go": "bats/bat1.bat") where bat1.bat is a cmd bat file that uses the windows style call or start commands to run commands in parallel. which works but gives me a script that only works on Windows.
It would be a lot simpler if I could get npm to run the script under a bash clone or cygwin.
I tried
config: { "shell": "bash"}
but that still ran cmd.exe
Is there any way to tell npm to run-scripts using a specific shell (not cmd.exe)?
Since npm 5.1
npm config set script-shell "C:\\Program Files (x86)\\git\\bin\\bash.exe"
or (64bit installation)
npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
Note that you need to have git for windows installed.
You can revert it by running:
npm config delete script-shell
Here's one way to do it:
Create a script, such as my_script.sh, in your project bin directory.
In your package.json file, add a line to run the script using bash. For example:
"scripts": {
"boogie": "bash bin/my_script.sh"
}
Now you can run your bash script from npm by:
npm run-script boogie
Not very elegant, but it works.
If you are developing in both Windows and Linux/Unix, then at least this approach is fairly portable to both environments.
Ideally, overriding the npm shell config parameter should work, but npm (at least version 1.4.14) seems in Windows to ignore the setting and use cmd.exe instead.
Use the following command in your bash or Git Bash shell to find out the shell setting:
$ npm config ls -l | grep shell
By default, the output will be:
shell = "C:\\WINDOWS\\system32\\cmd.exe"
However, to override the default shell parameter, you can add (or edit) an npmrc file to the \Users\yourusername\AppData\Roaming\npm\etc directory. Just add the following line:
shell = "C:\\Program Files (x86)\\git\\bin\\bash.exe"
The path you use can be any valid path to bash.exe. Now, if you run the above "npm config ls -l | grep shell" command, you will see the following output, indicating that the shell parameter has been overriden:
shell = "C:\\Program Files (x86)\\git\\bin\\bash.exe"
; shell = "C:\\WINDOWS\\system32\\cmd.exe" (overridden)
One day, perhaps, a new version of npm will pay attention to the overridden shell parameter.
You can also use cross-platform powershell https://github.com/powershell/powershell#get-powershell for npm scripts.
To set for single project, run this from project root folder:
npm config set script-shell pwsh --userconfig ./.npmrc
To globally set for all node projects:
npm config set script-shell pwsh [--global]
just using CMD's way to run .bat!
.json
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"app": "cd build & browser-sync start --server --files 'index.html'",
"bat": "start start-browser.bat",
"starts": "start http://localhost:7777/datas/ && start http://localhost:7777/Info/"
},
.bat
start http://localhost:7777/datas/ && start http://localhost:7777/Info/
Use a specifically created node_module for this purpose. I suggest using npm-run-all, but others exists, such as parallelshell.
Parallelshell example is below for drop-in-replacement for your question.
"scripts": {
"parallelexample1": "parallelshell \"echo 1\" \"echo 2\" \"echo 3\""
},
following command:
npm run parallelexample1
works both on windows and unix(Linux/MacOS).
Interestingly npm-run-all does not support shell commands; therefore we need to put all shell commands to separate scripts like below.
"scripts": {
"parallelexample2": "npm-run-all echo*",
"echo1": "echo 1",
"echo2": "echo 2",
"echo3": "echo 3"
},
Following command:
npm run parallelexample2
works both on windows and unix(Linux/MacOS).
In my case I just needed to run npm start from inside Bash. I run cmd then I open bash by running "c:\Program Files\Git\bin\bash.exe". Under bash shell I then was able to call npm build and npm start succesfully.
You may already have bash if you are using Git. If not, you can install it.
Hope this may save someone's time.

Resources