Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I often set my work computer running some time-consuming computations before I go home in the evening. After they're done, I'd like it to shutdown. However, as shutdown requires root privileges, it must be executed using sudo. Obviously I'm not there to enter my password 7 hours later, so thus far I've had to execute the entire list of commands (cmd1; cmd2; cmd3;... shutdown) as root - not ideal.

Is there a way I can "pre-authorise" the sudo at the end of a list of commands? Some way I can entire my password at the start of execution so bash doesn't need me later on?

Thanks!

share|improve this question
    
How are you running these events? Can you sudo the thing that is running the events in the first place? You said you don't want to run the whole script as root does that also include running the whole script as sudo too? –  Scott Chamberlain Sep 3 '13 at 17:15
    
Have you seen any answers that you would qualify as "Best" mate? One usually marks an answer as best in the question to help anyone else who has a similar problem. –  PsychoData Sep 7 '13 at 20:29

5 Answers 5

To complete the first answer:

Set up a way that your non-privileged time-consuming computations can initiate the shutdown that’s waiting in the privileged shell (started by sudo -s or sudo su).  Using a file as a flag is a good, simple way of doing this.  So, in your non-privileged shell, do

cmd1; cmd2; cmd3; > ok_to_shutdown_now

and in your privileged shell, run a script that says

while [ ! -f ok_to_shutdown_now ]
do
        sleep 60
done
shutdown
share|improve this answer
    
This is the best solution IMHO. inotifywait may be used to wait for the ok_to_shut file to be created but for shutting down purpose this should be enough. –  jaychris Sep 3 '13 at 21:11

You can run commands in a root environment so that you enter in your password once when you are at the computer and then run root commands after without entering your password.

You can use

   sudo -s

or

   sudo su

(Read here for the difference)

This will give you root privileges and stop you from entering your password in again. Then, you can run sudo commands after this and they will not prompt you for a password.

share|improve this answer

You have quite a few options here to reach your goal, depending on what you are willing to risk. Not all of them having to do with caching your sudo credentials.

You can switch off needing a password for your user or for your user executing shutdown by adding a line with the tag NOPASSWD to your sudoers file.

You can authenticate to sudo by running sudo -v. Your password will be cached for 15 minutes so you would have to run a sudo -v all, say, 12 minutes, to update your credentials and kill that task after the last batched process is done.

If you know how long your batched processes take, you can simply tell shutdown the time after which it shall shut down the system.

You can increase the default 15 minute timeout of the password cache with the option timestamp_timeout in the sudoers file. Either arbitrarily, or, if you know how long the batches processes take, to that time. Or you could switch off the timeout completely with a negative value and use sudo -v to authenticate and sudo -k to "log out" of sudo when you are done. In between these two calls, you can run sudo without entering your password again.

If you are not fine with not requiring a password for sudo, but instead store your password in a text file, readable only by your user, then you can create a little batch script which outputs your password to stdout and either use this as the askpass program for sudo or simply use it in a pipe with sudo -S, which read the password from stdin. Combined with a little script you could store your password in a text file at the beginning, from which it is read by the script providing it to sudo and at the end delete this file again.

Echo script

#!/bin/bash
echo mypassword

or

#!/bin/bash
cat /mysecretpwfile

and then ./echopw | sudo -S or

export ASKPASS=~/echopw
sudo -A shutdown -h now

Or, at last, you could simply put the shutdown command in a shell script and set the shell script to suid, owner root, your group, 750.

share|improve this answer

you could

sudo sh -c 'cmd1; cmd2; cmd3;... shutdown'

or give your userid privilege to sudo shutdown without a password. There is an example just for this here: Shutting down without root

share|improve this answer
    
I'd go for the sudoers config. That file is quite long; the relevant example is in the section headlined "Shutting Down From The Console Without A Password" –  rici Sep 3 '13 at 23:32

The easiest solution is probably storing the password in a variable and then later pipe it from this variable to sudo -S.

#!/bin/bash
echo "Please enter your password:"
read -s mypasswd

#additional stuff

echo $mypasswd | sudo -S shutdown -h now
share|improve this answer

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.