(Using an Ubuntu EC2 on AWS)

I've a script, /home/ubuntu/start.sh. If I run it as ubuntu, it runs well. I need it to be run at launch, so I put it in /etc/rc.local. This will then be run as root on reboot, and this fails. I'm able to reproduce the failure by:

# I'm ubuntu
$ whoami
ubuntu
$ sudo su
# i'm now root
$ whoami
root
$ ./start.sh
./start.sh: line 9: npm: command not found
$ su -c ./start.sh - ubuntu
./start.sh: line 9: npm: command not found

So it looks like:

  • root doesn't know about npm (installed by ubuntu under /home/ubuntu/.nvm/versions/node/v4.2.6/bin/npm so that makes sense)
  • su -c ./start.sh - ubuntu doesn't exactly run the script as ubuntu

How can I run this script exactly as if I was logged in as ubuntu?

share|improve this question
    
As user ubuntu this could work to run as root: sudo -E ./start.sh Regarding /etc/rc.local. I would run it as user ubuntu: sudo -i -u ubuntu /home/ubuntu/start.sh – rudimeier Nov 4 '16 at 23:22

PATH=$PATH:/node/v4.2.6/bin/ ./start.sh

share|improve this answer
    
I saw how that could work, but is there a more systematic way to solve this? Because I'll have to do that for each program that's unknown from root, or if I change my npm version etc. Can I not just tell root "become ubuntu for a sec, do a few things like ubuntu would do, and become root again" ? – Guig Nov 4 '16 at 23:11
    
Then launch it with ubuntu user from crontab. @reboot. Don't forget PATH! – Ipor Sircer Nov 4 '16 at 23:15

Edit /etc/rc.local with your favorite text editor.

sudo nano /etc/rc.local

And add the line su ubuntu -c /etc/rc.local/ubuntu/start.sh & (if that is indeed the path to your file)

This runs /etc/rc.local/ubuntu/start.sh as the user ubuntu on startup and as a background process.

share|improve this answer
    
Based on what you describe about starting and stopping it sounds like you'll want to make a daemon that you can /etc/init.d/myservice start (or stop) – trudgemank Nov 4 '16 at 23:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.