1

As written in the title, I would like to run a command ONLY after the previous command 'youtube-dl' has finished and closes. I am not running this command on bash just on the ubuntu terminal.

I would like to run this command:

Youtube-dl -o output.file link ; sleep 4 ; mv output* /to-directory

Command 1 can run from 5 seconds to xx minute and if I state just sleep for 4 seconds it may not take enough time to run the command 2 when command 1 has finished and closed regardless of the result. I could use sleep 4 for indefinite timelapse but it would really hampers my speed of executing commands altogether.

13
  • If I am not mistaken, it should be enough to write these commands next to each other with space between them. "command1 command2" Mar 19, 2020 at 11:11
  • unfortunately it is not. Mar 19, 2020 at 11:14
  • 3
    So what is the problem? man bash says:Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. Isn't that what you wanted?
    – mook765
    Mar 19, 2020 at 11:19
  • 1
    You haven't given your OS/release details, but if I use mousepad ; sleep 2 ; echo 'hi' the echo doesn't occur till 2 seconds after I exit mousepad.
    – guiverc
    Mar 19, 2020 at 11:20
  • 4
    youtube-dl forks to the background. This is why this construct does not work. It really depends on the command on what happens, so you may want to make your question more specific.
    – vanadium
    Mar 19, 2020 at 11:51

2 Answers 2

3

If you really want only to move the file, why don't output it directly to the destination directory?

youtube-dl -o /to-directory/output.file link

If you want to perform some other, much complicated task, you could use the option --exec, described at the bottom of youtube-dl -h, so the command should looks like:

youtube-dl -o output.file link --exec 'mv {} /to-directory/'

Here is а working example:

youtube-dl -o irakli https://youtu.be/FyV_49qbvU4 --exec 'mv {} ~/Desktop/'

After the command is finished, the file irakli.mkv will be at your Desktop. Enjoy watching!


Bonus track :) Here is one script that I'm using in order to download mp3s from youtube.com, while I'm browsing it via my desktop browser.

cat /usr/local/bin/youtube-dl-clipboard # The file must be executable, of course
#!/bin/sh
notify-send "YouTube-Download BEGIN"
youtube-dl -i -x --audio-format mp3 --output "$HOME/Downloads/%(title)s.%(ext)s" "$(xclip -o)"
  • Note the script uses xclip, so it should be installed, along with the most recent version of youtube-dl.

Crate custom shortcut as this:

enter image description here

Then while you browsing youtube.com, just copy the link of any video you like - Ctrl+C, and then press Ctrl+Alt+V. After few seconds a new mp3 file will be available within the Download directory.

7
  • it says exec command not found, 'youtube-dl -x -o output.file youtube.com/… --exec 'ffmpeg {} -codec:a libmp3lame -qscale:a 2 output.mp3'' Mar 19, 2020 at 19:39
  • Hi, @Alessio_110, do we talking about Ubuntu and the most recent version of youtube-dl?
    – pa4080
    Mar 19, 2020 at 19:42
  • linux mint 19.2 Mar 19, 2020 at 19:44
  • 1
    Mint is off topic here. However follow the wget method from the link, provided above and you will have the newest version of the software with the --exec option available. It is strange how an old version could still downloading... @Alessio_110
    – pa4080
    Mar 19, 2020 at 19:48
  • I have download it and yet exec is not running. I acknowledge the fact I could just use youtube-dl -x --audio-format mp3 youtube.com/… but I thought I could learn a little bit more today. apologies for wasting everybody's time Mar 19, 2020 at 19:55
2

Youtube-dl -o output.file link && sleep 4 && mv output* /to-directory
or even better:
Youtube-dl -o /to-directory/output.file link as you would not have to move anything later on.

If youtube-dl indeed runs in background and is set to exit after downloading,
You can use:

youtube-dl -o output.file link; while :;do sleep 1; [[ $(ps -C youtube-dl -o pid=) ]] || break;done; mv output.* /to-directory/

Explanation while :; - an infinite loop meant to wait for youtube-dl to finish.
[[ $(ps -C youtube-dl -o pid=) ]] || break - check if there exists a process id for youtube-dl. if exists, loop again. End the loop otherwise.(using break).

NOTE: pidof Youtube-dl command can be used to obtain process ID for a program. however, as youtube-dl is called using python3, it has not process of its own. Thus pidof youtube-dl would not yield a value in this case. as such, ps command has been used to obtain the pid.

3
  • 1
    Unfortunately, it does not work, it still stalls after running the youtube-dl, can you please explain a bit what you did there? it is quite interesting and it is teaching me a lot. Mar 19, 2020 at 19:09
  • 1
    pid="$(Youtube-dl -o output.file link &)"; # runs Youtube-dl command in background, saving the generated process id in "pid" variable. while ([ "${#pid}" -gt 0 ])do sleep 1;done; # waits for the process id length to be 0, meaning that there is no process running. oops! it should be dynamic. hence pid value ought to be updates within the loop. i.e pid=$(pidof youtube-dl) . Updating the solution
    – ByronTaaka
    Mar 19, 2020 at 21:02
  • This works flawlessly Apr 7, 2020 at 22:14

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.