Unable to restart rogue Jenkins on Ubuntu - linux

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

Related

Using Huey with Django: How to keep run_huey command running?

I'm using Django on Ubuntu 18.04.
I've got everything set up. And I type python manage.py run_huey in the server (through an SSH connection) to start huey, and it works.
However this is done through the command line through SSH and it will shut off when I close the SSH connection.
How do I keep run_huey running so that it will stay active at all times? Furthermore, after a system reboot, how do I get run_huey to automatically start?
You may explore supervisorctl utility for ubuntu, it keeps process running and can log into file and any other features. Google it.

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

DigitalOcean stop Node.js server running with nohup

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>>.

how to automatically restart a node server?

We are finishing development of a project, the client is already using it but occasionally some errors occur - crashing the server.
I know I could register a service as 'upstart' script on linux, in order to have my node service restart when it crashes.
But our server is running other stuff, so we can't restart it.
Well, actually, while writing, I realize I have two questions then:
Will 'upstart' work without having to reboot? Something is just whispering yes to me :)
If not, what other option would I have to 'respawn' my node server when it crashes?
Yes, upstart will restart your process without a reboot.
Also, you should look into forever.
PM2 is a Production process manager for Node.js app.
If your focus for automatic restart is an always running application, I suggest to use a process manager. Process manager, in general, handles the node process(es if cluster enabled), and is responsible for the process/es execution. PM leans on the operative system: your node app and the OS are not so strinctly chained because the pm is in the middle.Final trick: put the process manager on upstart. Here is a complete performance improvement path to follow.
Using a shared server and not having root privileges, I can't download or install any of the previously mentioned libraries. What I am able to do is use a simple infinite bash loop to solve my issue. First, I created the file ./startup.sh in the base directory ($ vim startup.sh):
#!/bin/bash
while:
do
node ./dist/sophisticatedPrimate/server/main.js
done
Then I run it with:
$ bash startup.sh
and it works fine. There is a downside to this, which is that is doesn't have a graceful way to end the loop (at least not once I exit the server). What I ended up doing is simply finding the process with:
$ ps aux | grep startup.sh
Then killing it with
$ kill <process id>
example
$ kill 555555

How do I run a Node.js application as its own process?

What is the best way to deploy Node.js?
I have a Dreamhost VPS (that's what they call a VM), and I have been able to install Node.js and set up a proxy. This works great as long as I keep the SSH connection that I started node with open.
2016 answer: nearly every Linux distribution comes with systemd, which means forever, monit, PM2, etc. are no longer necessary - your OS already handles these tasks.
Make a myapp.service file (replacing 'myapp' with your app's name, obviously):
[Unit]
Description=My app
[Service]
ExecStart=/var/www/myapp/app.js
Restart=always
User=nobody
# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/myapp
[Install]
WantedBy=multi-user.target
Note if you're new to Unix: /var/www/myapp/app.js should have #!/usr/bin/env node on the very first line and have the executable mode turned on chmod +x myapp.js.
Copy your service file into the /etc/systemd/system folder.
Tell systemd about the new service with systemctl daemon-reload.
Start it with systemctl start myapp.
Enable it to run on boot with systemctl enable myapp.
See logs with journalctl -u myapp
This is taken from How we deploy node apps on Linux, 2018 edition, which also includes commands to generate an AWS/DigitalOcean/Azure CloudConfig to build Linux/node servers (including the .service file).
Use Forever. It runs Node.js programs in separate processes and restarts them if any dies.
Usage:
forever start example.js to start a process.
forever list to see list of all processes started by forever
forever stop example.js to stop the process, or forever stop 0 to stop the process with index 0 (as shown by forever list).
I've written about my deployment method here: Deploying node.js apps
In short:
Use git post-receive hook
Jake for the build tool
Upstart as a service wrapper for node
Monit to monitor and restart applications it they go down
nginx to route requests to different applications on the same server
pm2 does the tricks.
Features are: Monitoring, hot code reload, built-in load balancer, automatic startup script, and resurrect/dump processes.
You can use monit, forever, upstart or systemd to start your server.
You can use Varnish or HAProxy instead of Nginx (Nginx is known not to work with websockets).
As a quick and dirty solution you can use nohup node your_app.js & to prevent your app terminating with your server, but forever, monit and other proposed solutions are better.
I made an Upstart script currently used for my apps:
description "YOUR APP NAME"
author "Capy - http://ecapy.com"
env LOG_FILE=/var/log/node/miapp.log
env APP_DIR=/var/node/miapp
env APP=app.js
env PID_NAME=miapp.pid
env USER=www-data
env GROUP=www-data
env POST_START_MESSAGE_TO_LOG="miapp HAS BEEN STARTED."
env NODE_BIN=/usr/local/bin/node
env PID_PATH=/var/opt/node/run
env SERVER_ENV="production"
######################################################
start on runlevel [2345]
stop on runlevel [016]
respawn
respawn limit 99 5
pre-start script
mkdir -p $PID_PATH
mkdir -p /var/log/node
end script
script
export NODE_ENV=$SERVER_ENV
exec start-stop-daemon --start --chuid $USER:$GROUP --make-pidfile --pidfile $PID_PATH/$PID_NAME --chdir $APP_DIR --exec $NODE_BIN -- $APP >> $LOG_FILE 2>&1
end script
post-start script
echo $POST_START_MESSAGE_TO_LOG >> $LOG_FILE
end script
Customize all before #########, create a file in /etc/init/your-service.conf and paste it there.
Then you can:
start your-service
stop your-service
restart your-service
status your-service
I've written a pretty comprehensive guide to deploying Node.js, with example files:
Tutorial: How to Deploy Node.js Applications, With Examples
It covers things like http-proxy, SSL and Socket.IO.
Here's a longer article on solving this problem with systemd: http://savanne.be/articles/deploying-node-js-with-systemd/
Some things to keep in mind:
Who will start your process monitoring? Forever is a great tool, but it needs a monitoring tool to keep itself running. That's a bit silly, why not just use your init system?
Can you adequately monitor your processes?
Are you running multiple backends? If so, do you have provisions in place to prevent any of them from bringing down the others in terms of resource usage?
Will the service be needed all the time? If not, consider socket activation (see the article).
All of these things are easily done with systemd.
If you have root access you would better set up a daemon so that it runs safe and sound in the background. You can read how to do just that for Debian and Ubuntu in blog post Run Node.js as a Service on Ubuntu.
Forever will do the trick.
#Kevin: You should be able to kill processes fine. I would double check the documentation a bit. If you can reproduce the error it would be great to post it as an issue on GitHub.
Try this: http://www.technology-ebay.de/the-teams/mobile-de/blog/deploying-node-applications-with-capistrano-github-nginx-and-upstart.html
A great and detailed guide for deploying Node.js apps with Capistrano, Upstart and Nginx
As Box9 said, Forever is a good choice for production code. But it is also possible to keep a process going even if the SSH connection is closed from the client.
While not necessarily a good idea for production, this is very handy when in the middle of long debug sessions, or to follow the console output of lengthy processes, or whenever is useful to disconnect your SSH connection, but keep the terminal alive in the server to reconnect later (like starting the Node.js application at home and reconnecting to the console later at work to check how things are going).
Assuming that your server is a *nix box, you can use the screen command from the shell to do keep the process running even if the client SSH is closed. You can download/install screen from the web if not already installed (look for a package for your distribution if Linux, or use MacPorts if OS X).
It works as following:
When you first open the SSH connection, type 'screen' - this will start your screen session.
Start working as normal (i.e. start your Node.js application)
When you are done, close your terminal. Your server process(es) will continue running.
To reconnect to your console, ssh back to the server, login, and enter 'screen -r' to reconnect. Your old console context will pop back ready for you to resume using it.
To exit screen, while connected to the server, type 'exit' on the console prompt - that will drop you onto the regular shell.
You can have multiple screen sessions running concurrently like this if you need, and you can connect to any of it from any client. Read the documentation online for all the options.
Forever is a good option for keeping apps running (and it's npm installable as a module which is nice).
But for more serious 'deployment' -- things like remote management of deploying, restarting, running commands etc -- I would use capistrano with the node extension.
https://github.com/loopj/capistrano-node-deploy
https://paastor.com is a relatively new service that does the deploy for you, to a VPS or other server. There is a CLI to push code. Paastor has a free tier, at least it did at the time of posting this.
In your case you may use the upstart daemon. For a complete deployment solution, I may suggest capistrano. Two useful guides are How to setup Node.js env and How to deploy via capistrano + upstart.
Try node-deploy-server. It is a complex toolset for deploying an application onto your private servers. It is written in Node.js and uses npm for installation.

Resources