Run script in userdata Detached - linux

I am trying to start and run a long running process on my ec2 instance via userdata.
This process needs to stay running for the lifespan of the instance.
This process also needs to run as another user i.e centos and not root.
I am able to run the script and it works fine but it will not allow me to ssh into the server until I stop/start the instance.
Here is how I am running the process now.
I get the error Connection refused
/bin/su -c "/home/centos/downloadAndStart.sh" - centos /dev/null &/dev/null &
without running the script I am able to ssh into the server as soon as it initializes.
I believe this is because of something like the process is still attached to the PID of the userdata script? Does that make sense?
I am not entirely sure why I cannot ssh into the instance until after I restart the instance.

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

Geddy CLI closes on SSH drop

In a remote CentOS VM Geddy application with MonogoDB wrapper is deployed. The application starts and listen to port 80 when below command is executed.
geddy -e production &
The problem in this CLI command is when the SSH connection to VM was disconnected the process automatically gets closed. To make application working SSH needs to be opened always which is not possible. Is there any alternative method to keep it running as background service.
This happens because processes that are merely backgrounded will be sent a SIGHUP signal when their controlling terminal (the SSH connection) is closed.
The traditional method of preventing this is using the nohup utility:
nohup geddy -e production &
Alternatively, you can use terminal multiplexers like screen or tmux to create persistent terminal sessions (ones that remain active when you log out, and that can be reattached when you log in again at a later time).

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

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

Resources