What is the command/configuration to run NodeJS/Express app as a daemon? - mean-stack

In SSH if I run:
$ node server
My web app works fine until I close the SSH session. What is the command or configuration needed so that it runs all the time without an active SSH session?

you can run it like:
nohup node server
Or you can install forever.
npm -g install forever
forever start server

If you have multiple nodejs, you can use pm2 to manage them.
Or, use screen or tmux to keep the nodejs running while detach the session.

Related

Weird error when deploying with pm2 and WSL2

I have a deploy.sh script which looks like this:
ssh -t <name> -<port> 'cd <foldername>; git pull; yarn build <appname>; pm2 restart <appname> --update-env'
It works perfectly when I'm using Windows Powershell but for some reason, it fails if I use my WSL terminal. It completes all stages of the script up until pm2 restart part, at this point error: unknown option --update-env appears, ssh connection closes and I have to manually connect to ssh server and restart pm2.
I'm not sure if there is a problem with my wsl or with pm2 but since '--update-env' is a pm2 option I guess it is pm2.
I'm using pm2 version 4.5.6
PSVersion 5.1.19041.1237
WSL2 Ubuntu-20.04.2 LTS

Running a GUI application on a CI service without X11

I have a GUI application that I would like to set up testing for via GitHub Actions. I already have it set up so that GitHub Actions compiles the application on Ubuntu, but now what I would like to do is run the application for a few seconds and test if it crashes or not. However, currently it fails to start because there is no X11 server installed.
Is there a way that I can install a dummy X11 server, so that the application runs? I don't care about what is actually displayed, I just want the application to be able to open without failing due to the X11 server missing.
Alternatively, is there a way to install a dummy Wayland server? This app can also run on Wayland.
I use this command:
linux:
runs-on: ubuntu-latest
steps:
run: |
export DISPLAY=:99
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 &
mycommand
You can try xvfb-run from Xvfb project. It starts your application(s) under fully compatible X Window server w/o any hardware (you can even run x11vnc among your apps and connect to the server over VNC, but I believe it's not your case for now). Personally I use xvfb-run for isolated screenless X.org-aware packages build, when, e. g., a package needs to take a snapshot of itself while making documentation.
$ xvfb-run x.org_application_binary
Yes, that is possible. Simply create a Docker Image with your X11 environment and deploy your application in it.
Alternatively, you can also just install X11 on your machine. Make sure to do it in every run, as the environments always fully reset:
sudo apt-get install xorg openbox

how do you quit docker-compose up # macOS?

after docker-compose up, on windows i quit that with "CTRL+C" and the container(s) are still running. When i do this on my mac, then docker kills my container :(
I tried it now with a shell script, which executes the docker-compose up into "echo", but on some container, the scripts hang up.
How do you do that? Are there any best practices? (suitable with windows)
Thanks a lot.
If you want to run docker-compose up and leave the process running without being attached to your terminal, you can run it in detached mode with docker-compose up -d.
https://docs.docker.com/compose/reference/up/
After doing so, you'd have to use docker-compose stop or docker-compose down to stop your running containers, since CTRL+C won't kill them.

bash process running in the background

I'm connecting to a remote server running Ubuntu 16.04 using ssh. I'm running nodemon in a bash session but my network connection drops out if I walk away from my computer or close my laptop. This locks up my sessions and I have to close the terminal window. When I reconnect I'm unable to restart nodemon because it's running as a process in the background.
Is there a way to re-open the bash window that locked up? What I've been doing is killing the nodemon process or restarting the system. I'm hoping there's a simpler way.
Use GNU screen. It runs a virtual terminal that stays open even if you loose your connection.
On the server, enter screen -S myscreen and do your normal command line work. When you disconnect, just open a new ssh connection to the server an execute screen -r myscreen. Your old session will be there as if you never left it.
You can manually leave the screen whithout killing it, by pressing ctrla and then d.
I would recommend installing the utility 'screen.'
You can install it in Ubuntu with:
apt-get install screen
Or in Red Hat / CentOS with:
yum install screen
Then you can just enter the command screen to start a session. Then you can start whatever script you need to stay running regardless of disconnects. Ctrl-A+Ctrl-D detaches you from the session. Then screen -r reconnects to it. You can also preceed a command with screen -d -m and that process will start in a screen session.

How to run auto restart in heroku for ruby scripts

In my Dev box on Nitrous, I am able to run God -c scripts.god -D to restart the two .rb files if they die.
I just run that and the processes for the most part stay alive.
But I cannot do the same in heroku. It seems when I run the god command the .god file does not open and generates an error in heroku.
Question:
How can I run God to restart failed processes in heroku as I do on my development Nitrous environment?
Or is there a recommended alternative way to watch heroku processes and restart them automatically when they fail?
On Heroku you shouldn't need to use a process supervisor like god. If all you need is to ensure your process is restarted if it crashes, Heroku can manage that fine.
It should be as simple as adding two entries in your procfile as workers. https://devcenter.heroku.com/articles/background-jobs-queueing
worker: bundle exec sidekiq
clock: bundle exec clockwork lib/clock.rb
slack_listener: bundle exec ruby lib/slack_bot.rb
You could possibly have issues, if your processing are crashing quite often. Dyno Crash Restart Policy
Your processes should start automatically when you access your website.
However, Heroku does provide commands to manage your processes, check out https://devcenter.heroku.com/articles/dynos for the complete list. E.g., to restart all processes, use the toolbelt command:
heroku ps:restart --app yourappname

Resources