0

I have a Linux command that can be called by another application multiple times (in quick succession) with different parameters. The problem is, if the command gets executed in too quick of succession, the function that it performs will not work properly.

What I’m looking for is some simple way to ensure that each call to the command will be properly delayed/spaced (by a couple milliseconds) from each other.

Order of execution does not matter in this case and I have no control over how the application makes the calls.

Edit: The command being called is used to transmit an RF signal on a Raspberry Pi. As such, the command execution must be exclusive (no concurrency) with an additional delay between executions to prevent the receivers from misreading the signals.

KYDronePilot
  • 527
  • 3
  • 12

2 Answers2

2

For anyone with the same problem, this worked for me: https://unix.stackexchange.com/questions/408934/how-to-serialize-command-execution-on-linux

CMD="<some command> && sleep <some delay in seconds>"

flock /tmp/some_lockfile $CMD
KYDronePilot
  • 527
  • 3
  • 12
1

For a simple concurrency control, which will limit concurrent execution to instances, consider the following while loop (modify as needed).

Note that the script must be invoked as /path/to/script.sh so that it will find other instances. Starting with 'bash /path/to/script.sh' will require changes!

#! /bin/bash

   # Process identifier.
echo "START $$"
ME=${0##*/}
   # Max number of instances
N=5
   # Sleep while there are more than N instances.
while [[ "$(pgrep -c -x $ME)" -gt "$N" ]] ; do echo Waiting ... ; sleep 1 ; done

   # Execute the job
sleep "$@"
echo "Done $$"
dash-o
  • 13,723
  • 1
  • 10
  • 37