Naxey
Naxey

Reputation: 506

How to run a .js file vie the console using a bash script

So I want to execute a javascript file the same way I run it via my console in Debian but by using a bash script at server start/reboot instead.

This is what I type manually into the console upon bootup.

cd directory
pm2 start index.js --watch
cd
cd directory2
pm2 start index2.js --watch

I want to execute the same commands including that I run the file via pm2 just with my bash script

Upvotes: 1

Views: 499

Answers (1)

slebetman
slebetman

Reputation: 113866

I'm going to assume that what you really want is to run your js script at server start/reboot instead of writing a bash script to do it.

Don't write a script to do this. PM2 can auto-write it for you.

All you need to do is make sure PM2 is running all the scripts you want to run at server start/reboot.

  1. Make sure PM2 is only running the scripts you want to run at system start. If necessary run pm2 delete all to stop all scripts then run the normal PM2 commands to run your scripts:

    pm2 start index.js --watch
    pm2 start index2.js --watch
    
  2. Save the current PM2 configuration:

    pm2 save
    

    This will save the current PM2 session in a config file. Note: run pm2 save as the same user that you run your scripts. For example if you run your scripts as naxey user then run pm2 save as naxey.

  3. Ask PM2 to write a startup script for you by running:

    pm2 startup
    

    Then follow the instructions given. Again, run this as the same user that you are running your program. But follow the instructions given by pm2 startup. For example, if pm2 startup prints out a command and ask you to run is as root then copy/paste the command and run that command as root either via sudo or su or a root login.

Once you install the PM2 startup script anytime you need to change what you want to run at system startup/reboot all you need to do is run pm2 save to re-save the PM2 session.

PM2 startup used to support Windows but currently it no longer supports Windows but it still supports most Linux distros, BSD and OSX

See the PM2 documentation for more info: https://pm2.keymetrics.io/docs/usage/startup/

Upvotes: 1

Related Questions