0

Is it possible to use the watch command to see if a file exists, and if it does then stop a service? Or possibly, as a workaround, check the content of the file and then stop a service?

I was looking at the watch command to do this:

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

The problem for me is that I don't understand this command completely. I think it check the output "ls" here, but what does it check "ls" for? I need the command to check the "twitter-failed.log" for any kind of content. The file is blank when there are no errors, but generates a whole bunch of text when there is an error. So the watch command only needs to check the file for any kind of output, then stop the service.

4
  • 1
    Please clarify if you want to stop the service "when file exists" or "when content changed".
    – pLumo
    Sep 12, 2018 at 8:47
  • Preferably "when file exists", but if that is harder to achieve then "when content changed" is good enough.
    – Marius
    Sep 12, 2018 at 8:48
  • 1
    but that condition doesn't fit with "The file is blank when there are no errors".
    – pLumo
    Sep 12, 2018 at 8:49
  • True, this solution is kind of a work around. If we can achieve "when file exists" then I'll just make sure to delete the file after the error has occurred. But if "when file exists" is hard to achieve then I can also make sure to delete the content of the file so that it is blank again.
    – Marius
    Sep 12, 2018 at 8:50

1 Answer 1

1

You can use inotifywait:

Put the following in a bash script and make it executable.

#!/bin/bash

set -e # make script exit on error

FILE_TO_WATCH="/home/username/twitter-failed.log"

# Truncate / Create empty file
> "$FILE_TO_WATCH"

# Wait for File was modified
inotifywait -e modify "$FILE_TO_WATCH"

# Do something after modified
service cups stop
send_yourself_a_notification

Alternatively, you could also use some loop like this, but I think it's better to use a specialized tool like inotifywait to do that:

until [ -s "$FILE_TO_WATCH" ]; do sleep 2; done && service cups stop
3
  • Thanks a bunch, I really think the inotifywait-tool will solve my problem! Just one follow up question though: I am quite bad at Ubuntu and have been put in a tight spot at my workplace, having to fix something I have no competence in. Do I make a .sh file in the same directory and copy/paste the script you wrote? I tried reading on the inotify manpage, but I guess they assume you are better at Ubuntu than I am, so it didn't really say how to use it.
    – Marius
    Sep 12, 2018 at 9:16
  • 1
    You can put the script file wherever you wish, the name and extension doesn't matter. Then run chmod u+x /path/to/scriptfile to make it executable. Then you can run it manually with issuing just /path/to/scriptfile or automatically at every reboot, run crontab -e and add a line @reboot /path/to/scriptfile.
    – pLumo
    Sep 12, 2018 at 9:20
  • I don't believe it is working as intended. I start the script manually with /home/username/rvplayer/whitescreen and it says in the terminal "Setting up watches. Watches established." But if I then edit the file and save it, then the cups service still runs.
    – Marius
    Sep 12, 2018 at 9:34

You must log in to answer this question.

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