I have a folder of .command files (regular task) on Mac OS X , which I would like to execute simultaneously.

At the moment, I have been iterating through each file in the directory one by one to execute it. using something like:

LAUNCHLOG=~/Desktop/My\ Automation/Resources/Logs/_AutoLaunchAgent.txt
mkdir -p ~/Desktop/My\ Automation/Resources/Logs/
mkfile -n 0k "$LAUNCHLOG"
chmod 0777 "$LAUNCHLOG"
FILES=`find -f ~/Desktop/My\ Automation/Resources/Temp/`;
while read -r line; do
    "$line" >>"$LAUNCHLOG"
done <<< "$FILES"
sleep 10

The above is working, however it is quite slow... For speed reasons, I would like to execute the commands (every item in my directory) at once.

The commands are all independent of each other and do not need to communicate, and if I do this manually everything works correctly and I get a big speed boost...

What is the best way to achieve this? I tried using parentheses inside my while loop in an attempt to execute inside a new subshell on each iteration, but the process was still executing one file at a time.


Launching your scripts in background would prevent the loop from waiting and do just what you want:

while read -r line; do
    (bash "$line" >>"$LAUNCHLOG") &
done <<< "$FILES"

The logs will be mixed in your logfile though...

  • Thanks! This worked well. However my command files do not seem to be executing properly? Currently, they seem to get to the first sleep command and then stop running entirely... I have some of them sleep until an external process has finished. Any ideas on how to fix this? – user1718699 Jun 26 '15 at 2:39
  • You may want to debug your scripts using the set -x bash functionnality. This is unlikely related to them being launched in background. – pedroapero Jun 26 '15 at 14:49
  • Thanks, I ended up adding a wait command at the end of my script so that it waits for all sub processes to finish before exiting as without this command all sub processes are terminated when the parent process finishes... – user1718699 Jun 28 '15 at 2:23
  • You could do that properly by using wait instead of a sleep. Without any parameter, a call to wait will wait for all subprocesses to finish before returning. – pedroapero Jun 29 '15 at 9:31

Why not try /usr/sbin/periodic? You can use periodic.conf for controlling your logging or redirect the output to a log file.

    /usr/sbin/periodic ~/Desktop/My\ Automation/Resources/Temp/ >> /path/to/logfile

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.