This question already has an answer here:

I have a Node.js app which I want to deploy on my VM machine. Before starting the app, I want to set few commands which should be read before the app is started.

I placed those commands inside .bashrc just similar to what I have done on my local environment.

Sample Command in .bashrc

export TOY_DIR=~/work/git/toyPlayer
export TOY_PLAYER_ENV=dev

Now, when I am running my app with node app.js. It is throwing me an error that environment not found. However this thing works perfectly on my localbox.

After some google search, I tried this command

node app.js

It started node server fine but now I am not able to exit from tmux shell. I used CTRL+C to exit but it stopped the node server.

PS: I know I can do remote login to it and work. But I am trying to learn doing things with command line.

What is the best practice to achieve this ?

PPS: I do not want to detect if the shell is controlled from SSH. I want to export some variables before starting my Node app. These variables are environment variables which are read by a config.js present inside my app.

Here is an example, How am I using the environment variables.

var env = process.env['TOY_PLAYER_ENV']

My question is How to set these variables on my remote machine ?

marked as duplicate by Anthon, jimmij, Networker, mdpc, John WH Smith Mar 18 '15 at 19:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • If you want to keep it running, instead CTRL+C use CTRL+Z, after that a line will show the number of the job that has been put to sleep. Run bg %theNumberYouGot and it will keep running in the background. – YoMismo Mar 18 '15 at 14:18
  • @YoMismo Thanks I will try this..This would be very simple solution if it works for me.. – sachinjain024 Mar 18 '15 at 14:21
  • @YoMismo No On Even pressing CTRL+Z it stopped my node server instance.. – sachinjain024 Mar 19 '15 at 3:23
  • are you sure? did you check the jobs? did you run the bg command? – YoMismo Mar 20 '15 at 7:28
  • @YoMismo Yes, No command runs since my node server is running. I am not able to check the jobs and nor able to run bg command. CTRL+Z stops the node server. – sachinjain024 Mar 20 '15 at 8:17

There already is a very similar question with a good accepted answer: How can I detect if the shell is controlled from SSH?

You can either check if one of the bash variables SSH_CLIENT or SSH_TTY is set by checking if their length is not zero:

if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
    # do your stuff here

Alternatively you can check if the shells parent process is named sshd by:

if [ $(ps -o comm= -p $PPID) = 'sshd' ]; then
    # do your stuff here

Not the answer you're looking for? Browse other questions tagged or ask your own question.