Question

Does this command exist for Linux?

runonce --dampen 300 echo "hello"

The command would take a command to run and optional criteria to limit of frequently it is executed.

The option dampen says wait 300 milliseconds and then run the command. Any other executions of this command get coalesced into a single run. This allows you to collapse events in a generic way and combine their execution.

if you ran

runonce --dampen 300 echo "hello"
runonce --dampen 300 echo "hello"
runonce --dampen 300 echo "hello"

From three different sub-shells at roughly the same time, the first one would live for 300 milliseconds and print hello. The other two would return immediately and do nothing.

If this exists, what is the name of the tool or a link to it's project page?

Was it helpful?

Solution

A possible solution, somehow taken from flock man page, would be:

#!/bin/sh
# name me 'runonce'

timeout="$1"; shift
command="$1"; shift
hash=$(echo "$command" "$@" | md5sum)
(
  flock -xw0 3 || exit
  sleep "$timeout"
  "$command" "$@"
) 3>"/tmp/$hash"

Example usage:

runonce 10 echo "hello"

where 10 is a number of seconds (not milliseconds).

EDIT: introduced hashing on commad+parameters

OTHER TIPS

I think you may want to look at cron, to see if this meets your requirement

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top