pm2: command not found in ec2 when running script file - node.js

after deploying node.js project using gitlab CICD successfully when try to execute script.sh file which is in side project folder it shows
./script.sh: line 3: pm2: command not found
I want to restart pm2 server after deploying node.js file
now , if I run this script.sh file after ssh into my ec2 from terminal it execute successfully.
here is my script.sh file
Help me to solve this ,
Thank You in advance :)

Okay so , after tried many possibles ways I have find that first go into your root in ec2 using sudo su and then install npm & pm2 into root directory.
after this you can able to run pm2 command from script file.

Also if you can add the below commands and check, it's worked for me.
sudo ln -s "$(which pm2)" /usr/bin/pm2
I worked in AWS EC2 instance.

Related

Launch local node server at system boot

I'm working on a Raspberry Pi running nextjs (node server) connected with nginx. I tried using x-terminal-emulator ‘/usr/bin/bash -c /home/pi/Desktop/startup.sh’ but many of them didn’t recognize /usr/bin/bash as a directory and most of them didn’t budge to execute the script. In how many ways can I start my bash script? What is the most useful way possible?
These are the ways that I tried and the errors corresponding to them.
contrab -e: Didn’t budge to execute the command line
rc.local: DIdn’t budge to execute the command line
systemd: /usr/bin/bash file or directory doesn’t exist.
I’ve must of been using them incorrectly. If so what is the way to use them?
This recipe allows you to launch node server at system boot. In this recipe it is assumed that you have a node server running on port 3000.
Steps
Add node server to the rc.local file
sudo nano /etc/rc.local
Add the following line to the rc.local file
sudo node /home/pi/node_server.js
Make rc.local executable
sudo chmod +x /etc/rc.local
Reboot the system
sudo reboot
Verify that the server is running at boot
sudo netstat –lntp | grep 3000
This should output node server which is running on port 3000.
Verify that the server is accessible from remote computer
curl -i http://<ip_address>:3000

Running a bash script (with some npm commands in it) in Ubuntu by double-clicking on the bash-file runs into `npm: command not found` error

I am currently trying to set up my system so I can run my bash file (startWebApp.bash) on Ubuntu by double-clicking on it. Unfortunately, that is not working, but when I run the script in the terminal with ./startWebApp.bash it works fine.
The underlying problem seems to be that the $PATH variable is different when running the script by double-clicking on the file. The $PATH variable when I run the file in my Terminal is:
/home/magonba/.nvm/versions/node/v14.17.6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
While when I run the file by double-clicking on it, $PATH is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Why is there a difference and how can I change that?
Reproduction steps:
Create the file startWebApp.bash with the following content:
#!/bin/bash
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
I set in the File Manager under Preferences -> Behavior -> Executable Text Files to Ask what to do (in order to be able to start the application by double-clicking on it).
When I double-click on the file, Ubuntu offers me the options: Run in Terminal, Display, Cancel and Run
I choose Run in Terminal (because I need to provide a password since I am using a sudo command)
Runs into the error(s):
/home/my/project/folder/nodejsapp/startWebApp.bash: line 6: npm: command not found
/home/my/project/folder/vuejsapp/startWebApp.bash: line 8: npm: command not found
Thanks for any help in advance!
The problem you got here is, that nvm is integrated in you .bashrc. But when you run your bash file from the GUI, your bashrc will be ignored, and a very minimal bash shell is started. You can fix this in multiple ways.
Install npm to a system installation path (e.g. with apt)
Change your $PATH in the script
#!/bin/bash
PATH="/home/magonba/.nvm/versions/node/v14.17.6/bin:$PATH"
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
Every time you call npm, give the full path
#!/bin/bash
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
/home/magonba/.nvm/versions/node/v14.17.6/bin/npm start &
cd /home/my/project/folder/vuejsapp
/home/magonba/.nvm/versions/node/v14.17.6/bin/npm run serve &
Include your .bashrc in your script
#!/bin/bash
. /home/magonba/.bashrc
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
Please note.
If you go for solution 2 or 3, every time you change your npm version via nvm, you will have to change your script accordingly.

Command 'pm2' not found

I recently cloned my nodejs express app on ec2 ubuntu instance. I ran npm install pm2 but it didn't have write permissions. So I ran
sudo chown _R $USER /usr/lib/node_modules
After that PM2 got installed but when I hit pm2 start app.js, it shows pm2 command not found. I have installed it globally locally but nothing works.
What should I do? Check out the screenshot of ERROR
pm2 needs to be installed globally (on the server) to function correctly.
Try
sudo npm install -g pm2
Digital Ocean has an excellent tutorial on this.
in your home directory
sudo npm install -g pm2
relogin to OR restart your instance
There is a simple way to solve it, just add the pm2 statement as a new script in your package.json file.
In your root project folder type
nano package.json
Then the package file opens and you can add the following line after the script line
"pm2 start src/<yourappname.js>"
your file should look like this
"scripts": {
"pm2": "pm2 starts src/<yourappname.js>"
}
Then press ctl + x and enter to save
(You ust replace src/<yourappname.js> for the path adn name of your js file, in my case I have a folder called src into my project root folder)
Lastly, just run the script by typing the following line
npm run pm2
and you got it.

Start node server on machine startup

I was trying to start node.js server on startup of the machine (ubuntu 16.04) with upstart by using the the following code in nodeserv.conf file:
#!upstart
description "Node.js server"
author "Sushant Kumar"
start on started mountall
stop on shutdown
respawn
respawn limit 99 5
script
export APP_HOME = "/home/ubuntu/chatbot_server"
export HOME = "/home/ubuntu"
cd $APP_HOME
exec sudo -u ubuntu /usr/local/nodejs/bin/node $HOME/chatbot_server/server.js >> /var/log/chatbotserv.log 2>&1
end script
post-start script
echo "Node Started"
end script
, but I run the command
# start nodeserv
I get the followig error: >>start: Job failed to start.
Can anyone help me please where I am going wrong?
Edit: This server is hosted on AWS EC2 instance (if that helps, I don't think it's relevant, but just in case).
Have a look on PM2.
PM2 is a really powerful Node.js process manager.
After install your app, you can easily set it on startup with:
sudo systemctl start pm2-yourusername
You can do this by running your app as a service. You can use forever to ensure that a given script runs continuously. First of all you need to install forever. Then go to your project directory and install forever-monitor. Now you can start your app.
npm install forever -g
cd /path/to/your/project
npm install forever-monitor
forever start app.js
Now you need to use forever-service to build your node script as a service.Firstly, install forever-service and then install your app as a service.
npm install -g forever-service
forever-service install test
If you want to work on your script, you can replace this code in your script.
export HOME="/root"
exec /usr/local/nodejs/bin/node /home/ubuntu/chatbot_server/server.js >> /var/log/node.log 2>&1

nodejs 'forever' just doesn't do anything

I am trying to run my nodejs app on an Amazon EC2 instance using forever. I installed nodejs npm, and then ran sudo npm install forever -g.
The installation didn't return any errors, so i go ahead and try running my app using forever start server.js, but it won't do anything - no output what-so-ever, not even an error.
I also tried forever --help and just forever, but none of them giving me any response...
When running my app regularly using nodejs - nodejs init.js then it works as expected, but i need to get it running using forever so it won't shut down when i disconnect from the server.
Edit :
Since the only problem i was having was that nodejs was closing when i closed the terminal session to my EC2 server, I solved this by using the linux nohup command like this :
nohup sudo nodejs server.js &
This kept nodejs running in a child process even after I closed the terminal window.
Thanks for the help though! :)
I was also not receiving any stdout input from any forever command and this fix nailed it:
sudo ln -s /usr/bin/nodejs /usr/local/bin/node
Hope that helps someone.
I don't know if this will help, but as an alternative, you can use upstart. Create a script like this:
#this is the upstart service task. move it to /etc/init/nodeapp.conf
description "server for Node App"
author "Neels Grobler"
chdir /root/servers/nodeapp
exec node init.js 1>stdout.log 2>stderr.log
respawn
And run it by typing start nodeapp. To stop type stop nodeapp. You can also change the script so that the node app starts on system boot etc. More info at upstart.ubuntu.com
You should be using forever start server.js not just forever server.js
Several guesses.
If you install using sudo npm install forever -g, you might need to sudo to run forever.
Put a full path of server.js when you run forever such as forever start /home/you/source/server.js or sudo forever start /home/you/source/server.js
On windows, when you run forever like:
forever start app.js
you can find generated log file in file system at:
C:\Users\USERNAME\.forever\SomeLogFileHere.txt
The log files are regenerated for every running script with unique identicator of each file. You can always check which file belongs to which process by:
forever list

Resources