1

I have a 22.04 server running PM2 on it to provide some node applications. These applications rely on a shell variable that says if the environment is 'dev' or 'prd'

Every time I reboot the system I have to run

export ENV_TYPE = 'dev'

and then restart all the PM2 processes with

pm2 restart 0 --update-env
pm2 restart 1 --update-env

and so on

How can I build a script that will perform these actions automatically when I reboot the server (not after login, right at the end of boot procedures)? Later on I will look into run the script but now the goal is to create the script

1 Answers1

1
  • vi ~/$USER/script.sh Replace script,sh to something more descriptive.

  • press i

  • paste this:

      #!/bin/bash
      export ENV_TYPE = 'dev'
      pm2 restart 0 --update-env
      pm2 restart 1 --update-env
    
  • press and type <ESC>:wq! and then

  • chmod 750 ~/$USER/script.sh

and you have a script. Move it to a directory in echo $PATH if you need direct access to it.

The next step is to add it to /etc/crontab with @reboot as time or (the better but a bit more difficult method) to create a systemd service so you can add it somewhere in the boot chain and have a start/stop/restart feature,

Rinzwind
  • 309,379