Pass environment variable from command line to yarn - windows

I have a code that reads port number from environment variable or from config. Code looks like this
const port = process.env.PORT || serverConfig.port;
await app.listen(port);
To run app without defining environment variable, I run following yarn command.
yarn start:dev
This command works successfully in Linux shell and Windows command line.
Now, I want to pass environment variable. I tried following,
PORT=2344 yarn start:dev
This commands works successfully in Linux shell but failing in Windows command line. I tried following ways but couldn't get it to work.
Tried: PORT=2344 yarn start:dev
I got error: 'PORT' is not recognized as an internal or external command,
operable program or batch file.
Tried: yarn PORT=2344 start:dev
I got error: yarn run v1.17.3
error Command "PORT=2344" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Any idea please? I know, I can define environment variables from System Properties in Windows. But any way if I can do it from command line?

i'd suggest you use the NPM module called cross-env. it allows adding particular env variables on the command line regardless of platform. with that said, you may try:
$ cross-env PORT=2344 yarn start:dev

You can chain commands on the Windows command prompt with &(or &&). To set an environment variable you need to use the set command.
The result should look like this: set PORT=1234 && yarn start:dev.

Found a solution for this problem in Windows command prompt.
Create a .env file in project root folder (outside src folder).
Define PORT in it. In my case, contents of .env file will be,
PORT=2344
Run yarn start:dev
Application will use port number that you have specified in .env file.

Put .env file at root. Then following command will expose content of .env file and then run yarn start command
$ source .env && yarn start
or this command
$ export $(cat .env) && yarn start
If update any variable in .env then close the terminal and open new terminal window and can again run above command. Or else can also run unset command to remove existing var.
unset VAR_NAME

You can use popular package dotenv:
create a file .env in root directory
put all your env vars
e.g.:
ENV=DEVELOPMENT
run your code like this
$ node -r dotenv/config your_script.js
here the explanation:
[https://github.com/motdotla/dotenv#preload]

To define environment variables in the Windows command prompt we can use the set command, you can then split your call into two lines.
set PORT=2344
yarn start:dev
The set command persists within the current command prompt, so you only need to run it once.
The equivalent command in bash is 'export'.

FYI (not a direct answer). I was attempting this in VS Code - passing .env variables through yarn to a JavaScript app. Google had very few examples so I'm sharing this for posterity as it's somewhat related.
The following simply substitutes text normally placed directly into the package.json or script file. Use this to quickly obfuscate or externalize your delivery configurations.
In Environment Variable File (.env)
PORT=2344
In Yarn File (package.json)
source .env; yarn ./start.sh --port $PORT
In Yarn Script (start.sh)
#!/bin/bash
while [ $? != 0 ]; do
node dist/src/index.js $1; #replace with your app call#
done
The app then accepts port as a variable. Great for multi-tenant deployments.

Related

(shell script file) pm2: command not found in crontab task

my shell script
#!/bin/bash
pm2 start server.js
my crontab task
* * * * * /home/ec2-user/abcd/test.sh > /home/ec2-user/cron.log 2>&1
What I got from the log:
home/ec2-user/abcd/test.sh: line 2: pm2: command not found
how to fix it?
Remark:
1.When I execute ./test.sh, it runs normally
2.which pm2 - shows
~/.nvm/versions/node/v14.16.1/bin/pm2
The problem may be because of that PATH environment variable hasn't been set when the cron job is being executed and this is why your shell script can't find pm2. You have to enter the complete address of pm2.
Example if pm2 is in /usr/bin/:
#!/bin/bash
/usr/bin/pm2 start server.js
Or you may want to just set PATH env before the command you want to execute in you shell script.
In normal, when you execute the program in you terminal, the environment variables are set properly. This is why you can run your shell script in your terminal without any problem.
Set the path command like mentioned in the other answer.
PATH="/usr/bin:/bin:/home/ec2-user/.nvm/versions/node/v14.16.1/bin"
Next, locate the path to your server.js file and add the pm2 start command with the path.
pm2 start /home/ec2-user/ .....your path to the file..... /server.js
Complete code:
#!/bin/bash
PATH="/usr/bin:/bin:/home/ec2-user/.nvm/versions/node/v14.16.1/bin"
pm2 start /home/ec2-user/ .....your path to the file..... /server.js

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'

PM2 startup on macOS Catalina

I'm trying to set pm2 to auto start on Mac OS like this pm2 startup
However I keep getting this error:
env: Fusion.app/Contents/Public:/usr/local/bin: No such file or directory
I tried the suggestion from this page: How to use pm2 startup command on Mac? But the even using this command I keep getting the same error: pm2 startup darwin
How can I get pm2 to startup automatically on Mac OS Catalina?
Thanks!
When you run pm2 startup command it suggests you PATH variable. It seems that this variable contains spaces that causes this error. Try to get rid of Fusion.app part of it.
Expanding on zored's answer, what's probably happening is that pm2 isn't generating the correct script because your PATH variable contains spaces (most likely "VMWare Fusion" in your case).
Run echo $PATH, clean it up by escaping the space character using a backslash (\), then manually paste the resulting PATH variable in pm2's generated startup script.
So basically instead of running a generated pm2 script like this:
sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup launchd -u <user> --hp <user_folder>
run something like this:
sudo env PATH=<your `echo $PATH` output with escaped spaces>:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup launchd -u <user> --hp <user_folder>
In case you wonder where the VMWare path parameter is set (and could be changed permanently):
/etc/paths.d/com.vmware.fusion.public
The content of this file is added to the PATH environment

How to execute gcloud command in bash script from crontab -e

I am trying execute some gcloud commands in bash script from crontab. The script execute sucessfully from command shell but not from the cron job.
I have tried with:
Settng the full path to gcloud like:
/etc/bash_completion.d/gcloud
/home/Arturo/.config/gcloud
/usr/bin/gcloud
/usr/lib/google-cloud-sdk/bin/gcloud
Setting in the begin the script:
/bin/bash -l
Setting in the crontab:
51 21 30 5 6 CLOUDSDK_PYTHON=/usr/bin/python2.7;
/home/myuser/folder1/myscript.sh param1 param2 param3 -f >>
/home/myuser/folder1/mylog.txt`
Setting inside the script:
export CLOUDSDK_PYTHON=/usr/bin/python2.7
Setting inside the script:
sudo ln -s /home/myuser/google-cloud-sdk/bin/gcloud /usr/bin/gcloud
Version Ubuntu 18.04.3 LTS
command to execute: gcloud config set project myproject
but nothing is working, maybe I am doing something wrongly. I hope you can help me.
You need to set your user in your crontab, for it to run the gcloud command. As well explained in this other post here, you need to modify your crontab to fetch the data in your Cloud SDK, for the execution to occur properly - it doesn't seem that you have made this configuration.
Another option that I would recommend you to try out, it's using a Cloud Scheduler to run your gcloud commands. This way, you can use gcloud for your cron jobs in a more integrated and easy way. You can verify more information about this option here: Creating and configuring cron jobs
Let me know if the information helped you!
I found my error, the problem here was only in the command: "gcloud dns record-sets transaction start", the others command was executing sucesfully but only no logging nothing, by that I though that was not executng the other commands. This Command create a temp file ex. transaction.yaml and that file could not be created in the default path for gcloud(snap/bin), but the log simply dont write any thing!. I had to specify the path and name for that file with the flag --transaction-file=mytransaction.yaml. Thanks for your supprot and ideas
I have run into the same issue before. I fixed it by forcing the profile to load in my script.sh,loading the gcloud environment variables with it. Example below:
#!/bin/bash
source /etc/profile
gcloud config set project myprojectecho
echo "Project set to myprojectecho."
I hope this can help others in the future with similar issues, as this also helped me when trying to set GKE nodes from 0-4 on a schedule.
Adding the below line to the shell script fixed my issue
#Execute user profile
source /root/.bash_profile

How to Set docker container ip to environment variable dynamically on startup?

I want to export docker container hostname as an environment variable which I can later use in my app. In my docker file I call my script "run" as last command
CMD run
The run file is executable and works fine with rest of commands I perform but before them I want to export container hostname to an env. variable as follows
"run" File Try 1
#!/bin/bash
export DOCKER_MACHINE_IP=`hostname -i`
my_other_commands
exec tail -f /dev/null
But when I enter docker container and check, the variable is not set. If I use
echo $DOCKER_MACHINE_IP
in run file after exporting, it shows ip on console when I try
docker logs
I also tried sourcing another script from "run" file as follows
"run" File Try 2
#!/bin/bash
source ./bin/script
my_other_commands
exec tail -f /dev/null
and the script again contains the export command. But this also does not set the environment variable. What I am doing wrong?
When you execute a script, any environment variable set by that script will be lost when the script exits.
But for both the cases you've posted above the environment variable should be accessible for the commands in your scripts, but when you enter the docker container via docker run you will get a new shell, which does not contain your variable.
tl;dr Your exported environment variable will only be available to sub shells of the shell which set the variable. And if you need it when logging in you should source the ./bin/script file.

Resources