How Do I Kill YouTube-DL in a Shell Script?

I am using shell script to run youtube-dl to download multiple videos in the same time, and it runs perfectly. But sometimes the download speed is too slow, I want to kill the process and restart it(It always runs faster the second time, and Without killing the process, the downloaded video would always be corrupted). The thing is, i couldn't find a way to kill it.

I tired to insert osascript in Shell Script to get its process ID:

osascript -e 'tell application "Keyboard Maestro Engine" to setvariable "PID" to "'$$'"'

But it seems youtube-dl called some python program with its own process ID, so using the PID of the shell script doesn't really work.

So my questions is:

Is it possible to get the real process ID of youtube-dl and return it?

if not, does killing the macro(it runs asynchronously) work?

Or is there any other way to kill the process?

Thank you very much!

If you press ctrl+c in the terminal (not cmd+c), the current download will cancel.

Hey Pang,

You're running from an Execute a Shell Script action in a Keyboard Maestro macro – yes?

You cannot access the script-runner process once it's going.

You have to cancel the macro using Keyboard Maestro's Status Menu, or kill the macro using another macro.

Here're some hints:

Determining which macro is going rogue and debugging it - #5 by ccstone

-Chris

1 Like

Yes. This command will return the PID of youtube-dl

	/usr/bin/pgrep -f youtube-dl

But what you really want is pkill (see below).

If the script is running asynchronously, then quitting the macro would not kill the script.

Yes. This:

	/usr/bin/pkill -f youtube-dl

will kill youtube-dl.


Note that you need -f for pgrep and pkill in order to find the process name. Otherwise it will not find youtube-dl.

Please feel free to ask questions if you have them.

1 Like

Yeah! That did the trick! Following this I used prep -f youtube-dl to find the latest PID that just started, then used kill -9 to kill it then restart the download. It worked perfectly!

Thank you very much, this really made day!

You don't need to do that because

pkill -f youtube-dl

will find the PID of youtube-dl just like pgrep will and kill it. That's exactly why pkill was created. And it's been in macOS since … Lion? I think? A long time.

You also don't need kill -9 just a regular pkill should work just fine.

1 Like

i have multiple youtube-dl running in the same time, so kind of have to find the specific one to kill

in that case you're going to have to go to Terminal.app and either do something like:

      pgrep -ifl youtube-dl

which should show you all of the youtube-dl that are running, including the PID. Then you can just kill the one you want. I would not recommend trying to automate that. Chances of accidentally kill-ing the wrong process is too high.