9

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.

El Burro
  • 271

9 Answers9

10

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
binW
  • 13,194
7

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
αғsнιη
  • 36,350
AK76
  • 851
3

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
aneeshep
  • 30,949
3

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.

David Oneill
  • 12,614
2

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

2

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.

finley
  • 2,095
1

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 (depending on the timeout period set - 15 minutes by default) 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
Zanna
  • 72,312
1

I personally found it was easier to use 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 -H 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

Zanna
  • 72,312
shan
  • 11
0

I don't 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;

Zanna
  • 72,312
Quamis
  • 127