LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Running shell scripts, commands need root/su, how? (https://www.linuxquestions.org/questions/linux-newbie-8/running-shell-scripts-commands-need-root-su-how-780455/)

digity 01-07-2010 12:10 AM

Running shell scripts, commands need root/su, how?
 
I've written a couple of shell scripts and some of the commands in them need root privileges. I've read somewhere to never put the root or super user's password in a script because it's not encrypted. How do I run these commands as root/su in a script?

Simon Bridge 01-07-2010 12:12 AM

sudo scriptname

digity 01-07-2010 12:20 AM

oops, sorry. They won't be ran manually, they'll be kicked off by cron. So it has to be automated, no user input required.

evo2 01-07-2010 01:13 AM

You can configure sudo to not require a password for specific commands.

Evo2.

PS. However, if you do this for the script, make very sure that the script is only writable by root. A slightly better approach is just to use sudo for the specific commands where root is needed, not for the whole script.

digity 01-07-2010 01:46 AM

hmmm. how about "sudo crontab -e"? won't that make my scripts run with root privileges?

Simon Bridge 01-07-2010 05:19 AM

@ digity - you are correct.

Just add it to root's crontab instead of yours.
Running from inside su or sudo can confuse crontab (see man crontab) so:

sudo -i
crontab -e


All times are GMT -5. The time now is 06:22 AM.