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 at askubuntu.


It depends you started task. If it is some command line tool you could simply run halt or shutdown -h now:

  1. wget http://..../somelargefile; halt - halt will be executed after wget

  2. wget http://..../somelargefile && halt - halt will be executed if wget return no errors

  3. wget http://..../somelargefile || halt - halt will be executed if wget return errors

Alternative to this is using kshutdown - in this app you can specify what action should be done (reboot/shutdown/hibernate/etc or command execution) and when (after some time or when another application exits). If you want to use this tool you need to chose correct process - some update utilities starts few subprocesses.


  • 4
    You can also give shutdown a time to wait; no need to use kshutdown for that purpose. – Cascabel Nov 30 '10 at 17:47
  • 1
    No, but shutdown doesn't have any way of waiting for an actual event. – Shadur Oct 31 '11 at 9:19

Every process has a directory named according to its process id (pid) in the proc filesystem. Therefore you can wait for a specific process' termination by checking whether its directory exists repeatedly.

You can use this to perform a system shutdown after process termination like this (replace 2296 by the pid to wait for):

su -c 'while [[ -d /proc/2296 ]]; do sleep 1; done; poweroff'


  • You can decide whether or not to shutdown after setting off the shell command
  • The shutdown is issued as root, the command to wait for is not necessarily done as root (no need to workaround the late sudo-password prompt).


  • It may happen that your process terminates and the OS then reuses the same pid for a newly spawned process within that second (race condition). You'd thus continue to wait for that new process to terminate.
  • 2
    Main point: This works even if the process has started. – Heisenberg Dec 6 '14 at 8:15
  • If you have started something from a terminal that you still have open ps in that same terminal should help you spot the PID for the CMD. If the process was/is in another terminal pipping the ps output for everything through grep to filter for your command should help you find it ps -elf | grep <command_related_word> – TafT Sep 11 '15 at 7:42
  • Better check my answer based on pgrep aka process grep... – Gilles Quenot Oct 15 '18 at 22:03
  • How can I put this command into the background? – DerWeh Sep 3 at 20:23
  • 1
    @DerWeh Wrap it in a shell script and follow the steps given here: stackoverflow.com/q/625409/1025391 – moooeeeep Sep 4 at 6:51

You can use the && and || operators to chain commands together. For example:

apt-get update && sleep 2 && init 0

which will perform an update to the system using aptitude, wait 2 seconds, and go to runlevel 0, depending on your system this may or may not actually power off the hardware.

To speak to one or more operations following a more arbitrary operation, such as a, presumably web browser, download you would require additional logic to watch the downloading file to verify that the download has in fact completed, however, the shell logic is unchanged.


If you want to monitor an already started process, you can do this in a :

# while pgrep &>/dev/null -f process_name; do sleep 60; done; shutdown -h now

There is a application, named sentinella which can do it for you with its GUI: sentinella

for installing it:

sudo aptitude update
sudo aptitude install sentinella

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.