how to run pm2 with "npm run start" - pm2

So i have an app that was built for me and I need to keep it running. but the app was built with the instructions of running it with
npm run start
I am trying to figure our how to do this using either the ecosystem.config.js or command line arguments.
I tryed
pm2 start npm --name "myApp" -- run "start-test"
But the pm2 log shows an exit code of 1.
So what is the propepr way to accomplish this?

Related

pm2 is prepending directory in Windows

I'm using pm2 to execute a process:
pm2 start "env-cmd -f ../bare-metal.env nest start --watch"
This works perfectly on Linux, however, on Windows I'm getting the following error:
[PM2][ERROR] Script not found: C:\Users\username\projectFolder\env-cmd -f ..\bare-metal.env nest start --watch
It seems like pm2 is prepending the directory. If I execute the command without pm2 it works well so the problem seems to be with pm2 on Windows.
Is it possible to fix this behavior so pm2 executes the command just as the command line does, without prepending anything?

Unable to run startup script when creating instance on Google Cloud Platform

I have a simple startup script which looks like so:
#!/usr/bin/env bash
sudo apt update
sudo apt install -y ruby-full ruby-bundler build-essential
And create VM instance on GCP like so:
$ gcloud compute instances create test-app --boot-disk-size=10GB --image-family ubuntu-1604-lts --image-project=ubuntu-os-cloud --machine-type=g1-small --zone europe-west1-b --tags test-server --restart-on-failure --metadata-from-file startup-script=startup.sh
My startup.sh is executable. I set its rights like so:
$ chmod +x startup.sh
When however I enter the shell of my newly created instance and check bundler:
test-app:~$ bundle -v
I get these messages:
The program 'bundle' is currently not installed...
So, what is wrong with that and how can I fix it? PS. If I run all my commands just from inside the instance shell, it's all ok, so there is some problem with using startup script on GCP.
I tested with your use case, But the bundle package was installed without making any changes.
Output:
bundle -v
Bundler version 1.11.2
You can check VM serial console log output to verify if start-up script ran. Check the VM instance to verify if the package is installed using the commands below:
sudo apt list --installed | grep -i bundle
sudo egrep bundle /var/log/dpkg.log
In addition, check the gem list bundle

How can I get pm2 to use my ecosystem.config.js file on reboot?

My ecosystem.config.js file loads my environment great with this command:
pm2 start ecosystem.config.js
When I run any of these command, my environment is reloaded just fine:
pm2 reload myapp
pm2 restart myapp
pm2 reload ecosystem.config.js
pm2 restart ecosystem.config.js
Then I try to make sure I get my environment back after a reboot. If I run pm2 startup I get this:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u me --hp /home/me
After running that command, I can reboot my machine and my application is restarted automatically. However, I am missing my environment from the ecosystem.config.js file. Why? How do I make sure the environment from my ecosystem.config.js environment is loaded when the machine is rebooted? Thanks.
Dont forget to save your config! pm2 save
In case you want pm2 on startup with changed logs path:
pm2 delete all
pm2 start ecosystem.js
pm2 save
pm2 startup
I had the same issue. After executing the command suggested by pm2 startup, I found I had to first run pm2 delete all, then restart using pm2 start ecosystem.config.js. My environment is now loading as expected after rebooting.
May be worth sharing that a file named ecosystem.js, even if empty, needs to exist on the system in the user's home path so that pm2 can load it.

pm2 command not found after path is added

I am installing PM2 for hubot rocket.chat adapter. so i did the following:
I firstly added a file to hubot adapter folder, then
sudo npm install pm2 -g
cd <hubot directory>
pm2 start <pm2.json>
The i got error saying this:
No command 'pm2' found, did you mean:
Command 'wm2' from package 'wm2' (universe)
Command 'pms' from package 'pms' (universe)
Command 'pmk' from package 'pmk' (universe)
Command 'pmw' from package 'pmw' (universe)
Command 'fpm2' from package 'fpm2' (universe)
Command 'pom2' from package 'libpod-pom-perl' (universe)
Command 'pmi' from package 'powermanagement-interface' (universe)
Command 'pm' from package 'powerman' (universe)
pm2: command not found
Then I tried to add the path by doing:
sudo env PATH=$PATH:/home/jy/.npm-global/bin pm2 startup -u safeuser
and
export PATH=$PATH:/home/jy/.npm-global/bin
and restarted PuTTY,
still get the same error.
when I went to where PM2 is at, which is /home/jy/.npm-global/bin, and do pm2 start <pm2.json> command, still get the same error.
Is there anything i did wrong? or there might be other reasons?
The problem is that you are running NPM as sudo, so you will only be able to access it using:
sudo pm2 start server.js
Install without sudo, you may even install without the -g flag and call it directly from node_modules directory. This may be useful if you do not have root (admin) privileges in the machine you're working on.
npm install pm2
./node_modules/.bin/pm2 start server.js
Source: https://stackoverflow.com/a/40812333/1052581

If a service is started during docker build, should it be running at runtime?

I have been working on setting up a self contained rails app in a single container. This means getting both rails and a data persistence service running at the same time in one container. In our case, that means mysql.
However, I ran into multiple issues getting this working, because mysql wasn't running.
During the build step, if I had RUN mysqld and then a separate RUN rake db:create step, rake would crash, because mysql was down.
So I worked around this by wrapping the two commands into a script. However, at runtime, rails would fail to startup because mysql wasn't running.
My intuition says that if mysql is started during the build, it should be available at runtime, but I did not have that experience. Starting the rails server had to be wrapped in a script with another call to mysqld.
Here's the dockerfile:
FROM ruby:2.2
RUN mkdir -p $APPDIR
WORKDIR $APPDIR
ADD Gemfile* $APPDIR/
RUN bundle install
RUN apt-get update -qq
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nodejs mysql-server --no-install-recommends
RUN rm -rf /var/lib/apt/lists/* # */ broken syntax highlighting
COPY . $APPDIR
RUN script/mysql-setup.sh # contents are: mysqld_safe; rake db:create; rake db:migrate
EXPOSE 3000
CMD ["script/rails-launcher.sh"] # contents are: mysqld_safe; rails s
Do I need to do something differently in the Dockerfile? Why isn't mysql up at runtime?
My intuition says that if mysql is started during the build, it should be available at runtime
This is incorrect. Docker will start the service for you and perform the subsequent steps you've defined in the same RUN command, but then it bundles everything up into an intermediate image for subsequent commands. The image doesn't have a known state of running processes, only whatever is required for startup such as init.d scripts.
Your solution would be to use a server startup script or continue to invoke mysqld_safe as you do in your CMD line.
A good idea is to use supervisord to maintain all of your services in a non-daemon mode. Phusion also provides a nice base image with a runit initializer script.
Eventually, you'll come to see how the power of Docker lies in how you can actually break MySQL out of your Rails app container and run it in an entirely different container linked together.
The RUN Command is used to configure your image, each time it is called, a new layer is created with the results of run command. So, if you need to configure your database on the image build step, you have 2 solutions: you can call a number of command in a single RUN call, like
RUN /bin/bash -c "mysqld_safe" && "rake db:create" && "rake db:migrate"
Or via call of single script, as you did.
In both cases, you have to inderstand, that the fact, you runned something during the image build, it'll not run automatcally on the container start up. So, you have to start your database server manually on container start up.