0

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

tmux
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 ?

  • 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.. – Sachin Jain Mar 18 '15 at 14:21
  • @YoMismo No On Even pressing CTRL+Z it stopped my node server instance.. – Sachin Jain Mar 19 '15 at 03:23
  • are you sure? did you check the jobs? did you run the bg command? – YoMismo Mar 20 '15 at 07: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. – Sachin Jain Mar 20 '15 at 08:17
  • CTRL+Z should take your process to a sleep state, It may seem that your node stopped but it didn't, it just went to sleep. If you are able to press CTRL+Z you should able to check the jobs, jobs command will show you those that are sleeping/running/... You will also be able to run bg command which will allow you to wake up a process (the node in this case) and keep it running in the background. – YoMismo Mar 20 '15 at 09:26
  • @YoMismo Thanks..You were right...It do not stop the node app. It just shows a message like this [1] stopped node app.js but it keeps on running. Thanks a lot for the help..Please put this as an answer and I will be happy to accept with an upvote. – Sachin Jain Mar 20 '15 at 09:39

1 Answers1

0

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
fi

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
fi
inVader
  • 248