DigitalOcean stop Node.js server running with nohup - node.js

I am running a Node.js server on a DigitalOcean droplet (with Ubuntu). I have worked out how to make it run when I'm not connected to it via Putty. However, just one issue: how do I stop it now?
I can see that control+C works when in the session, but what if I exit the session and come back? How will I stop the server then?
Also, will running it multiple times run multiple servers at once?
Thanks!

You really should be using a tool like supervisord (http://supervisord.org/) for your long-running processes.
But if you want to stop an already running process that you started with nohup then look up the process ID first (with ps aux and look/grep for your process) and then run kill <<pid>>.

Related

automatically start node server on instance start in aws autoscaling by providing user data

I have a demo project in AWS and then I created an AMI for it so that I can use it for auto-scaling. now I am looking for something that I can put in user text in my launch configuration which will let me start the server without going to ssh. I am trying out below, let me know where is my mistake.
#!/bin/bash
cd demo
node server.js
when I launch a new instance with my AMI and just do cd through SSH it works absolutely fine, however, I want to start the server with going to SSH.
These are common one can face when running node application without process manager on a remote server.
Let suppose the above script but what if a node application encounter error? so the application will be stopped, so better to use process manager which will take care of such thing and you will not need to do ssh.
You can use pm2. Which also have slack integration another interesting feature that will help to monitor the process.
You can also set Setup startup script.
Restarting PM2 with the processes you manage on server boot/reboot is
critical. To solve this, just run this command to generate an active
startup script:
run these command in the AMI, and pm2 will take care of the process on all instances.
pm2 startup
#And to freeze a process list for automatic respawn:
pm2 save

Start node app through ssh, stays running?

I have a raspberry PI on which I run a node server. To start and control the terminal on which the server runs I use desktop remote to remote control the raspberry. Now this method is really slow so I was wondering, since I only need a command line anyway if I couldn't just connect to my raspberry pi using ssh for example.
My question now, would be if I do so, can I navigate to my node folder, run my node file and then close the ssh connection? Will my Node server keep running and if so how would I access the terminal with the node session after closing the connection?
The easiest way to do this is something like:
nohup node myapp.js &
This will make the app run in the background, and nohup prevents it from stopping when the connection closes.
This is a cheap and quick way to do this. A more appropriate way might be one of the following:
Using something like docker to manage running applications.
Using something like supervisord to do the same thing.
Writing scripts for initd and turn it into a real 'service'.
Changing the node application to fork & deamonize itself.

How to stop a node program while in another command window

I have an ubuntu server running with 2 node websites on it and I keep closing the terminals that start the app so I cannot shut the app down to make changes, I end up restarting the server.
How can I see what node instances are running and then stop them, or how do I stop a node instance through programming and I'll just make a button that kills the node instance. Either way...
If you dont want to use PM2 or systemd you can get the list of all the node.js instances running using
ps -aux | grep node
once you have the list of all nodejs processes you can kill any process using kill command
kill "Process ID goes here"
Use a process manager. PM2 is the most widely recommended.
npm install pm2#latest -g
then
pm2 start app.js
Alternatively you can use something like screen to have multiple terminal sessions running in one window

Unable to restart rogue Jenkins on Ubuntu

I was configuring Jenkins last night to run some reporting plugins (codestyle, findbugs, cobertura). When I ran my build job it got hung up somewhere in codestyle, and the server ui became unresponsive.
Today I logged in to the server and the Jenkins log is reporting errors that look like the server ran out of memory, but more than that, I cannot seem to stop or restart the server. I have limited experience with services in linux.
Jenkins was installed on Ubuntu with atp. I have tried $ sudo /etc/init.d/jenkins restart but it reports
* Starting Jenkins Continuous Integration Server jenkins
The selected http port (8080) seems to be in use by another program
Please select another port to use for jenkins
When I try to run service jenkins status to get a pid to kill i get
2 instances of jenkins are running at the moment
but the pidfile /var/run/jenkins/jenkins.pid is missing
Running netstat and ps has identified the port being held by a jenkins instance.
How can I recover from this?
Mostly I was concerned about abruptly killing the Jenkins server while it has gone rogue. Something this tied into process with server connections and plugins makes me wary of taking a shotgun to the process.
That's exactly what I did. server jenkins status didn't work, so I got the process id from netstat -tulpn. kill -15 didn't work so I did kill -9, waited a respectful grieving period, then restarted the Jenkins service.
I will next be investigating the root problem of running out of memory in my Jenkins installation so hopefully this doesn't happen again while I am firewalled away from my server.
Where is your server hosted?
I had the same issue with AWS EC2 server.
Command lines did not work to reboot the server.
However, on AWS admin console, I did: EC2 -> restart and it works like a charm.
This may not be a solution but a workaround.
I was able to do
sudo ps aux | grep jenkins
To find a list of jenkins processes. Then I ran
sudo kill <pid>
And then finally
sudo service jenkins restart

Node.js on Amazon EC2 faster development, have to kill PID everytime i change something

i am developing on a amazon EC2 server, running node.js with express.
I run the server by doing a sudo node server.js and its fune, i can see the requests coming in etc. But when i change something and reupload, a send a ctrl+Z a SIGTERM.
This gives me back the session, but it does not completly shut down the server, when i try to run it again by doing sudo node server.js it tells me the port is in use, so i have to do a
ps aux | grep node
Get process id and
kill -9 PRCOCESS_ID
This gets quite annoying in the long run when developing. Any faster way?
Try nodemon: it will automatically restart the server after changes. https://github.com/remy/nodemon
I picked up on that from this Medium post, it has some other good Node tips in it.

Resources