Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've modified my .profile to include a folder if a flash drive is plugged in. When running the command as the user it works fine but tells me the scrip must be run by sudo (this is what i want). However, when i try to run it with sudo i get "command not found"

I have a symlink (flash) in my /var/www folder pointing to my /media/flash drive. (nevermind this setup since is just for dev)

this is my user's .profile :

# set PATH so it includes flash scripts
if [ -d "/var/www/flash/scripts" ] ; then
    PATH="/var/www/flash/scripts:$PATH"
fi

when trying to run as sudo i get:

sudo: script: command not found    

any ideas?

share|improve this question
add comment

3 Answers

When you run the script as sudo you are trying to access root. So your $HOME/.profile wont be accessed instead /root/.profile will be accessed.

So make the changes what ever you did in $HOME/.profile to /root/.profile. To create /root/.profile. Use the following commands.

sudo su
cd /root
touch .profile

after making the changes try running the script.

or edit $HOME/.bashrc and add the following line

alias sudo='sudo env PATH=$PATH $@'

Hope this helps.

share|improve this answer
    
I tried "alias sudo='sudo env PATH=$PATH $@'" but i get "env: wp: Permission denied" –  user564448 Nov 9 '12 at 17:35
    
sorry, "wp" is the script –  user564448 Nov 9 '12 at 17:47
    
Try the other step I mentioned. –  devav2 Nov 9 '12 at 17:49
    
Read it here you should use sudo su -p –  devav2 Nov 9 '12 at 17:53
add comment

that is the behavior of sudo in ubuntu..

for example run following command

echo 'echo $PATH' | sh

and than this one

echo 'echo $PATH' | sudo sh 

you see the output is different! to avoid different paths put the following in your ~/.bashrc

alias sudo='sudo env PATH=$PATH'
share|improve this answer
    
+1. You are right. –  devav2 Nov 9 '12 at 17:29
    
After putting alias sudo='sudo env PATH=$PATH' into my .bashrc i get "env: wp: Permission denied" –  user564448 Nov 9 '12 at 17:33
add comment

You can preserve the value of $PATH when using sudo. Use visudo to add $PATH to the list of variables to keep. A line like the following in /etc/sudoers should do the trick:

Defaults env_keep += "PATH"
share|improve this answer
add comment

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.