Is it OK if while loop runs every 5 - 10 seconds constantly?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
BlackVeils

Is it OK if while loop runs every 5 - 10 seconds constantly?

Post by BlackVeils »

all it will do is check for a folder in /media/username, for my encrypted USB flash drive, to then do my usual rsync backup. i havent actually thought about the script yet, but thats the general idea.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: Is it OK if while loop runs every 5 - 10 seconds constan

Post by xenopeek »

That's fine. You can also use the inotifywait command to wait for specific filesystem events in a certain directory. That way your bash script waits till a certain event occurs in a directory; no need to loop with a sleep. Just loop with a wait for an event (like a directory being created as mount point). Or you could write an udev rule for it.
Image
BlackVeils

Re: Is it OK if while loop runs every 5 - 10 seconds constan

Post by BlackVeils »

thanks for the useful information.

Edit:
i've just realised, how can this work, if its looping, it will be repeating the backup when its completed
Habitual

Re: Is it OK if while loop runs every 5 - 10 seconds constan

Post by Habitual »

I have these /media/username mounts:

Code: Select all

/dev/sdb1 on /media/jj/internal type ext4 (rw,nosuid,nodev,uhelper=udisks2)
/dev/sdc1 on /media/jj/external type ext4 (rw,nosuid,nodev,uhelper=udisks2)
I can use that here

Code: Select all

if $(cat /proc/mounts | grep external  > /dev/null) ; then rysnc stuff here ; else exit; fi
That in a script could run often as you wish and if external is NOT connected, it exits.
NEEDS loop / sleep timer function. Homework for you!
Should work.
BlackVeils

Re: Is it OK if while loop runs every 5 - 10 seconds constan

Post by BlackVeils »

with the if statement in a while loop, the backup will be stuck on repeat while the device is mounted. the only way i can see to stop that would be to unmount at the end of backup. the disk was encrypted using gnome-disk-utility.
Habitual

Re: Is it OK if while loop runs every 5 - 10 seconds constan

Post by Habitual »

BlackVeils wrote:with the if statement in a while loop, the backup will be stuck on repeat while the device is mounted. the only way i can see to stop that would be to unmount at the end of backup. the disk was encrypted using gnome-disk-utility.
This should do the trick. /tmp/$$ could be something even more unique...

Code: Select all

function do_work()
{
	touch /tmp/$$
	rsycn stuff here
	rm /tmp/$$	
}

if [[ -e /tmp/$$  ]]  ; then exit ; else do_work ; fi
BlackVeils

Re: Is it OK if while loop runs every 5 - 10 seconds constan

Post by BlackVeils »

so this is what i did, this includes my error logging method.

Code: Select all

while true; do
  
  if [ -e "/media/vicky/Backup Flash 2" ]   # Check if my USB flash drive is mounted
  then

         ERR_LOG='/home/vicky/.Local-Backup-ERRORS.log'

        {
           rsync script commands   # The original contents of my script

        } 2> "$ERR_LOG"; size=$(wc -c "$ERR_LOG" | cut -d" " -f1)   # Error logging for selected area
	if [ "$size" -eq "0" ]   # If there are no errors
	then
	       my commands
	       while true; do
	       if [ ! -e "/media/vicky/Backup Flash 2" ]   # Check if it is unmounted
	      then
		     break   # If it is unmounted, break from this loop, and continue script
	      fi
	      sleep 30; done   # Check every 30 seconds for the USB flash

	 else
	       sed -i -e '1i\====  Error during data backup  ====\' "$ERR_LOG"   # Move first line down in log, then add info above
	       sleep 3
	       nohup gedit "$ERR_LOG" > /dev/null 2>&1 &    # Open log file with this program if its installed
	       nohup pluma "$ERR_LOG" > /dev/null 2>&1 &  # Open log file with this program if its installed
	       sleep 5   # Need this
	       while true; do
	       if [ ! -e "/media/vicky/Backup Flash 2" ]   # Check if it is unmounted
	       then
		     break   # If it is unmounted, break from this loop
	       fi
	       sleep 30; done   # Check every 30 seconds for the USB flash

	 fi

  fi

sleep 5; done
the formatting has been slightly disrupted, i dont think it likes Tab's.
Locked

Return to “Scripts & Bash”