Please no three page AI drivel. Please.
On Monday, September 21, 2026 at 5:06:31 PM UTC-7 John Bonzey wrote:
> Claude says the following;
>
> Subject: Re: bash while loop syntax for weewx backup script
>
> Hi,
>
> I think I spotted the issue. Your while condition is:
>
> while [ systemctl -q is-active weewx ];
>
> The problem is that [ ... ] is the "test" command - it doesn't actually
> run systemctl at all. It just checks whether the string "systemctl -q
> is-active weewx" is non-empty, which is always true, so the loop never ends
> based on weewx's real state (it just happens to work when weewx stops
> quickly, since the loop body still runs and eventually you get lucky with
> timing).
>
> The fix is to drop the [ ] brackets entirely and use the command itself as
> the condition. In bash, while just checks the exit status of whatever
> command you give it - it doesn't need [ ] at all unless you're testing a
> string, number, or file:
>
> while systemctl -q is-active weewx;
> do
> sleep 1
> echo $(date): Weewx is still stopping >> /var/log/weewx-backup.log
> done
>
> systemctl -q is-active weewx exits 0 (true) while the service is active,
> and non-zero once it's fully stopped - which is exactly what you want the
> loop to check.
>
> General rule of thumb: use while [ condition ] for string/number/file
> tests, and while some_command when you want to "loop while this command
> succeeds."
>
> Your instinct not to copy the DB mid-write is exactly right, and this fix
> makes that work as intended.
>
> One more suggestion: it's worth adding a timeout guard in case weewx ever
> hangs on stop, so the script can't wait forever. Here's the full script
> with that added:
>
> #!/bin/bash
> #
> # Stop weewx service
> echo $(date): Weewx is stopping >> /var/log/weewx-backup.log
> systemctl stop weewx
> #
> # Check service has stopped (with timeout)
> timeout=30
> count=0
> while systemctl -q is-active weewx;
> do
> if [ $count -ge $timeout ]; then
> echo $(date): Weewx did not stop after ${timeout}s -
> aborting backup >> /var/log/weewx-backup.log
> systemctl start weewx
> exit 1
> fi
> sleep 1
> count=$((count + 1))
> echo $(date): Weewx is still stopping >> /var/log/weewx-backup.log
> done
> echo $(date): Weewx has stopped >> /var/log/weewx-backup.log
> #
> # Copy files to backup folder
> echo $(date): Backup is starting >> /var/log/weewx-backup.log
> tar -czvf /mnt/backup/weewx-conf.tar.gz /etc/weewx
> tar -czvf /mnt/backup/weewx-db.tar.gz /var/lib/weewx/weewx.sdb
> echo $(date): Backup is complete >> /var/log/weewx-backup.log
> #
> # Start weewx service
> systemctl start weewx
> echo $(date): Weewx is starting >> /var/log/weewx-backup.log
>
> With the timeout added, if weewx ever fails to stop within 30 seconds, the
> script restarts weewx, logs the failure, and exits before touching the
> backup files - so a hung stop can't leave weewx down or risk copying a
> database mid-write.
>
> Hope that helps!
> ------------------------------
> *From:* [email protected] <[email protected]> on behalf
> of David Schulz <[email protected]>
> *Sent:* Monday, September 21, 2026 7:22 PM
> *To:* weewx-user <[email protected]>
> *Subject:* [weewx-user] Assistance with While condition for backup script
>
> Hi
> I'm attempting to write my first bash script to backup weewx. I'm
> struggling with the syntax of a condition for a while loop that checks if
> weewx has stopped or is still stopping.
>
> I don't really understand the syntax of the example conditions I can find
> for various tutorials on using while to create a loop. I cannot find
> anywhere an exact example of using 'systemctl is-active' as the condition
> for a while loop.
>
> Appreciate any feedback from people more learned than I.
>
> TIA
>
> Here's what I have at the moment. If I run this script when I know weewx
> won't be writing to the database, the commands to zip the files into the
> backup directory all work OK.
>
> I don't think I want to be copying the database file in the middle of a
> write. So I have a separate script with the backup section removed just
> trying to test the stopping, check has stopped and start parts of the
> script. I've been unsuccessfully futzing around with different condition
> syntax and actively trying to run it when weewx is active and the stop
> command will take a while to execute while the weewx main loop completes.
> But TBH I'm just wandering about in the dark.
>
> ```
> #!/bin/bash
> #
> # Stop weewx service
> echo $(date): Weewx is stopping >> /var/log/weewx-backup.log
> systemctl stop weewx
> #
> # Check service has stopped
> while [ systemctl -q is-active weewx ];
> do
> sleep 1
> echo $(date): Weewx is still stopping >> /var/log/weewx-backup.log
> done
> echo $(date): Weewx has stopped >> /var/log/weewx-backup.log
> #
> # Copy files to backup folder
> echo $(date): Backup is starting >> /var/log/weewx-backup.log
> tar -czvf /mnt/backup/weewx-conf.tar.gz /etc/weewx
> tar -czvf /mnt/backup/weewx-db.tar.gz /var/lib/weewx/weewx.sdb
> echo $(date): Backup is complete >> /var/log/weewx-backup.log
> #
> # Start weewx service
> systemctl start weewx
> echo $(date): Weewx is starting >> /var/log/weewx-backup.log
> ```
>
> --
> You received this message because you are subscribed to the Google Groups
> "weewx-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion visit
> https://groups.google.com/d/msgid/weewx-user/df76a922-ace3-44ae-a65a-0a50b6f8fe92n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/weewx-user/df76a922-ace3-44ae-a65a-0a50b6f8fe92n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/weewx-user/fcc77b81-05a5-420b-93a6-48380b94edf3n%40googlegroups.com.