G'day,
I have tried to solve this problem all morning but have not had success. I would like to search for certain scripts in a particular folder and execute them all in parallel. The scripts have different names, but all end with "run.sh". Below what I have tried without success.
The first approach was to use find in combination with -execdir. This works, however, the scripts are executed in a sequantial fashion. I want all scripts to be executed simultaneously (parallel). There seems to be no option to achieve this with -execdir
find . -name "*run.sh" -type f -execdir 'nohup' {} '&' \;
Then, I tried using xargs because there is has a parallel option (-P). I have not tried the parallel option yet, because I am unable to get xargs to run the scripts in their respective subfolders. The commands below execute all scripts, but do that in the folder I am running the command in, therefore the scripts themselves do not work. The scripts have to be executed in their own subfolders. In the above example, -execdir is doing that as opposed to -exec. How do I achieve that with xargs?
nohup find . -name "*run.sh" -type f | xargs -0 -I{} bash -c f\ \{\}
or
nohup find . -name "*run.sh" -type f | xargs -0 -I{} bash -c "f {}"
or
nohup find . -name "*run.sh" -type f | xargs -0 -I{} bash -c "./{}"
I am really frustrated and am hoping that there is somebody out there that can help.
Thank you so much!
Best Answer
Inspired by choroba's answer:
You can use
-printf
to build the command line.%h
is the directory where the file is located, and%f
is the name of the file without the path (the basename). The-P
option enables parallelism inxargs
, and-L1 -0
makes it use one null-terminated line of input per command.