I have a small script, that I want to constantly run until I Ctrl+C. I saw a few suggestions of using watch, and the small examples I've seen work fine, but I can't get it to work for me.

Here's my script

echo -en "\nQueued forks: "$(( $(ls -lash /var/www/data/forked/* | wc -l) - 10 ));
echo -en "\nRunning fork processes: "$(ps -ef | awk '$8 ~ /jsfork*/ {print}' | wc -l)"\n\n";
ps auxfww;

if [ -e /var/www/data/logs/fork.log ]
    echo -en "\n\nErrors:\n"
    more /var/www/data/logs/fork.log
    echo -en "\n\nNo errors.\n\n"

All the examples I've seen have been roughly watch -n x 'do something', but since my script has ' in it, it didn't work just wrapping it around.

Is there anyway I can use watch with my script?

I'm not sure if OS makes much of a difference in this case, but it's running on Ubuntu 12

  • 1
    Place your script in a file, make it executable and provide it as an argument to watch. – Marco Sep 23 '15 at 12:59
  • 1
    Anything more than a straightforward two or three commands would be better in a script. Even if it's in /tmp and thrown away after use. – roaima Sep 23 '15 at 13:04
  • 1
    Ahh good idea, just did that and it works great. If you wanna post that as an answer I'll accept. – TMH Sep 23 '15 at 13:09
  • @roaima Is that just down to readability, or is there a technical reason why that would be a better approach too? – TMH Sep 23 '15 at 15:40
  • Practicality. It is possible to escape quotes inside quotes but it gets very messy. My personal take is that if it gets that complicated it's probably easier just to throw the commands in a script and watch that. – roaima Sep 23 '15 at 17:36

You can simply place the commands in a file, prepend a shebang (#!/bin/sh), make it executable (chmod +x script.sh) and provide it as argument to watch:

watch ./script.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.