0

Where do I add nohup and & to put this in the background? I tried multiple places and had no luck.

find . -links 2  -type d |
xargs -I{} -P 5 -n 1  bash -c 'cd "{}" && run_script.sh'
1
  • bash -c 'cd "{}"... will break on ", Use arguments - xargs -n1 bash -c 'cd "$1"' --
    – KamilCuk
    Commented Jul 22, 2021 at 14:01

1 Answer 1

1

Simplest answer - just put it at the end of the line.

find . -links 2 -type d | xargs -I{} -P 5 -n 1 bash -c 'cd "{}" && run_script.sh' &

That effectively puts the pipeline in background.
You're probably going to want to manage your i/o though.

4
  • -P 5-n should probably be -P 5 -n. You can put the nohup in front of the find.
    – tripleee
    Commented Jul 22, 2021 at 13:54
  • Yeah, I had to fix that in my test and forgot to fix it here. Thanks. Commented Jul 22, 2021 at 14:45
  • run_script.sh is long running process. It is terminating even if find is in the background
    – Tims
    Commented Jul 22, 2021 at 15:06
  • maybe what you need is cd "{}" && nohup run_script.sh &'. That will thow a nohup.out` in every directory, but your output (if any) will be clearly associated with the relevant location. You could manage your own redirections if you wanted. Make sure here is useful logging, even if it's just turning on debug mode with set -x inside run_script.sh. Commented Jul 22, 2021 at 15:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.