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

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.

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.

Run node server in background and start karma from npm script

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.

Start/Assign macOS app with root privileges

I have written a macOS Cocoa app using XCode 9.2.
The purpose of the app is as a wrapper to a set of shell commands so that users can run commands easily as required. The shell script allow the users to control a particular device driver for which the software is written by another party.
I have managed to get the app to invoke the shell command as root.
But there are some sub-commands that fail. The actual commands run are, and I think it is the 2nd "stop -a" that fails:
do shell script \"
cd '/Library/Application Support/ABC' ;
python '/Library/Application Support/ABC/abc.py' stop -d;
python '/Library/Application Support/ABC/abc.py' stop -a;
python '/Library/Application Support/ABC/abc.py' stop -g;
python '/Library/Application Support/ABC/abc.py' stop -t;\"
with administrator privileges
When I run the same commands in Terminal they work, either as discrete commands or in a shell script.
As a last resort I ran the app from the command line using
sudo ~/Library/Developer/Xcode/DerivedData/appname/Build/Products/Debug/appname.app/Contents/MacOS/appname
And this works perfectly.
So, it seems that when I run using the NSAppleScript framework, which correctly prompts me for the root password, there is still something amiss which causes the command failure. But when the whole app runs under sudo everything works.
So the solution appears to be to set the app to require Root privs. Is there a setting that can be set for the built project that would automatically set this such that the root password is requested on startup?
Seems to be working in that I do not get the error, but would still like some pointers on how to set an app to run as admin.

Launching a bash script on boot on OS X

UPDATE:
The proposed link was the solution to this question. On a Mac you have to make symlink:
sudo ln -s /usr/local/bin/node /usr/bin/node
I wanted to automate some processes on boot of a mac mini.
The script should fire up some node.js scripts (forever). The script itself is fired by a launchd process. I have several scripts that work like that. But that one I have a problem:
#!/bin/bash
export PATH=/usr/local/bin
export NODE_ENV=production
cd /Volumes/Services/Proxy
forever -a start /Volumes/Services/Proxy/app.js
This script works pretty fine if executed in the terminal. It makes what it does.
But this script throws errors on std.err when launched on boot.
env: node: no such file or directory
First I thought it's a problem because the PATH to "forever" ist not known on startup so I added the export PATH explicitly.
It throws the same error when I give the absolute path to "forever":
#!/bin/bash
export NODE_ENV=production
cd /Volumes/Services/Proxy
/usr/local/bin/forever -a start /Volumes/Services/Proxy/app.js
There is no error in the launched.plist file. If I add other content to this script like for example mkdir FOO it does this without problems.
... im not so firm with shell scripting... :(
Anyone knows what I'm doin wrong?

Resources