Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm downloading file with wget, and I want to turn off my computer after downloading will be finished, but I need to go somewhere now :) Is it possible to execute command in bash exacly when process will be finished?

I know that this works:

wget; shutdown now

But what if I type only:

share|improve this question
Consider using && and || instead of ;. && and || check the return value of the previous command before executing the next command...just in case wget fails, you may not want to shutdown. – KFro Sep 8 '09 at 0:27
up vote 30 down vote accepted

You can do something like this:

  1. stop execution of wget with ctrl-z (it's not stopped, it's suspended)
  2. put it into background (bg)
  3. run: wait; shutdown -h now

wait will wait for all programs ran in background from current shell - i.e. this wget.

After wait will finish - shutdown will go on.

share|improve this answer
don't forget to foreground (fg) your wget instance when done. If wget is already running, you can put it into the foreground with ^z (control-z). – Andrew Scagnelli Sep 3 '09 at 19:31
No need unless you really want to see the progress bar. – dmckee Sep 3 '09 at 19:32
No need since he is leaving the machine to itself and then shut down. – Wolf Sep 13 '09 at 13:54

In another shell, use

ps -u <your username> | grep wget

to find the PID of the command you want to wait on, then

wait <PID>; shutdown -h now

so that shutdown will not run until process has terminated. See also depesz's answer.

share|improve this answer
+1, just a note: wait will only work for commands started from the same shell – Chriki Aug 7 '15 at 7:11

My solutions is this: suspend your long-running task with Control+Z, then type something like:

fg; echo "Done!"

If you want to run react differently based on the success or failure of your command, use && and || as short-circuit operators.

fg && echo "Success!" || echo "Failure!"

EDIT: This won't work with sudo, because it will ask for your password. I have, accordingly, removed the examples using sudo. If you need sudo for the second task, use another of the methods posted here.

On the other hand, I believe that both GNOME and KDE offer ways to trigger a shutdown/reboot without using sudo, by sending an appropriate D-Bus signal or something.

share|improve this answer
+1 for fg..., but sudo could be a problem if it prompts you for a password – mark4o Sep 7 '09 at 7:56
Hmm. Yes, I haven't used this trick for sudoing things when something finishes. For sudo, you'd need a different trick. I'll edit my post accordingly. – Ryan Thompson Sep 8 '09 at 0:13
For my purpose this answer is superior as it puts the process automatically back into the foreground – Cookie Nov 20 '12 at 17:35
Also, passwords for sudo can be disabled (per user and per command) - if the user is allowed to shutdown via sudo this once, he might be allowed to do so always. A hostile shutdown might not be that dangerous in some cases - certainly not as dangerous as a compromised guest access on a running machine. – Cookie Nov 20 '12 at 17:37

Yes, just type out your second command and press enter. If your current program is scrolling text you can't see what you're typing correctly so watch your keys so you do it right.

A smarter way is to switch to another TTY (CTRL+ALT+F1 through F6) and run the command simultaneously, or if you're using a command-line program like screen you could create a new window in it.

share|improve this answer
+1 for the first option. I'm afraid I don't understand how the second option is supposed to work. – innaM Sep 3 '09 at 19:16
Pressing for example CTRL+ALT+F2 will switch you to a new second console where you can login and type a command, in this way you can run a second command there. Second, if you download screen (a program to use your console in a more fancy way) you can open up a second tab/window where you can again type commands, without having to login again. But as you need it simultaneously, you can stay with option 1, I don't think there is another way to achieve the same effect. Unless you use another TTY/window to check through a script if the process is still running. :-P – Tom Wijsman Sep 4 '09 at 7:34
Use the wait PID from dmckee's answer – Cookie Nov 20 '12 at 17:39

If you want to get fancy, check the return status of the wget and only shutdown if it was successful.

% wget
% if [ $? -eq 0 ]
> then
> shutdown now
> fi
share|improve this answer
This is a good approach when scripting, but while wget runs you don't get a prompt back to build the test. – dmckee Sep 3 '09 at 19:35
You can combine this with my Control-Z and fg trick to get around that. – Ryan Thompson Sep 8 '09 at 0:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .