How to shut down the computer after a task has been completed?

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.

3

9 Answers

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

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
1

To shutdown after update you may use this python script:

To shutdown after download i use this command:

wget && halt
1

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.

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

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 && 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.

2

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

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
2

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;

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like