开发者

does a runonce command exist to limit command execution

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 m开发者_开发百科illiseconds 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 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜