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
  • 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 06 '10 at 21:36
  • I think the process based answer in your cross-post is one of the nicest and most flexible I have seen. http://unix.stackexchange.com/a/65060/21070 You may be able to use `ps -elf | grep <somthing_related_to_task> to identify the PID which will expire at the end of the process. – TafT Sep 11 '15 at 07:39

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,034
  • You can add sudo at the start of the command if you're accessing the system remotely. – x__x Oct 15 '22 at 13:31
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нιη
  • 35,660
AK76
  • 801
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,321
  • 2
    Remember that halt requires superuser privilleges, so you should run sudo -s before entering the commands. – loevborg Nov 30 '10 at 16:32
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,144
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,085
  • 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 01 '10 at 09:22
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
  • 70,465
  • what if the network gets down for a short time and apt-get fails doing its job? – Quamis Dec 02 '10 at 07: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 02 '10 at 19:37
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
  • 70,465
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
  • 70,465
Quamis
  • 127