Search code examples
node.jsubuntupm2

PM2 --watch not working when edited by other users


Running on Ubuntu, latest versions for everything including pm2.

I have my main user "ubuntu" with full access via ssh, a user "sftp" that is only allowed sftp access that also owns the folders where the nodejs apps are.

To set up auto-start on boot and file watch I did:

pm2 startup systemd
*pasted the cmd output*
pm2 start path/to/project/index.js --watch --name app_name
pm2 save

It autostarts on boot, but when I make a change to the project files (I confirmed they get synced to the remote), pm2 doesn't reload the app. I read in some places that the user watching for changes has to be the same as the owner of said files, how do I do this? I tried sudo su before starting the app via pm2, but that also didn't work.

Thank you in advance.


Solution

  • Figured it out, it was the working dir of the pm2 watcher. By providing an absolute path to the dir it works, tried two methods: pm2 start /path/to/project --name my_app --watch /path/to/project

    The second is creating a json file for pm2 to load with and also using absolute paths inside:

    {
      "apps": [{
        "name": "my_app",
        "script": "/path/to/project",
        "watch" : ["/path/to/project"],
      }]
    }
    

    Followed by pm2 start my_pm2_config.json

    Edit: Third solution (from pm2 git issue thread about cwd). Navigate to the project dir and run pm2 start index.js --name my_app --watch from there. After reboot, this working dir will persist.