1

I run youtube-dl in background by putting & after the command. The problem is it doesn't care that I put it in the background and keeps printing information in the terminal.

Not only does it refuse to stop with Ctrl+C or Ctrl+Z, it just doesn't do anything other than showing these messages. To stop it I must close the terminal.

Here is what happens exactly:

mohamed@Smithers:~$ clear
mohamed@Smithers:~$ youtube-dl https://www.youtube.com/watch?v=LMQsYSuw8hw &
[1] 2028
mohamed@Smithers:~$ [youtube] LMQsYSuw8hw: Downloading webpage
[youtube] LMQsYSuw8hw: Downloading video info webpage
[youtube] LMQsYSuw8hw: Extracting video information
[youtube] LMQsYSuw8hw: Downloading MPD manifest
WARNING: Requested formats are incompatible for merge and will be merged into mkv.
[download] Resuming download at byte 11440128
[download] Destination: I Was On Japanese TV... and I F#%KING HATED IT.-LMQsYSuw8hw.f137.mp4
[download]   4.8% of 231.85MiB at 199.01KiB/s ETA 18:56^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
[download]   4.8% of 231.85MiB at 136.95KiB/s ETA 27:30^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
[download]   5.0% of 231.85MiB at 127.41KiB/s ETA 29:31^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
mohamed@Smithers:~$ ^C
[download]   5.0% of 231.85MiB at 111.38KiB/s ETA 33:44^C
mohamed@Smithers:~$ ^C
[download]   5.0% of 231.85MiB at 111.21KiB/s ETA 33:47^C
mohamed@Smithers:~$ ^C
[download]   5.1% of 231.85MiB at 116.77KiB/s ETA 32:09^C

[download]   5.5% of 231.85MiB at 108.71KiB/s ETA 34:24^C

[download]   5.6% of 231.85MiB at 76.68KiB/s ETA 48:41
  • 1
    To stop background jobs, you first need to bring them to foreground by fg command. If you do want background jobs with no output on terminal, just run command > /dev/null 2>&1 &; if you want to completely disconnect them from terminal, run nohup command &; this will keep the command running, even if you close the terminal, and append the output to a file named nohup.out. – ridgy Dec 28 '16 at 19:54
  • 1
    @ridgy You might want to post an answer about that. My answer focuses on how to run youtube-dl differently, and does include information about redirection, but it doesn't address how the OP should fix the problem after running it in a way they don't want (as your comment does), nor does it give any information about job control. I'm somewhat reluctant to expand my own answer since it's already rather complicated and because I don't want to remove the primary focus from youtube-dl's options too much. – Eliah Kagan Dec 28 '16 at 20:42
5

youtube-dl is running in the background, but it is still sending output to your terminal, and producing quite a lot of it to inform you of its progress. There are a number of ways to change this, but you'll probably either want to make youtube-dl produce less output, or cause its output to be sent to a file (or discarded altogether) instead of appearing in your terminal.

Way 1: Run youtube-dl -q to suppress status messages.

If you don't want to see the progress messages, you can pass the -q flag (or --quiet, which does the same thing) to the youtube-dl command:

youtube-dl -q https://www.youtube.com/watch?v=NZ-ElsvYKyo &

You'd have to replace that URL with whatever you're trying to download, of course. Some messages may still be printed, including errors and warnings. But it's likely you want to see those.

If you don't want youtube-dl to produce any warnings, you can run youtube-dl with the --no-warnings flag. I don't suggest doing that, though, unless you're sure that's what you want.

youtube-dl -q --no-warnings https://www.youtube.com/watch?v=NZ-ElsvYKyo &

To learn more about the command-line options accepted by youtube-dl, you can run man youtube-dl or read its manual page online.

Way 2: Redirect standard output of youtube-dl to a file.

To append the output to dl.log in the current directory (you can name this what you want, and it will be created if it doesn't already exist), run:

youtube-dl https://www.youtube.com/watch?v=NZ-ElsvYKyo >> dl.log &

If you want to overwrite dl.log instead of appending text to the end, use > instead of >>:

youtube-dl https://www.youtube.com/watch?v=NZ-ElsvYKyo > dl.log &

Normal output will then be redirected to dl.log (or whatever file you tell it to use). Error output, however, will still be shown on your terminal. For example, when I ran that command, I got one warning printed to my terminal:

ek@Io:~$ youtube-dl https://www.youtube.com/watch?v=NZ-ElsvYKyo >> dl.log &
[1] 14456
ek@Io:~$ WARNING: Requested formats are incompatible for merge and will be merged into mkv

Everything else went into the log file:

ek@Io:~$ cat dl.log
[youtube] NZ-ElsvYKyo: Downloading webpage
[youtube] NZ-ElsvYKyo: Downloading video info webpage
[youtube] NZ-ElsvYKyo: Extracting video information
[youtube] NZ-ElsvYKyo: Downloading MPD manifest
[download] Destination: Lecture 1 _ Topics in String Theory-NZ-ElsvYKyo.f244.webm
[download]  23.2% of 359.52MiB at 204.49KiB/s ETA 23:02ek@Io:~$

You can view the output with cat (as I've done above) at any time, to check the progress.

When you run youtube-dl without -q, it produces output that is primarily meant for a terminal. This output includes control characters that tell a terminal to erase the previous progress message, so that it may be visually replaced with the new progress message. Some ways of viewing the file, like cat dl.log, will not show the "erased" messages or the control characters. They're still part of the file, though, and you can view them with some other utilities; for example, view dl.log works.

Way 2a: Redirect the output to /dev/null to get rid of it.

If you want the output to just be discarded rather than saved in an actual file, then you can redirect it to /dev/null:

youtube-dl https://www.youtube.com/watch?v=NZ-ElsvYKyo > /dev/null &

With /dev/null, it doesn't matter if you "overwrite" (>) or "append" (>>).

This is a handy technique in general, though probably unnecessary with youtube-dl. If you want to suppress progress, you can just run youtube-dl -q instead.

Way 3: Redirect standard error, too.

Regular output and error output are separate. It's common for youtube-dl to produce both. The above method redirected only standard output. You can also redirect standard error. There's a lot you can do with redirection, including redirecting the two output streams to separate files. For our purposes here, I'll just show how to send both standard output and standard error to the same file:

youtube-dl https://www.youtube.com/watch?v=NZ-ElsvYKyo &>> dl.log &

When I ran that, my shell showed only

[2] 15009

...and both regular output and error output appeared in dl.log:

ek@Io:~$ cat dl.log
[youtube] NZ-ElsvYKyo: Downloading webpage
[youtube] NZ-ElsvYKyo: Downloading video info webpage
[youtube] NZ-ElsvYKyo: Extracting video information
[youtube] NZ-ElsvYKyo: Downloading MPD manifest
WARNING: Requested formats are incompatible for merge and will be merged into mkv.
[download] Destination: Lecture 1 _ Topics in String Theory-NZ-ElsvYKyo.f244.webm
[download]   0.0% of 359.52MiB at 107.65KiB/s ETA 56:59ek@Io:~$

The above example used &>>, which appends to dl.log. To overwrite the file instead, use &> (or >&, which also works) instead of &>>:

youtube-dl https://www.youtube.com/watch?v=NZ-ElsvYKyo &> dl.log &

Way 3a: Redirect both normal and error output to /dev/null.

If you really want to, you can redirect both standard output and standard error to /dev/null so it is all simply discarded:

youtube-dl https://www.youtube.com/watch?v=NZ-ElsvYKyo &> /dev/null &

You probably want to see actual error messages, though. If it's just status messages and warnings you don't want, then you don't need any redirection. In that case you can use youtube-dl -q --no-warnings as described above. (See Way 1.)

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.