I need a service stopped when a change happens in a failed.log file. I was looking at the watch command to do this: watch -d -g -t 'ls -l /home/username/*failed.log' This command will check if the output from ls has changed. If it has I want it to stop the CUPS service.

I was looking into inotify-tools, but this is where it got a bit too complicated for me. Any suggestions?


How about connecting both commands by &&? This will make sure that the first command is successfully exited with code 0 which equals true in bash before the second part is executed. If that is true, the second part is executed and cups service will be stopped.

watch -d -g -t ls -l /home/username/*failed.log && service cups stop

  • Thank you Andy, I will try this :) The change to the log file doesn't happen all that often, so it might take me some days or even a week to test. – Marius Sep 6 '18 at 8:23
  • @Marius it will work, however to check it out just create the file your self for test, open an other terminal and run: touch $HOME/testfailed.log – Ravexina Sep 6 '18 at 8:26
  • @Ravexina Ok thank you, I will get this tested asap! – Marius Sep 6 '18 at 9:20

I have a similar use scenario that might help you, depending on the content of the log file.

I have an OpenVPN server at work. When I need to reboot to apply updates but I have current user sessions, I set up a while loop to check for a specific grep pattern, ("vpnip,username,wanip,timestamp"). If the pattern is found, I wait some time; else if the pattern isn't found (non-zero exit code), I reboot.

This is very specific to my server, but you can adjust the grep statement to suit your needs.

while [ 1 ] ; do 
    if egrep '10.8.0.[0-9]+,[a-zA-Z\-]+,[0-9\.]{7,15}:[0-9]+,(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) +[0-9]{1,2} [0-9:]{8} [0-9]{4}' /ramdisk/openvpn-status.log ; then 
        echo someone still connected ;
        sleep 30s ; 
        echo reboot now ; 
    fi ; 

The example above has echo reboot now because I didn't want to reboot just now, but you get the idea.

SSH session, wait to reboot

  • Thank you for your suggestion, altough I do not think this fits the scope of my problem. I am monitoring public display machines that shows the bus times for our local bus company. If these machines gets their network connection interrupted then the image that previously showed bus times, goes completely white. The monitoring software is quite limited, so it can't pick up short interruptions lesser than 5 minutes. My workaround solution to this was to make a script that checks if the image showing software logs an error to the log file, then stop a service if it does. – Marius Sep 6 '18 at 9:22

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.