0

Suspending a for loop (using Ctrl-Z), then resuming it with bg specifically, will prevent it from iterating to the next instance. The for loop still finishes the current iteration, but just stops there. The issue happens when sending the job to background only. No issue when using fg.

Running in a subshell does not even help.

Context:

$ (for f in *.webm; do ffmpeg -fflags +genpts -i "$f" -map 0 -s hd1080 -c:v libx264 -preset slow -crf 20 -b:a 64k "${f%.webm}".mp4 > /dev/null 2>&1; done)

Is there a way to allow the for loop to complete till the end after suspending/resuming to the background?

Faxopita
  • 83
  • 1
  • 9
  • Probably a typo, but the loop in your post does not have a closing `done`. I have not been able to reproduce this on my system, with a simpler loop: `for f in *.webm; do; print ${f:r}.mp4; sleep 2; done`. The process continues in the background after issuing the `bg` command. – Gairfowl Apr 12 '23 at 02:23
  • Thanks. Corrected. I ran your command as well, and it just works fine. However, the issue still persists with FFmpeg. I don't understand why it's such a problem with FFmpeg. – Faxopita Apr 12 '23 at 09:46
  • I did further tests. Combining `for` loop with FFmpeg simply doesn't work with pausing/resuming it with `bg` command. Simple example: `(for f in *.m(kv|p4); do ffmpeg -i "$f" -vn -c:a copy ${f%.m(kv|p4)}.aac 2> ffmpeg.log; sleep 3; done` – Faxopita Apr 12 '23 at 11:08
  • On my system, `ffmpeg` was pausing to ask if the output should be overwritten, even though `-n` is supposed to prevent that. This seemed to work, both with `fg` and `bg`: `for f in *.mp4; do print $f; ffmpeg -i $f -y -c:a copy $f:r.aac &> $f:r.log; sleep 2; done`. I tweaked the code a bit so the log wasn't overwritten in the loop; `$f:r` is the filename without the extension, `&>` redirects both stdout and stderr. – Gairfowl Apr 12 '23 at 13:55
  • Thanks for your suggestion. After issuing your command it still only works with `fg` on my end. – Faxopita Apr 12 '23 at 14:48

0 Answers0