2

How to start a command for example ls -la every 5 seconds on Solaris ? . I'm searching for something like watch -n 5 ls -la

3
  • 1
    and what's wrong with watch ?
    – Carpette
    Mar 3, 2016 at 13:25
  • @Carpette, I'm not sure it comes with a default Solaris installation; I see a 3rd-party package available at opencsw.org/packages/CSWwatch
    – Jeff Schaller
    Mar 3, 2016 at 13:27
  • FWIW OpenCSW is a pretty common repo for installing software on Solaris. I would just as a matter of best practice have it available unless you have a reason to leave it off (security clearance or some such).
    – Bratchley
    Mar 3, 2016 at 13:40

3 Answers 3

6

You can make a script using sleep, as described in this ticket Basically, this make something like that:

while true
do 
    ls -la
    sleep 5
done

You can launch this in a screen if you want to reach it at any moment, or you can redirect the output in a file that you can consult at any time (because i think screen, as watch, isn't installed with the basic solaris installation).

1

Building on both answers by @Carpette and @AmitSanghvi:

Loops of the form

while true
do
    ...
    sleep 5
done

can be notoriously difficult to interrupt with Ctrl+C. On the other hand, the sleep interval is a prime target for keyboard interruption.

Better is:

while sleep 5
do
    ...
done
0

To build on the earlier answer by @Carpette ... here's a parameterised version of the same script.

if [ $# -ne 2 ]; then
  echo "ERROR: Syntax is script <command> interval"
  exit
fi

while true
do
  eval "${1}"
  echo "
++++++++++++++++++++++++++++++++++++
"
  sleep $2
done

Example usage:

~/scripts/watch.sh "tail -10 /oracle/home/alert.log" 5

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .