I need to run repeatedly (every 3600 seconds) the following commands from the terminal:
if whois abcxyz.com | grep -q 'string'; then echo 'Message line 1' echo 'Message line 2'
fiI tried using watch, as follows:
watch -n 3600 if whois abcxyz.com | grep -q 'string'; then echo 'Message line 1' echo 'Message line 2'
fibut I get error messages.
Could you please help me make it work?
Thanks
12 Answers
Since watch [options] command executes command using sh -c by default, you can use it run snippets of shell code directly provided that:
- you get the quoting right
and
- your code is sh-compatible i.e. doesn't use any bash/zsh/csh-"isms"
So for example
$ watch -n 36 'if whois abcxyz.com | grep -q "string"; then echo "Message line 1" | ts echo "Message line 2" | ts
fi' 1 watch is documented (in man watch) as:
watch [options] commandIt wants a simple command, not a whole command expression,
so, you have to wrap your command in a bash script, and watch that.
For example,
In $HOME/bin/foo:
#!/bin/bash
if whois abcxyz.com | grep -q 'string'; then echo 'Message line 1'
else echo 'Message line 2'
fiThen, afer making foo executable with chmod +x $HOME/bin/foo,
watch -n 3600 $HOME/bin/foo 4