Score:2

Watch Command Issue, Bash Script Issue or System Issue?

in flag

I have written a simple bash script named write-date.sh which simply writes the date to a file:

#!/bin/bash

echo "$(date) Write Done" >> write-date.log

And I am running the bash script every 60 seconds like this:

watch -n 60 ./write-date.sh

However when I run the command, the output produced in the write-date.log file contains output like in the snippet here:

Thu  1 Jul 09:42:03 BST 2021 Write Done
Thu  1 Jul 09:42:03 BST 2021 Write Done
Thu  1 Jul 09:42:03 BST 2021 Write Done
Thu  1 Jul 09:42:03 BST 2021 Write Done
Thu  1 Jul 09:42:03 BST 2021 Write Done
Thu  1 Jul 09:42:04 BST 2021 Write Done
Thu  1 Jul 09:42:04 BST 2021 Write Done
Thu  1 Jul 09:42:04 BST 2021 Write Done
Thu  1 Jul 09:42:04 BST 2021 Write Done
Thu  1 Jul 09:42:04 BST 2021 Write Done
Thu  1 Jul 09:42:04 BST 2021 Write Done
Thu  1 Jul 09:42:04 BST 2021 Write Done
Thu  1 Jul 09:42:04 BST 2021 Write Done
Thu  1 Jul 09:42:04 BST 2021 Write Done
Thu  1 Jul 09:43:04 BST 2021 Write Done
Thu  1 Jul 09:43:26 BST 2021 Write Done
Thu  1 Jul 09:44:26 BST 2021 Write Done
Thu  1 Jul 09:45:26 BST 2021 Write Done
Thu  1 Jul 09:46:26 BST 2021 Write Done
Thu  1 Jul 09:47:26 BST 2021 Write Done
Thu  1 Jul 09:48:26 BST 2021 Write Done

As can be seen, there are times where the date is written to the log file more than once per minute (at 09:42 and 09:43 in this sample snippet) and I can't see why this might be... is the watch command wrong? Is the bash script itself wrong? Is there a system clock issue somewhere?

The os is Ubuntu 20.04 where uname -a is:

Linux machine 5.4.0-77-generic #86-Ubuntu SMP Thu Jun 17 02:35:03 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

I should also point out that the reason for this script is that I am having an issue where the os' root filesystem goes into read-only mode every few hours or sometimes every few days and so I am trying to find out the day and time when this happens so I can later look in /var/log/syslog around that time to see if anything odd had happened to the system at said time.

--- UPDATE ---

Have stopped running command and cleared log file and re-run command again from scratch and now the date is logged once per minute as one would expect.

As pointed out in comments, the duplicate logs printed may have been from a previous dodgy script run (probably user error) but I can't think how or when this might have happened. Will keep running for hours/days anyway so will keep an eye on it.

muru avatar
us flag
You're **appending** (`>>`) to the log. Are you sure those entries before `09:43:26` weren't already there before you started `watch`? There is no `09:23` in the sample.
Going Bananas avatar
in flag
@muru Sorry, the 09:23 was a typo which should have said 09:42 instead. I'll edit the post to correct the typo... Also, I have since deleted the file and re-run from scratch and now it is writing only once per sec as one would expect so maybe it was stale data from a previous dodgy script run but I can't think when that could have happened but it does point to user error. I'll keep running different runs for a while see if issue re-occurs. will add this detail in post too.
muru avatar
us flag
Wait, it's still logging once per **sec**? Not once a minute?
Going Bananas avatar
in flag
Sorry @muru. Another typo. I meant to write once per minute. I'll correct in post...
FedKad avatar
cn flag
You are probably running multiple copies of the script (probably in background or another terminal). I would do `ps -ef | grep my_script_name` and `kill` any running version, before starting a new one.
Going Bananas avatar
in flag
@FedonKadifeli I had checked that as a possibility at the time but there was just the one script running. Also, if there was n scripts running then each written date would appear exactly n times which was not the case as can be seen in the log snippet posted
FedKad avatar
cn flag
A correction, sorry: I would kill any `watch` process, not just a process with your script name...
FedKad avatar
cn flag
Another possibility is that you entered '0' instead of '60'. In that case `watch` will use a delay of 0.1 seconds, which explains why you see ten entries per second in your output file. Please look at your command `history` of Bash to verify.
Going Bananas avatar
in flag
The thing is that it was only sometimes it was writing data to file more than once per minute during the same script run.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.