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 usually start a download before I go to school.
Which command should I use to automatically make the necessary saves and turn off the computer after a particular task has been completed?
(Say, after installing the updates, or after downloading a large file.)

I have also cross-posted this at unix.stackexchange.

share|improve this question
1  
If you download torrents, then some of them have an option to shutdown after completion. make sure that completion means 100% and not seeding. And if you are doing it theough the command line, just append a && halt at the end of the command to automatically stop the PC. the && means execute after. –  theTuxRacer Nov 30 '10 at 15:59
    
Nearly all the answers are correct, imho. The more details you give about the kind of download and program you use the more we can be accurate :) –  Pitto Dec 6 '10 at 21:36

9 Answers 9

To do once to enable you to shut down the machine without a password:

Open a command line and type:

sudo visudo 

In the File that opens add the following line at the end:

yourusername ALL=NOPASSWD: /sbin/halt

Then exit the editor and safe it (CTRL+x).

Now you can shut down your computer from the command line, e.g. after you download something:

wget UrlOfTheFileYouWantToDownload ; sudo halt
share|improve this answer
2  
To ensure that wget returned without an error, replace ";" with "&&". –  David Rivers Jan 18 '11 at 2:56

you can use the download managers or torrent clients to shut down the pc after done or suppose you are defragmenting the disk you can click on shut down after done which is available in most softwares

share|improve this answer

To shutdown after update you may use this python script: http://kotbcorp.blogspot.com/2009/06/shutdown-after-update-in-ubuntu.html

To shutdown after download i use this command:

wget http://somehwere.com/path/to/some.file && halt
share|improve this answer
2  
Remember that halt requires superuser privilleges, so you should run sudo -s before entering the commands. –  loevborg Nov 30 '10 at 16:32

I know that this does not exactly answer your question but you can also make it time based i.e if you think that your download will complete in 2 hours you can simply issue command:

shutdown -h +120
share|improve this answer

I guess it depends a lot on how you did your 'download'.

If you're talking about an apt-get update, upgrade sequence, as root you string your commands together using the && (the sequence only continues if there were no errors)

sudo su -
apt-get update && apt-get upgrade && shutdown -h now

If you're talking about a download (e.g. using wget) [again, string the commands together using the && notation, ensuring that the commands execute in order only if the previous command was successful]

sudo su -
wget -t0 -c http://somedownloadsite/path/file.foo && shutdown -h now

If your download was done by a graphical client (gui), then it would depend on that client's mechanisms for executing scripts after a successful download.

share|improve this answer
2  
Those commands won't work because sudo su - starts a new shell and the commands after the semicolon aren't executed until the new shell terminates. –  loevborg Nov 30 '10 at 16:31
    
Fair comment. have modified the instructions to indicate that they're a separate sequence of commands. –  finley Dec 1 '10 at 9:22

Other answers are correct, but missing something:

wget UrlOfTheFileYouWantToDownload ; sudo halt

vs

wget UrlOfTheFileYouWantToDownload && sudo halt

The second one will only shut down the computer if wget returns successfully. The first one will run halt whether wget returns successfully or fails.

share|improve this answer

Instead of changing the permissions on shutdown, you could do the following:

sudo bash -c 'apt-get update; apt-get dist-upgrade; shutdown -h now'

What this does is run a bash prompt as root that executes the items in the single quoted list and then quits. The bonus of this method is that Ubuntu will forget your initial sudo authentication after some period (not sure what the default is) and someone coming across your machine could only stop the current command, not run a new one. Also, for everyone saying to use su, try:

sudo -s

share|improve this answer
    
what if the network gets down for a short time and apt-get fails doing its job? –  Quamis Dec 2 '10 at 7:23
1  
I would assume you'd still want to shutdown. If any stage of the commands in -c fail the others will continue as I used a semicolon instead of the && (any commands chained with && will only function if the each one succeeds in turn). –  Calcipher Dec 2 '10 at 19:37

Thank you for all this info

i personally found it was easier to use sudo gedit to set up the original

yourusername ALL=NOPASSWD: /sbin/halt since

sudo visudo was confusing to me and did not show the last line clearly

so i used

sudo gedit /etc/sudoers

then ended up with a clear (to me) page

# User privilege specification
root    ALL=(ALL:ALL) ALL


# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

#includedir /etc/sudoers.d
shantiq ALL=NOPASSWD: /sbin/halt

Just thought i would mention that in case anyone else was a bit confused too

share|improve this answer

I dont think you can do that... There is no way you can know when a specific application finished its thing.

I used to do sudo su; sleep 3600; halt;

share|improve this answer
    
why the vote down? –  Quamis Dec 2 '10 at 7:24
    
Someone probably disagreed with the 'there is no way of knowing' part? –  belacqua Aug 23 '12 at 15:00

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.