'Commands not working inside shell script but working directly on the console
So i have this command which i want to run inside the script:
pm2 restart my-application --update-env
My script:
#!/bin/bash
sudo -u deploy bash << EOF
cd /home/deploy/my-agent
pm2 start ecosystem.json
EOF
echo "out of the deploy user"
echo "export edgeboxId=$edgeboxId">> /etc/bash.bashrc
sudo -u edgebox bash << EOF
pm2 restart my-application --update-env
EOF
echo "out of application user"
sleep infinity
this command pm2 restart my-application --update-env doesnt work inside the script as expected(it should the update the env for pm2 but it restarts application without updating the env). Also it doesnt throw any error. The output of the command is:
[PM2] Applying action restartProcessId on app [my-application](ids: [ 0 ])
[PM2] [my-application](0) ✓
┌─────┬───────────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼───────────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ my-application │ default │ 1.0.328 │ fork │ 348 │ 0s │ 2 │ online │ 0% │ 20.9mb │ edgebox │ disabled │
└─────┴───────────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
But when i run this command directly on the console, it works as expected. So my question is, how to make this command work from inside script? It looks like some shell problem but i cant figure out.
Note: This command and the script is being run inside a docker container and the shell inside docker container is /bin/bash and version is 5.0.3(1)-release.
Solution 1:[1]
You can launch pm2 because its location is part of the $PATH variable. That $PATH variable is user dependent.
So, when you do sudo you switch to another user, which means another $PATH variable, so it's possible that pm2's location is not there.
In order to solve this, switch to that user in command prompt and adapt $PATH environment variable (in a persistent way).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dominique |
