Search code examples
node.jsamazon-ec2sudopm2amazon-linux

On EC2: sudo pm2 ls command not found, but pm2 ls without sudo is ok


Problem: When I run "pm2 ls" under ec2-user, it runs perfectly.

When I run "pm2 ls" after sudo -i, it gives -bash: pm2: command not found.


Solution

  • for safety reasons, sudo typically runs under a different environment. for example, an administrator could configure sudo to use a secure PATH rather than inheriting the caller's PATH.

    you have a few options:

    1. preserve the caller's PATH using --preserve-env, e.g. sudo -E "PATH=$PATH" env.
    2. modify the sudoers policy to set PATH to include pm2's parent directory.
    3. move pm2 to a directory that's already in the secure path.

    lastly, you can reconsider if pm2 really needs root permissions.