Skip to content

Instantly share code, notes, and snippets.

@lxr
Created November 13, 2015 21:38
Show Gist options
  • Save lxr/958657cd237a191d1142 to your computer and use it in GitHub Desktop.
Save lxr/958657cd237a191d1142 to your computer and use it in GitHub Desktop.
execute a command repeatedly
#!/bin/sh -e
delimiter='\
'
interval=1
while getopts d:i: opt; do
case $opt in
d) delimiter=$OPTARG;;
i) interval=$OPTARG;;
\?) exit 1;;
esac
done
shift $((OPTIND-1))
while sleep -- "$interval"; do
"$@" | sed ":a;\$!{N;s/\n/$delimiter/;ba;}"
done
NAME
poll - execute a command repeatedly
SYNOPSIS
poll [-i interval] [-d delimiter] [command ...]
DESCRIPTION
The poll utility executes command at one-second intervals until
it fails.
The options are as follows:
-i interval
Set the execution interval (in seconds).
-d delimiter
Replace all newlines (except the last) in the output
of command with the delimiter string. Any forward
slashes and newlines in the string must be escaped with
backslashes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment