views:

44

answers:

2

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?

A: 

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

Raghuram
`at` might be closer, but I don't believe that it supports the joining logic that the OP requests.
dmckee
+1  A: 

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

enzotib
This is a cool solution. I didn't state it, but runonce --dampen echo "goodbye" shouldn't be blocked, since it's a second different command. Using your flock scripting solution, I guess you could make a lock file that is the hash of your command, so that dampening varied over commands.
Ozten
I have taken into account your suggestion, and modified correspondingly.
enzotib