How do I shutdown a node project started by Forever? ungit - node.js

I'm using the open source project called Ungit. It is based on node.js and I really enjoy it but I don't know how to shut it off other than rebooting the whole box.
Ungit is using "forever-monitor" to have a process run forever. So when I do nohup ungit > /dev/null & I can't shut it down by killing it's process as it will restart it.
I have looked up forever-monitor project and I was able to install it globally and try to shut it by running forever stop [number] but forever list shows nothing and I don't know how to shut it down.
Thanks for any advices,

with forever list you can see list of forever process
with forever stop 0 you can stop first process .
with forever stop myscript.js you can stop a process with name of script

find the process that forever-monitor is running under and kill it first then kill the ungit process.
http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/

Related

Does supervisord kill running processes on with the restart command?

We have supervisord running in production and I'd like to know if after running $ supervisorctl restart group-name:* if running processes are killed immediately or if supervisor lets running processes finish.
Tried my best to find that out in the docs and the source code.
As far as I know, supervisorctl will kill process, because usually under supervisor work worker script which never stops, hence it is no way to supervisorctl understand when script is ready to be stopped.

What is forever in nodejs?

I am having hard time understanding what is forever in nodejs.
Can someone explain what is forever in simplest way that i can understand and what is the purpose of it
forever is a node.js package that is used to keep the server alive even when the server crash/stops. When the node server is stopped because of some error, exception, etc. forever automatically restarts it
From the npmjs https://www.npmjs.com/package/forever
A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever)
Forever can be used as
forever start app.js
It provides many useful functions which you can see in the link above.
Directly quoting from http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/
The purpose of Forever is to keep a child process (such as your node.js web server) running continuously and automatically restart it when it exits unexpectedly.
Forever basically allows you to run your nodejs application as a process.
Without forever you might type npm start or node index.js to start your application and it will run in your terminal session.
With forever, you can start it off and still have access to your terminal session/close it.
You can even use the nohup command it simply ignores the hang up signal.
node index.js && nohup -& (to run as a background process - no hiccup)

keep server alive after closing command prompt (with forever or forever-monitor)

I'm using Amazon WS to test some rudimentary nodejs server. The problem I'm having is that when I close the putty command prompt on my PC, that I can not reach the server anymore with a browser.
Ive read about forever and forever-monitor. I'n not sure why the script must be restarted constantly, but ok let's assume it must.
I'm using both
forever "/home/ec2-user/myApp.js"
and
node "/home/ec2-user/foreverMonitor.js"
(The latter has the myApp.js reference in the foreverMonitor.js file. Similar to Where place forever-monitor code?.)
Both do start the server, but when I close putty, both also let the server die.
What am I missing here?
------------------------------------- update -------------------------------------
I guess I can also skip foreverMonitor (not verified yet)
nohup forever "/home/ec2-user/myApp.js" &
forever stop "/home/ec2-user/myApp.js"
------------------------------------- update -------------------------------------
working, now using this
nohup forever "/home/ec2-user/foreverMonitor.js" &
forever stop "/home/ec2-user/foreverMonitor.js"
I'm not totally familiar with AWS, but it seems that you probably need to run nohup. The trailing ampersand should give you control of the terminal again immediately after executing the command.
$ nohup forever "/home/ec2-user/myApp.js" &
$ nohup node "/home/ec2-user/foreverMonitor.js" &
See this answer for more details on nohup and the trailing ampersand: https://stackoverflow.com/a/15595391/498624
Have a look at PM2 https://github.com/Unitech/pm2
After using forever successfully, I switched to PM2.
forever works fine but I found PM2 was a better fit to my mental model. PM2 also has a very neat (and repidly evolving) Web interface where you can monitor and control node instances. As a bonus you can also run non-node tasks under PM2

Cannot get my Upstart script to run Node.js and Forever when server restarts

I've been setting up my server recently and today I had to restart it... then I realised all of my Node apps I had running weren't running anymore. I'm using Node Forever module to keep the apps running, but then I realised I still need to have them starting when my server restarts or shut downs and powers up again.
I have been researching the best way to do this, but what I'm trying just doesn't seem to work. I've created an Upstart script in my /etc/init/ folder on my Ubuntu Server 10.04LTS remote server and tried restarting and it doesn't seem to do anything. Nothing is getting listed when I run forever list.
Here is my current Upstart script I was trying out today:
#/etc/init/myapp.conf
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
script
exec sudo /usr/local/bin/node /var/www/myapp/myapp.forever.js
end script
I use Forever in a Node script as I find it easier to configure it how I want. It's confirmed that the script runs just fine if I do this outside the script, there is just something wrong with the Upstart script itself. It seems to have the same permissions as all the other Upstart scripts in /etc/init/ folder.
As an additional note, I have gone through almost all the answers I could find here on StackOverflow, and that it how I got together the script that I have at present.
UPDATE:
With Tom's answer, I have now tried:
#/etc/init/myapp.conf
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
exec sudo /usr/local/bin/node /var/www/myapp/myapp.forever.js
But it's still not working.
So I don't know why this isn't running when I restart my server. Please help!
This is not a very happy setup. The way upstart works is that it starts your process running it using the process id for the start command. Forever JS works similarly, it is probably inspired by Upstart.
When you try to run forever.js with upstart, the forever process you create in your upstart script exits immediately after starting. Upstart counts on having the process continue to run.
When I tried to run forever using upstart, I wound up with five different forever process running because upstart thought it had failed to start forever, and it retried five times.
Did you try doing it without the start script lines?
description "my server"
author "name"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
#respawn if you were not using forever
exec sudo /usr/local/bin/node myapp.forever.js
Source: http://caolanmcmahon.com/posts/deploying_node_js_with_upstart
I've opted to use an #reboot statement in the user's crontab file, which will execute forever on server restarts.
#reboot forever start app.js
Additional Reading - http://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/

How to run node.js app forever when console is closed?

I connect to my remote server via ssh. Then I start my node.js app with Forever. Everything works fine until I close my console window. How to run node.js app FOREVER on my remote server even when I close my connection via ssh? I just want to start an app and shut down my copmputer. My app should be working in the background on my remote server.
You may also want to consider using the upstart utility. It will allow you to start, stop and restart you node application like a service. Upstart can configured to automatically restart your application if it crashes.
Install upstart:
sudo apt-get install upstart
Create a simple script for your application that will look something like:
#!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/app.js >> /var/log/myapp.log 2>&1
Then copy the script file (myapp.conf) to /etc/init and make sure its marked as executable. Your application can then be managed using the following commands:
sudo start myapp
sudo stop myapp
sudo restart myapp
Two answers: One for Windows, one for *nix:
On Windows, you can use the start command to start the process disconnected from your instance of cmd.exe:
start node example.js
On *nix, there are two aspects of this: Disconnecting the process from the console, and making sure it doesn't receive the HUP signal ("hang up"), which most processes (including Node) will respond to by terminating. The former is possibly optional, but the latter is necessary.
Starting disconnected from the console is easy: Usually, you just put an ampersand (&) at the end of the command line:
# Keep reading, don't just grab this and use it
node example.js &
But the above doesn't protect the process from HUP signals. The program may or may not receive HUP when you close the shell (console), depending on a shell option called huponexit. If huponexit is true, the process will receive HUP when the shell exits and will presumably terminate.
huponexit defaults to false on the various Linux variants I've used, and in fact I happily used the above for years until coderjoe and others helped me understand (in a very long comment stream under the answer that may have since been deleted) that I was relying on huponexit being false.
To avoid the possibility that huponexit might be true in your environment, explicitly use nohup. nohup runs the process immune from HUP signals. You use it like this:
nohup node example.js > /dev/null &
or
nohup node example.js > your-desired-filename-or-stream-here &
The redirection is important; if you don't do it, you'll end up with a nohup.out file containing the output from stdout and stderr. (By default, nohup redirects stderr to stdout, and if stdout is outputting to a terminal, it redirects that to nohup.out. nohup also redirects stdin if it's receiving from a terminal, so we don't have to do that. See man nohup or info coreutils 'nohup invocation' for details.)
In general for these things, you want to use a process monitor so that if the process crashes for some reason, the monitor restarts it, but the above does work for simple cases.
I would definitely recommend pm2
npm install -g pm2
To start server: pm2 start [yourServerFile.js]
To stop server: pm2 stop [yourServerFile.js]
Close client and server will run forever....will also restart if app crashes.
Ive been running a node server on Ubuntu for months with zero issues
Always, simple is the best, no need upstart, no need forever, just nohup:
nohup node file.js &
Believe me, I'm running so that for my case!
You could install forever using npm like this:
sudo npm install -g forever
Or as a service:
forever start server.js
Or stop service
forever stop server.js
To list all running processes:
forever list
node expamle.js & for example
In Linux, SSH into your remote server and run
screen
to launch into a new screen.
Finally, type ctrlad to detach the screen session without killing the process.
More info here.
I had similar issue and I think using forever will help to handle crashed and restarts
You can install forever globally:
sudo nom install -g forever
And run this command:
nohup forever server.js &
This should handle all the trouble of closing the terminal, closing ssh session, node crashes and restarts.
If you're running node.js in a production environment, you should consider using PM2, forever.js, or Nodemon.
There is no shortage of articles online comparing the different packages.
This is only a partial answer for Windows. I’ve created a single line Visual Basic Script called app.vbs that will start your node application within a hidden window:
CreateObject("Wscript.Shell").Run "node app.js", 0
To execute it automatically at startup, open the %AppData%\Microsoft\Windows\Start Menu\Programs\Startup\ directory and add a shortcut to the app.vbs file.
More info at: https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
Wow, I just found a very simple solution:
First, start your process (node app)
forever dist/index.js
run: ^Z cmd + z.
Then: bg. Yeah.. bg (background).
And pum.. you are out.
Finish with exitif you are with sshor just close the terminal.
my start.sh file:
#/bin/bash
nohup forever -c php artisan your:command >>storage/logs/yourcommand.log 2>&1 &
There is one important thing only. FIRST COMMAND MUST BE "nohup", second command must be "forever" and "-c" parameter is forever's param, "2>&1 &" area is for "nohup". After running this line then you can logout from your terminal, relogin and run "forever restartall" voilaa... You can restart and you can be sure that if script halts then forever will restart it.
I <3 forever

Resources