How do you use pm2 startup with a non-root user? - node.js

According to the documentation here: http://pm2.keymetrics.io/docs/usage/startup/#startup-systems-support
You can use the command pm2 startup ubuntu -u nodeapps to resurrect all saved pm2 jobs on server startup.
I ran this command as the nodeapps user. Then I was given a sudo su command to run. I logged out of nodeapps, used sudo su to log into the system as root, and ran the command:
sudo su -c "env PATH=$PATH:/usr/bin pm2 startup ubuntu -u nodapps --hp /home/nodeapps"
The processes did not restart on server restart. I found this question on Stack Overflow: Ubuntu 14.04 - pm2 startup not starting after reboot.
In the script /etc/init.d/pm2-init.sh I found the line that question recommended addressing:
export PATH=/usr/bin:$PATH
export PM2_HOME="/home/nodeapps/.pm2"
But it looks correct to me so I didn't change anything.
I then found this question: pm2 Startup not starting up on Ubuntu
and in my boot logs I find the following line:
Starting pm2
/usr/bin/env: node: No such file or directory
I know that 'node' on Ubuntu is actually 'nodejs'. Could this be the reason?
If it is, what can I do to make the startup command look for nodejs instead of node.
Alternatively, could this be a $PATH problem? If it is, how can I add the correct path to root (at least I think it should be added to root)

I don't know if it will help you but I use in this way:
As a non-root user
pm2 startup -u <YOUR_NON_ROOT_USER>
Copy line showed like
env PATH=$PATH:/usr/bin pm2 startup systemd -u delivery --hp /home/delivery
As a root execute
env PATH=$PATH:/usr/bin pm2 startup systemd -u delivery --hp /home/delivery
Back to non root user and type:
pm2 start <YOUR /PATH/TO/INDEX.JS> --name <YOU_APPLICATION_NAME>
As a non-root type:
pm2 save
reboot
sudo reboot
As a non-root user type the commando bellow to check if it works
pm2 status
PS: Change as needed.
I hope it will be useful for you or someone.

(Posted on behalf of the OP).
In fact that was the problem. Fixed via creating a symlink (as root):
ln -s /usr/bin/nodejs /usr/sbin/node

Related

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.
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:
preserve the caller's PATH using --preserve-env, e.g. sudo -E "PATH=$PATH" env.
modify the sudoers policy to set PATH to include pm2's parent directory.
move pm2 to a directory that's already in the secure path.
lastly, you can reconsider if pm2 really needs root permissions.

pm2: command not found in ec2 when running script file

after deploying node.js project using gitlab CICD successfully when try to execute script.sh file which is in side project folder it shows
./script.sh: line 3: pm2: command not found
I want to restart pm2 server after deploying node.js file
now , if I run this script.sh file after ssh into my ec2 from terminal it execute successfully.
here is my script.sh file
Help me to solve this ,
Thank You in advance :)
Okay so , after tried many possibles ways I have find that first go into your root in ec2 using sudo su and then install npm & pm2 into root directory.
after this you can able to run pm2 command from script file.
Also if you can add the below commands and check, it's worked for me.
sudo ln -s "$(which pm2)" /usr/bin/pm2
I worked in AWS EC2 instance.

PM2 installation not generating script

I need to deploy a NodeJs application following this tutorial https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04.
When installing PM2 module and generating pm2 daemon script pm2 startup upstart, no command is shown, normally it should display a command like this:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
NodeJs v0.10.48 (on Ubuntu-12)

Run shell command in jenkins as root user?

I have recently started using Jenkins for integration. All was well until I was running jobs on master node without shell command but I have to run jobs on master as well as slave node which contains shell commands to. I am not able to run those shell commands as root user. I have tried
Using SSH Keys.
Setting user name in shell commands.
Using sudo.
I am getting permission denied error every time I use any of the above methods.
I would suggest against running the jenkins user as root. This could expose the operating system and all of the repo's which jenkins can build.
Running any script as root is a security risk, but a slightly safer method would be to grant the jenkins user sudo access to only run the one script, without needing a password.
sudo visudo
and add the following:
jenkins ALL = NOPASSWD: /var/lib/jenkins/jobs/[job name]/workspace/script
Double check your path via the console log of a failed build script. The one shown here is the default.
Now within the jenkins task you can call sudo $WORKSPACE/your script
You need to modify the permission for jenkins user so that you can run the shell commands.
You can install the jenkins as as service (download the rpm package), You might need to change the ports because by default it runs http on 8080 and AJP on 8009 port.
Following process is for CentOS
1. Open up the this script (using VIM or other editor):
vim /etc/sysconfig/jenkins
2. Find this $JENKINS_USER and change to “root”:
$JENKINS_USER="root"
3. Then change the ownership of Jenkins home, webroot and logs:
chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins
4) Restart Jenkins and check the user has been changed:
service jenkins restart
ps -ef | grep jenkins
Now you should be able to run the Jenkins jobs as the root user and all the shell command will be executed as root.
For Linux try to follow these steps:-
This worked for me.
Change Jenkins user: sudo vi /etc/default/jenkins
Change user root or your user that you use to access to your files:
$JENKINS_USER="root"
Execute using the user that you setup before:
sudo chown -R root:root /var/lib/jenkins
sudo chown -R root:root /var/cache/jenkins
sudo chown -R root:root /var/log/jenkins
Run as a services:
service jenkins restart
Or
systemctl jenkins restart
You can execute jenkins has a process and disable headless mode for Linux with UI.
/etc/alternatives/java -Djava.awt.headless=false -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
Validate Jenkins is running currently:
ps -ef | grep jenkins
Another option is to set up a jenkins "Slave" that is actually running as root on the master and restrict it to tied jobs, then point your job at that slave. Far from ideal but certainly a quick solution.
Or you can change the permission of docker.sock. Make sure your docker container is running the user as root
docker exec <jenkinsContainerID> chmod 777 /var/run/docker.sock
You just need to run the shell command on Linux machine using Root privileges from Jenkins.
Steps :
1) sudo vi /etc/sudoers
2) Add line :
jenkins ALL=NOPASSWD:/path of script/
3) From Jenkins,run the script on remote shell using sudo .
for eg : sudo ps -ef
4) Build Jenkins job now.
This job runs the script on Linux machine using root privileges.

Start http, mysql, ftp and cronjobs at boot on EC2 running Linux?

Is there a script that can be run at launch to start all these? If so, where do I put it?
If not, is there a way to do it via the management console?
This Just Works if you install these from packages. An init script will be placed in /etc/init.d and they will be started on boot.
for example installing apache with:
sudo apt-get install apache2
will result in the file /etc/init.d/apache2 being installed and the appropriate links set up to ensure the script gets called on system startup and shutdown.
You can edit the crontab with the command:
crontab -e
The cron daemon is automatically started at boot time and will run these commands according to the times you specify.

Resources