PM2 with application arguments on restart only - arguments

I would like to use pm2 with 2 different scenarios, the first start of the app without any arguments.
Any restart of the app after crash with an argument.
pm2 start myapp
and if the app crashes it should run with an argument
myapp --myargument
Any ideas?

Related

AdonisJs 4.1: how to create a start script that migrates the db and start the server?

I have a dockerized AdonisJs App that I'm trying to deploy on ECS (AWS). I managed to deploy the image but now I don't know how to run migrations when I deploy.
By following a Udemy course I saw that somebody had to do the same stuff, but with laravel. In the Dockerfile instead of running CMD ['artisan','serve'] he created a start.sh script where he starts the app, put it in the background, runs the migrations, and then put the app back in the background. Here is the script:
#!/bin/sh
# turn on bash's job control
set -m
# Start the primary process and put it in the background
php-fpm &
# Start the helper process
php artisan migrate
# now we bring the primary process back into the foreground
# and leave it there
fg %1
I tried to do the same thing with Adonis, this is my script (one of many versions):
#!/bin/sh
# turn on bash's job control
set -m
# Start the primary process and put it in the background
adonis serve &
# Start the helper process
adonis migration:run
# now we bring the primary process back into the foreground
# and leave it there
fg %1
But I always get errors. For example:
The server starts but then the migrations do not run because adonis cannot connect to the database. I don't know how to debug it, since if I just start the app normally Adonis can perfectly connect to the database.
(I tried this only locally) The server starts, the migrations run but then the server process doesn't come in foreground so the application is not really started (curl localhost it gives me curl: (7) Failed to connect to localhost port 80: Connection refused) and I cannot either stop the server with ctrl+c, I have to find the docker container and stop the container.
This is what the console shows me:
SERVER STARTED
info: serving app on http://0.0.0.0:80
Nothing to migrate
Could you please help me create a script that does this?
EDIT1: I noticed that even if I create a script with only "adonis serve" it still doesn't work, so maybe that it's just not the right way to start the server trough a script?
The first problem I needed to solve was the migration command that wasn't terminating. The cause was how I was starting the adonis-scheduler package. I was starting it from the start/kernel.js file. Now instead I created a scheduler.js (inside start) with:
'use strict'
/*
|--------------------------------------------------------------------------
| Run Scheduler
|--------------------------------------------------------------------------
|
| Run the scheduler on boot of the web sever.
|
*/
const Scheduler = use('Adonis/Addons/Scheduler')
Scheduler.run()
And added this in server.js:
new Ignitor(require('#adonisjs/fold'))
.appRoot(__dirname)
.preLoad('start/scheduler') //code added
.fireHttpServer()
.catch(console.error)
By doing this my migration always terminates.
The second problem I faced was that "#!/bin/bash" wasn't supported, so I needed to change the script in a "#!/bin/sh" format. Unfortunately this format doesn't support putting job in background and move them in foreground later, so I just run the migrations and then start the server. This is the file:
#!/bin/sh
# Start the helper process
adonis key:generate
adonis migration:run --force
# Start the primary process
adonis serve

AWS ECS trouble - Running shell script to boot program

I am trying to run a Docker image on amazon ECS. I am using a command that starts a shell script to boot up the program:
CMD ["sh","-c", "app/bin/app start; bash"]
in order to start it because for some reason when I run the app (elixir/phoenix app) in the background it was crashing immediately but if I run it in the foreground it is fine. If I run it this way locally, everything works fine but when I try to run it in my cluster, it shuts down. Please help!!
Docker was supposed to keep track of your running foreground process, if the process stop, the container stop. The reason your container work when you use command with "bash" because bash wasn't stop.
I guess you use she'll script to start an application that serve in background like nginx or a daemon. So try to find an option that make the app running foreground will keep your container alive. I.e nginx has an option while starting "daemon off"
for some reason when I run the app (elixir/phoenix app) in the background it was crashing immediately
So you have a corrupted application and you are looking for a kludge to make it looking like it somewhat works. This is not a reliable approach at all.
Instead you should:
make it working in background
use systemctl or upstart to manage restarts of Erlang VM on crashes
Please note that it matters where you have your application compiled. It must be the exactly same architecture/container as the production one, with same Erlang, Elixir, OS versions, otherwise nobody guarantees it will be robust or even working.

PyCharm debugger stopping containers after "Waiting for connection" ends

I am trying to debug docker-compose containers in PyCharm.
Here is how my run configuration looks like:
Note that it is a Django app. When I try to start the debugger it is successfully starting all containers and I can access the app on the browser for a short amount of time. Also there is a background task running "Waiting for connection"
Right after it ends, the containers are suddenly stopped.
What am I doing wrong?
Here is what I did to fix this:
Moved the entrypoint to the docker-compose file instead of the Dockerfile
Changed the port to 8000 from 8050
I have no idea why this matters but it fixed it.

Running an docker image with cron

I am using an image from docker hub and it uses cron to perform some actions after some interval. I have registered and pushed it as described in documentation as a worker process (not a web). It also requires several environment variables.
I've run it from command line, e.g. docker run -t -e E_VAR1=VAL1 registry.heroku.com/image_name/worker and it worked for few days, then suddenly stopped and I had to run the command again.
Questions:
Is this a correct way to run a docker (as worker process) in Heroku?
Why might it stop running after few days? Is there any logs to check?
Is there a way to restart the process automatically?
How properly set environment variables for the docker in Heroku?
Thanks!
If you want to have this run in the background, you should use the -d flag to disconnect stdin and stdout, and not -t.
To check logs, user docker logs [container name or id]. You can find out the container's name and id using docker ps -a. That should give you an idea as to why the container stopped.
To have the container restart automatically add the --restart always flag when you run it. Alternatively, use --restart on-failure to only restart when it exited with a nonzero exit code.
The way you set environment variables seems fine.

Run a command without the command line running in a new window

I am trying to run a command from a node.js app that's installed on the machine. The command runs fine (it is a command to launches Tomcat). However, the command I run actually open the command line window that is launching Tomcat. Is there a way to do the same thing (run the same command to launch Tomcat) but without the command line window opening up?
Let me know if you need more info!
Your problem isn't with node.js but rather with tomcat. You are probably running the wrong command.
instead of calling catalina.bat start, rather call catalina.bat run.
alternatively you can set it up as a service and call net start tomcatX

Resources