launchd startInterval shorter than time taken for the script to complete
launchd focuses on launching jobs and keeping jobs running, it does not have a mechanism to handle overlapping jobs.

Lock File

Traditionally in a UNIX environment, a lock file is used to stop processes from being run multiple times.

The core steps are:


On script launch, if the lock file already exists, stop the script.
On script launch, if the lock file does not exist, create the lock file.
On script finish, remove the lock file.


On macOS, create your lock file in /var/tmp for computer wide processes.

Sample Implementation

if ! mkdir /var/tmp/myscript.lock 2&gt;/dev/null; then
    echo "Myscript is already running." &gt;&amp;2
    exit 1
fi


See Quick-and-dirty way to ensure only one instance of a shell script is running at a time and What is the best way to ensure only one instance of a Bash script is running? for sample scripts.

Potential Problems

There are edge cases.

launchd wants jobs to run for at least n seconds before finishing. When the script finds an existing lock file, consider sleeping for n seconds and then exiting.

What happens if your script is killed or exits because of an error? Can you be certain the lock file is removed?

In C, a trick to ensure a file's removal is to create, open, and delete the file – a deleted file held open on UNIX will remain until the opening process exits. The file is deleted even if the process crashes.

In a shell script, catch the terminate signal and ensure the file is removed.

Another potential problem is checking for the file existing. This check could happen factions of second before the previous script finishes. This will be rare but possible. The approach quoted above claims to overcome this.