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.

6 Answers 6


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, 2010 at 17:47
  • 1
    No, but shutdown doesn't have any way of waiting for an actual event.
    – Shadur
    Oct 31, 2011 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, 2014 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, 2015 at 7:42
  • 1
    Better check my answer based on pgrep aka process grep... Oct 15, 2018 at 22:03
  • How can I put this command into the background?
    – DerWeh
    Sep 3, 2019 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, 2019 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

I had the same problem with download (in Firefox) and backup. I solved it by starting another, parallel bash script. It monitors network traffics. When it is low for some period of time, it will suspend/shutdown the computer.

The heart of the script:

ip -s link show  dev wlan0 # This gets transffered bytes on wifi. 
ip -s link show  dev wlan0 | sed -n '4p;6p'         |  awk '{ print $1 }' | paste -sd+ | bc # This combines all the traffic together. 

So the core function:

function statistics { # $1=network $2=seconds
first=$(ip -s link show  dev $1 | sed -n '4p;6p' | awk '{ print $1 }' | paste -sd+ | bc)
sleep $2
second=$(ip -s link show  dev $1 | sed -n '4p;6p' | awk '{ print $1 }'| paste -sd+ | bc)
echo $(( (1 + $second - $first) / $2 )) # simple averadge
return 0

while (true); do
 if [ $(statistics wlan0 300) -lt 100000 ]; then 
  sudo pm-suspend # but you must put pm-suspend in "sudo visudo". Say as UserIsMarchingSchool ALL=NOPASSWD: /usr/sbin/pm-suspend

Well, a bit crazy, but independent from the original program. May it help you as an hint. I suppose it can be solved in a simpler way. :-)

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.