I'm using a resource-intensive program in a Perl script [specifically
rclone
rclone
rclone
rclone
<A>
<A>
rclone
<A>
<A>
<A>
rclone
rclone
rclone
pgrep
ps
pgrep -wc
sleep
while
if
I'll assume for a moment that your script is the only thing running rclone
. If you wanted only 1 copy running, you would just use a lockfile.
For N instances (for small N), I would just have N lockfiles - have the program try each lock in turn, in a loop; pause if all the locks are already held and retry 1s later, in a loop. Once it has a lock, run rclone
then release the lock when it is done.
A more sound approach would be to use SysV semaphores but, unless you want a large N, really care about response times or are worried about fairness between callers, it is not likely to be worth the time learning them.
If your script is not the only program calling rclone
, then would need to intercept all calls - instead of putting this code in your program, could replace rclone
by wrapper that implements the parallelism constraint as above and then calls the real program.