Bash script to run firebase functions [duplicate] - bash

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.

Related

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 run a sh script with CLI container using the docker exec command? [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 3 years ago.
I want to create s sh script but it stops when "docker exec -it cli bash" is executed and do not go to the next line. How to run the other commands on root?
root#ee3abae377df:/opt/gopath/src/github.com/hyperledger/fabric/peer#
Stops here and i am not able do execute the next command
docker exec -it cli bash
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin#org1.example.com/msp
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CHANNEL_NAME=mychannel
docker exec -it creates an interactive docker container. It starts a new shell in your current terminal. This blocks the rest of the commands from running until you kill or exit the container. The rest of the commands you have will in fact be run, once you exit the container.
I assume this is not desired. You should look into creating an entrypoint in a custom Dockerfile for your docker container where you execute the remainder of the commands in your script.
If you haven't created a Dockerfile before, the getting started guide from Docker is a pretty good intro to everything docker-related.
Executing commands using CLI on any peer
As per the description you have given, cli is pointing to
peer0.org1.example.com:7051. (Please check your docker-compose file
and there would be one service with container name as "cli" and image
as hyperledger/fabric-tools)
When you are executing "docker exec -it cli bash", you are entering
into container of peer0.org1. It provides an interactive shell to
you.
Let us consider we want to install chaincode using cli on Pee1 Org1, create one test.sh file and write the following cammand inside test.sh file
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_ADDRESS=peer1.org1.example.com:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin#org1.example.com/msp" cli peer chaincode install -n cc_name -v cc_version -p cc_path
Here we are passing environment varibles CORE_PEER_ADDRESS and CORE_PEER_MSPCONFIGPATH.
Then just execute test.sh file, the chaincode will be installed on peer1 provided container is already running and environment varibles are correct. (please provide correct chaincode path, name, and version)
You should leverage the option given by #jeremysprofile. But still, if you want to achieve it through shell script then you might want to write a expect script along with shell script or might merge both the scripts in one. It should look something like this :
#!/usr/bin/expect -f
expect "*root*" {
send -- "<your command>\r"
send -- "exit \r"
}
Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be.
Here is the Linux man Page.

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.

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?

What text editor is available in Heroku bash shell? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
I'm trying to update httpd.conf in my Cedar-based Heroku app. I got to my Heroku bash with
heroku run bash
and found the conf dir under apache. But when I try to open any editor vi, vim, or emacs, I can't find any of these programs. How do you edit conf files on Heroku?
I recently turned the original gist into a heroku cli plugin.
Just install:
heroku plugins:install https://github.com/naaman/heroku-vim
And use:
heroku vim
The heroku vim command will drop you into a bash shell with vim installed on your $PATH. All you have to do is retrain your fingers to type heroku vim instead of heroku run bash.
If you don't want to mess around with plugins and just want a copy of nano in your one-off dyno, just run
mkdir /app/nano
curl https://github.com/Ehryk/heroku-nano/raw/master/heroku-nano-2.5.1/nano.tar.gz --location --silent | tar xz -C /app/nano
export PATH=$PATH:/app/nano
This will download a copy of nano from this plugin and put it in your PATH.
there's ed if you're a masochist.
It looks like you can download and install vim for one session:
#!/usr/bin/env bash
curl https://s3.amazonaws.com/heroku-jvm-buildpack-vi/vim-7.3.tar.gz --output vim.tar.gz
mkdir vim && tar xzvf vim.tar.gz -C vim
export PATH=$PATH:/app/vim/bin
This idea was found here.
Even if you could edit the files with vi it probably wouldn't solve your problem because the file system is ephemeral. Meaning... If you edit a file via heroku run bash you aren't actually changing the file for other dynos. To change a file for all dynos you need to either change what you push in a Git repo or change the buildpack. More details:
https://devcenter.heroku.com/articles/oneoff-admin-ps#formation-dynos-vs-oneoff-dynos
The plugin provided by Naaman Newbold is no longer working with heroku-16 stack, so I made a new plugin out of this updated gist.
Install:
heroku plugins:install #jasonheecs/heroku-vim
And use:
heroku vim
In the comments on the Brian Takita's answer link, there is the more recent solution to get Vim working on the Heroku console:
https://gist.github.com/dvdbng/7375821b20f189c189ab1bd29392c98e
Just saved me a lot of time! :)
Debugging on Heroku
Prepare the dyno
After installing naaman/heroku-vim you can create a new ephemeral dyno via heroku vim. As pointed out correctly by other posts you won't be able to see your changes when viewing through the browser because changes won't be propagated, but... you can actually view the changes from inside the dyno itself.
I've only experimented with "browsing" via curl, but if you could get lynx on there, or better yet get an ssh tunnel -- could be really great.
Start the server
The web server won't be running when you instantiate heroku-vim so you'll need to do it yourself. In my example I'm running php:
~ $ cat Procfile
web: vendor/bin/heroku-php-apache2
You can start this command yourself!
~ $ vendor/bin/heroku-php-apache2 2>/dev/null &
[2] 845
It's now running in the background!
curl your website
Dynos start up on random ports. Luckily you know which one because it's the $PORT variable!
~ $ curl localhost:$PORT
Hello World!
Editing
Do your vim thing now, but when you save the file and curl again - you won't see the changes. I don't understand where it's cached, but it's cached. You have to kill the server and restart it.
Restarting the server
Find the process id
~ $ ps -f
UID PID PPID C STIME TTY TIME CMD
u6897 3 1 0 05:34 ? 00:00:00 bash
u6897 582 3 0 05:53 ? 00:00:00 bash vendor/bin/heroku-php-apache2
u6897 652 582 0 05:53 ? 00:00:00 bash vendor/bin/heroku-php-apache2
u6897 653 582 0 05:53 ? 00:00:00 bash vendor/bin/heroku-php-apache2
Here 582 is the parent id -- use that.
kill 582
Wait just 1 second, and then start the server again (you'll get a new process id!). Curling via the same command will now give you the updated page.
An urgent alternative to edit a file in Heroku:
place a copy of it on some remote host. I like to use Gist
edit the file on Gist and when finished get the raw URL to it
wget the raw URL on your Heroku bash
copy the fetched file to the path of original file
I wrote a complete article on How to Edit a File on Heroku Dynos using Nano or Vim, but basically:
You can use command line:
curl https://s3.amazonaws.com/heroku-jvm-buildpack-vi/vim-7.3.tar.gz --output vim.tar.gz
mkdir vim && tar xzvf vim.tar.gz -C vim
export PATH=$PATH:/app/vim/bin
You can use Heroku Plugins: heroku-vim
You can use Heroku Buildpacks: heroku-buildpack-vip
Hope it helps!
If you would like to just view the contents of the file then:
cd to the folder where the file is located e.g. $ cd folder
run cat command + the filename e.g. $ cat filename.csv
There are now a number of buildpacks that include vim: https://elements.heroku.com/search/buildpacks?q=vim
You could add one of these to the Heroku app in question, using support buildpack support.
the alternative way if your server run php is to upload PHP File Manager, it single file and you can download it from
http://phpfm.sourceforge.net/
One can change files in a dyno and see the result without pushing to Heroku:
Install heroku-buildpack-vim buildpack:
$ heroku buildpacks:add \
https://github.com/carloluis/heroku-buildpack-vim
Ssh into a dyno:
$ heroku ps:exec
Create and run start.sh:
#!/usr/bin/env bash
set -eu
export DATABASE_URL=...
bin/rails s -p 4000
Forward port 4000 (second console):
$ heroku ps:forward
Open localhost:4000 in your browser.
Stop start.sh, change a file, start again, refresh the browser page.
I prefer Nano editor, you can use following buildpack...
https://github.com/velizarn/heroku-buildpack-nano

Resources