Node.js - Unable to run node server using forever - node.js

I am trying to run node server using forever command.
I installed forever globally using:
npm install forever -g
After installing forever I try to run my node script by using below command:
node_modules\.bin\forever start app.js
Below is my console:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up f
or at least 1000ms
info: Forever processing file: app.js
Please help me to resolve this issue!

There is no problem here other than warnings for configs forever recommends you declare. If you see the final message there it tells you it has processed your script. Just run forever list and you should see your script running.
I ran into this same thing when installing npm via yum repository ( yum install npm ) and then installing forever whereas when I install node and npm via shell scripts and then install forever it doesn't occur. It must have something to do with the formulas for the package installer or potentially missing alias with flags with installer to set those values behind the scene.
Those don't mean it's not working. See below I created a js file using sample code from node's site and ran it manually (I flushed firewall to open port for app temporarily but you don't need that):
[root#app1 ~]# vi example.js
[root#app1 ~]# apf -f
apf(23924): {glob} flushing & zeroing chain policies
apf(23924): {glob} firewall offline
[root#app1 ~]# node example.js
Server running at http://127.0.0.1:1337/
I then start app using forever:
^C[root#app1 ~]# forever start example.js
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: example.js
Now I check to see if my app is running:
[root#app1 ~]# forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] dan1 /usr/bin/node example.js 23976 23978 /root/.forever/dan1.log 0:0:0:27.320
[root#app1 ~]#

This solved my issue:
forever start -c node [path/to/app]
"-c" means - Run commnad; and then just run via nodejs
This way - you get the Respawn by default of min. 1000ms uptime
Taken from: https://github.com/nodejitsu/forever/issues/422, by "Basarat"

If you are using node js with express framework then script will not start using :
forever start app.js
First stop all running apps:
forever stopall
When this Express framework used it must be started with:
forever start ./bin/www

First stop all running apps:
forever stopall
then use this command. It works for me and solved my issue:
forever -w ./bin/www
and you should find this in package.json file:
"scripts": {
"start": "node ./bin/www"
}
I hope it helps you.

One thing that also produces same kind of output, but doesn't start the application is if forever is unable to write to the specified log file. I had a case where the log file had become too big and this prevented the process from starting.

Firstly change your package.json scripts like
"scripts": {
"start": "forever ./bin/www.js"
}
than start this command on linux console:sudo npm start
for windows just :npm start

I just ran into this today on an AWS Lightsail server, and NONE of the answers here or elsewhere had any effect. Everything worked fine until upgrading from NodeJS 10.x to 13.x. I tried removing and reinstalling forever, changing the permissions on the files and directories, etc, and I kept getting the EACCES error. The issue seemed to be that forever could not create directories within its .forever directory. The only thing that worked was to do the following:
1) Remove the .forever folder and all its subfolders and contents. For me, this was accomplished as follows:
sudo rm -rf /home/bitnami/.forever
2) Manually recreate the .forever folder:
sudo mkdir /home/bitnami/.forever
3) Manually set the permissions on the .forever folder:
sudo chmod -R o+rwx /home/bitnami/.forever
4) Manually recreate the .forever/pids folder:
sudo mkdir /home/bitnami/.forever/pids
5) Manually set the permissions on the .forever/pids folder:
sudo chmod -R o+rwx /home/bitnami/.forever/pids
6) Manually recreate the .forever/sock folder:
sudo mkdir /home/bitnami/.forever/sock
7) Manually set the permissions on the .forever/sock folder:
sudo chmod -R o+rwx /home/bitnami/.forever/sock
8) Run my NodeJS app via forever again with the sudo command.
9) List the processes forever is running, and verify that my app was there.
I'm not sure why I had to go through all of this, as setting the permissions recursively should have done the same thing, but after doing this, forever started running perfectly as it did before.
Hope this helps someone.

forever stopall
cd /<app-folder>
forever -w ./bin/www
This just worked for me in a Google Cloud Bitnami VM

If you setup a module globaly (-g option) "forever" is in the $path
forever start app.js
should work.

Related

forever is not working as expected after closing terminal or console

I have installed forever on shared hosting Cpanel for node js application when I run forever start app.js, node js application works on the server.
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: app.js
But when I close terminal or console then node app stopped working. Any suggestions around it?
Closing the terminal will typically close the application running. Consider using tmux or screen to launch the app or also nohup.
Launching this way should be considered a short-term solution. You probably want to look at how your specific Linux distribution handles startup scripts and services.
like maldina said closing forever will stop your app consider
consider upstart (it runs tasks when the computer is started)
you basiclly create a conf file and place it in your init folder "var/etc/init"
the file content should look like this
#!upstart
description "my-app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/server.js >> /var/log/myapp.log 2>&1
you can use the following commands to manage your app
sudo start my-app
sudo stop my-app
sudo restart my-app

how to run node js on dedicated server?

I am doing a chat app and integrating it on a website. When i execute teh command 'node index.js' on the local server everything works fine. But when i try installing node js on a dedicated server and try to execute the command 'nohup node index.js &' through ssh it gives following message.
nohup: ignoring input and appending output to `nohup.out'
I had followed the method mentioned in this site for installation of node js on server https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts
Can someone help me, please?
You first need to install Node in a correct way. I wrote a tutorial about it: How to get Node 6.7.0 on Linux (of course you can use newer versions, just change the version in the commands).
Basically it's something like this - change the version to the one you like:
# change dir to your home:
cd ~
# download the source:
curl -O https://nodejs.org/dist/v6.1.0/node-v6.1.0.tar.gz
# extract the archive:
tar xzvf node-v6.1.0.tar.gz
# go into the extracted dir:
cd node-v6.1.0
# configure for installation:
./configure --prefix=/opt/node-v6.1.0
# build and test:
make && make test
# install:
sudo make install
# make a symlink to that version:
sudo ln -svf /opt/node-v6.1.0 /opt/node
I recommend building Node from source and always running make test but you can also install a binary package which is faster - just make sure you understand the issues with paths and hashbang lines if you do so - more info on that and more install options are described in my tutorial.
Then you need to make sure that your application is started every time the server is restarted. I recommend using Upstart if you can.
Using Upstart, save something like this in /etc/init/YOURAPP.conf:
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [06]
# If the process quits unexpectadly trigger a respawn
respawn
# Start the process
exec start-stop-daemon --start --chuid node --make-pidfile --pidfile /www/YOURAPP/run/node-upstart.pid --exec /opt/node/bin/node -- /www/YOURAPP/app/app.js >> /www/YOURAPP/log/node-upstart.log 2>&1
Just change:
YOURAPP to the name of your own app
/opt/node/bin/node to your path to node
/www/YOURAPP/app/app.js to the path of your Node app
/www/YOURAPP/run to where you want your PID file
/www/YOURAPP/log to where you want your logs
--chuid node to --chuid OTHERUSER if you want it to run as a different user than node
(make sure to add a user with a name from --chuid above)
With your /etc/init/YOURAPP.conf in place you can safely restart your server and have your app still running, you can run:
start YOURAPP
restart YOURAPP
stop YOURAPP
to start, restart and stop your app - which would also happen automatically during the system boot or shutdown.
For more info see those answers about:
Installing Node
Running Node on servers
You can also use systemd for that but there are some differences as the system is much more complicated and often leads to some problems.

Keep meteor running on amazon EC2

I have a simple meteor app that I'm running on an Amazon EC2 server. Everything is working great. I start it manually with my user via meteor in the project directory.
However, what I would like is for this app to
Run on boot
Be immune to hangups
I try running it via nohup meteor &, but when I try to log out of the EC2 instance, I get the "You have running jobs" message. Continuing to log out stops the app.
How can I get the app to start on startup and stay up (unless it crashes for some reason)?
Install forever and use a start script.
$ npm install -g forever
I have several scripts for managing my production environment - the start script looks something like:
#!/bin/bash
forever stopall
export MAIL_URL=...
export MONGO_URL=...
export MONGO_OPLOG_URL=...
export PORT=3000
export ROOT_URL=...
forever start /home/ubuntu/apps/myapp/bundle/main.js
exit 0
Conveniently, it will also append to a log file in ~/.forever which will show any errors encountered while running your app. You can get the location of the log file and other stats about your app with:
$ forever list
To get your app to start on startup, you'd need to do something appropriate for your flavor of linux. You can maybe just put the start script in /etc/rc.local. For ubuntu see this question.
Also note you really should be bundling your app if using it in production. See this comparison for more details on the differences.
I am using upstart on Ubuntu server which you should be able to easily install on Amazon linux.
This is roughly my /etc/init/myapp.conf:
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/home/deploy"
export NODE_ENV="production"
export MONGO_URL="mongodb://localhost:27017/myappdb"
export ROOT_URL=http://localhost
export MAIL_URL=smtp://localhost:25
export METEOR_SETTINGS='{"somesetting":true}'
cd /var/www/myapp/bundle/
exec sudo -u deploy PORT=3000 /usr/bin/node main.js >> /var/log/node.log 2>&1
end script
I can then manually start and stop myapp like this:
sudo start myapp
sudo stop myapp
I believe this package solves your problem: https://github.com/arunoda/meteor-up
which seems to use forever: https://github.com/nodejitsu/forever

Bash script for Ghost blog not starting up on server reboot

I have a very simple bash script which should launch my ghost blog. I am using crontab to launch the script on startup, here is the crontab command I am running:
#reboot /var/www/ghost/launch.sh
The script has the following code:
#!/bin/sh
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? != 0 ]
then
NODE_ENV=production forever start --sourceDir /var/www/ghost index.js
fi
When I sudo reboot the server, and use forever list to find the processes running, I see the following:
data: [0] sHyo /usr/bin/nodejs index.js 1299 1314 /home/webadmin/.forever/sHyo.log 0:0:1:25.957
When I nano to that log file, the log says the following:
^[[31m
ERROR:^[[39m ^[[31mCould not locate a configuration file.^[[39m
^[[37m/home/webadmin^[[39m
^[[32mPlease check your deployment for config.js or config.example.js.^[[39m
Error: Could not locate a configuration file.
at checkTemplate (/var/www/ghost/core/config-loader.js:16:36)
at Object.cb [as oncomplete] (fs.js:168:19)
error: Forever detected script was killed by signal: null
It appears to be looking in /home/webadmin/, but ghost is installed at /var/www/ghost????
When I run the exact same script in the terminal manually after the sever has started up by ssh-ing into the server, the script works fine. I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine. The log for that forever process says the following:
^[[32mGhost is running...^[[39m
Your blog is now available on http://blog.example.com ^[[90m
Ctrl+C to shut down^[[39m
What is wrong with my script or crontab that it cannot launch the script properly?
I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine.
That's the thing, your cron job is not doing the same:
#reboot /var/www/ghost/launch.sh
This script is executed from your home directory. One way to fix is to change your crontab:
#reboot cd /var/www/ghost; ./launch.sh
Another way is to add this line near the top of launch.sh, anywhere before launching forever:
# change to the directory of this script
cd $(dirname "$0")
Just an FYI for anybody that runs across this I would highly suggest looking into pm2 to start Ghost and to monitor Ghost. It will monitor Ghost like Forever and has a built in feature to generate a init script to start pm2 when your server restarts. Also has better features to monitor Ghost while it is running. Check out my how to here.

Setting Up Node.js on Webfaction

What are the steps required for setting up a Node.js application on Webfaction shared hosting account?
Introduction
The result of the following instructions is a Node.js application that is running continously, produces logfiles and restarts itself even after the reboot of the server machine. The instructions are especially targeted for the shared host accounts on Webfaction but can be used for general purpose also.
Install Node.js
Eventhough Node.js download page offers Linux binaries, it would be more robust to install Node.js from source. Download the most recent source codes and extract them to ~/src/.
(log in to your webfaction console)
> mkdir -p ~/src/
> cd src/
> wget http://nodejs.org/dist/v0.10.18/node-v0.10.18.tar.gz
> tar -xzf node-v0.10.18.tar.gz
The next step is to compile and install the code. For this Python is used. Because the default python version is 2.4 (at least on my Webfaction server web223) and because the Node.js installation requires one of the versions 2.6+, you must temporarily set newer version to be the default. See the following snippet (also see this article for details). Note --prefix=$HOME which is required due Webfaction's environment restrictions (you have access only to your home directory).
> cd node-v0.10.18/
> alias python=python2.7
> python configure --prefix=$HOME
> make
> make install
Node.js installed. Verify the success by
> node -v
v0.10.18
That also installed node package manager npm.
> npm -v
1.3.8
Install Forever
To keep run Node.js application running as long as possible and logging the output for maintenance, you need Forever. For convenience, install it globally (for you) using flag -g.
> cd ~
> npm install -g forever
> forever --version
v0.10.8
You can later update forever by
> npm update -g forever
Start the Application
First, create a custom application via Webfaction Control Panel (Custom app (listening on port)). Name it for example foonode. You may also create a domain and website for the app.
Second, make a note about the socket port number that was given for the app by Webfaction. Let the example be 48888.
Third, copy your application code to the directory of the app i.e. ~/webapps/foonode/. After that the directory contents would for example be the following.
> cd ~/webapps/foonode/
> ls
node_modules/
index.js
Index.js would be something like the snippet below. The key point is using the given port number for the app.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Universe.\n');
}).listen(48888, '127.0.0.1');
console.log('Listening at http://127.0.0.1:48888/');
Now you could run the app by node index.js. However there is a problem: when you exit the shell, also the app exits. Therefore you should run the Node.js as a daemon. That is something where Forever does a lot for you. Start node with forever:
> cd ~/webapps/foonode/
> forever start index.js
Now the app continues running after you logout and, as a very nice thing to have, is quickly restarted if it happens to crash. This is a lot but still not enough. What if the server reboots? Where is the output from the app going? What if the script crashes continously? What if you have multiple scripts with the name index.js? Keep reading.
Use Absolute Paths with Forever
Forever identifies the process by the filename of the script fed to forever start <filename_of_script>. A problem arises when you have multiple applications with the same filename for the script.
For example consider you have two applications /home/foouser/webapps/foonode/index.js and /home/foouser/webapps/barnode/index.js. If you now start both with forever start index.js within the directories of the applications and then run forever stop index.js only once, the result is that both applications become stopped. This happens because they both had the same identity index.js
The solution is to use the absolute filepaths when specifying the script.
> forever start /home/foouser/webapps/foonode/index.js
And to stop execution:
> forever stop /home/foouser/webapps/foonode/index.js
This ensures that only the intended application becomes stopped. This habit of using absolute paths has also positive effects to forever list. See this issue for details.
Logging
To store the application output to somewhere, specify the following arguments.
-l <logfile> = stream all output to this file. Relative to ~/.forever/
-o <logfile> = stream script's stdout to this file. Relative to current dir.
-e <logfile> = stream script's stderr to this file. Relative to current dir.
-a = append to the files instead of overwriting them.
It seems convenient to have a subdirectory for the logs, e.g. logs/. With the arguments, absolute path and the subdirectory logs/ the command becomes the following:
> cd ~/webapps/foonode/
> mkdir -p logs/
> forever start -a -l forever.log -o logs/out.log -e logs/err.log /home/foouser/webapps/foonode/index.js
Detecting and Restarting a Spinning Process
Forever has the parameters --minUptime and --spinSleepTime which are not so well documented currently. The meaning of the parameters is to control situation where the script crashes and almost immediately crashes again after restart. As forever tries to restart the script as soon as possible after the crash, this can lead to a busy loop which may eat lots of resources of the server.
--minUptime specifies the number of milliseconds the script has to be up and running without crashes to be restarted immediately. If the uptime of the crashed script is less than minUptime, then the script is considered spinning i.e. problematic. If uptime is greater then the script is considered non-spinning.
If a spinning script crashes i.e. the uptime of the script is less than --minUptime then forever waits --spinSleepTime number of milliseconds before restarting the script. Otherwise the script is restarted as soon as possible. This prevents the resource-eating loop. See this answer for futher discussion.
I personally use 5000 for --minUptime to make sure that Node is fully started before declaring it non-spinning. 2000 would be a good one for --spinSleepTime to avoid the loop but still trying to start the script quickly after the problematic situation resolves.
> forever start -a -l forever.log -o logs/out.log -e logs/err.log --minUptime=5000 --spinSleepTime=2000 /home/foouser/webapps/foonode/index.js
Manage the Application
As the commands grow, memorizing and writing them becomes more and more cumbersome. Therefore it is convenient to copy them into a Makefile. The makefile also becomes handy later on when you need to specify a command to run after the server reboot.
> cd ~/webapps/foonode/
> cat Makefile
foreverstart:
# Run server forever (until reboot)
mkdir -p logs
forever start -a -l forever.log -o logs/out.log -e logs/err.log --minUptime 5000 --spinSleepTime 2000 /home/foouser/webapps/foonode/index.js
foreverstop:
forever stop /home/foouser/webapps/foonode/index.js
Keeping the Application Running
Forever does not cover the case where the server reboots. For that you need to specify a cronjob with #reboot rule that start the forever after reboot.
> export EDITOR=nano # Editor to use to edit crontab. A matter of taste.
> crontab -e
Add the following line and save.
#reboot make -C ~/webapps/foonode/ -f ~/webapps/foonode/Makefile foreverstart
The line ensures that foonode is started immediately after the server reboot. The flag -C specifies the directory to run the makefile from and -f the actual location of the makefile. See this answer for details about using #reboot.
Conclusion
Now you have a node process running truly forever or at least as long as there is maintenance guys feeding the server with electricity. Lots of things done but maybe more will come. Things you may like to do in the future which were not covered here includes the following.
Watching for file changes and automatically restarting when detected (See --watch in forever --help)
Using Grunt.js instead of Make (See bustardcelly/grunt-forever)
Backupping the application once in a while.
See also
Setting up Redis on Webfaction
Any ideas, comments or corrections?
Well, you probably already followed all the steps in the other answer (wow! such detail! great), but it is now a lot easier: they now have a one-click installer for node.js.
Another option is to install nvm (Node Version Manager) and type:
nvm install node
It's not only valid for Webfaction.

Resources