19

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 http://example.com/file.rar; shutdown now

But what if I type only:

wget http://example.com/file.rar
1
  • 2
    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, 2009 at 0:27

5 Answers 5

32

You can do something like this:

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

wait will wait for all programs running in background in the current shell - i.e. this wget to finish. After wait finishes - shutdown will run.

3
  • 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). Sep 3, 2009 at 19:31
  • 4
    No need unless you really want to see the progress bar. Sep 3, 2009 at 19:32
  • No need since he is leaving the machine to itself and then shut down.
    – Wolf
    Sep 13, 2009 at 13:54
11

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.

1
  • +1, just a note: wait will only work for commands started from the same shell
    – Chriki
    Aug 7, 2015 at 7:11
6

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.

4
  • +1 for fg..., but sudo could be a problem if it prompts you for a password
    – mark4o
    Sep 7, 2009 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. Sep 8, 2009 at 0:13
  • For my purpose this answer is superior as it puts the process automatically back into the foreground
    – Cookie
    Nov 20, 2012 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, 2012 at 17:37
1

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.

3
  • +1 for the first option. I'm afraid I don't understand how the second option is supposed to work.
    – innaM
    Sep 3, 2009 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 Sep 4, 2009 at 7:34
  • Use the wait PID from dmckee's answer
    – Cookie
    Nov 20, 2012 at 17:39
1

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

% wget http://example.com/file.rar
% if [ $? -eq 0 ]
> then
> shutdown now
> fi
2
  • 2
    This is a good approach when scripting, but while wget runs you don't get a prompt back to build the test. Sep 3, 2009 at 19:35
  • You can combine this with my Control-Z and fg trick to get around that. Sep 8, 2009 at 0:20

You must log in to answer this question.

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