Is it OK if while loop runs every 5 - 10 seconds constantly?
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
Is it OK if while loop runs every 5 - 10 seconds constantly?
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: Is it OK if while loop runs every 5 - 10 seconds constan
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.
Re: Is it OK if while loop runs every 5 - 10 seconds constan
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
Edit:
i've just realised, how can this work, if its looping, it will be repeating the backup when its completed
Re: Is it OK if while loop runs every 5 - 10 seconds constan
I have these /media/username mounts:
I can use that here
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.
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)
Code: Select all
if $(cat /proc/mounts | grep external > /dev/null) ; then rysnc stuff here ; else exit; fi
NEEDS loop / sleep timer function. Homework for you!
Should work.
Re: Is it OK if while loop runs every 5 - 10 seconds constan
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.
Re: Is it OK if while loop runs every 5 - 10 seconds constan
This should do the trick. /tmp/$$ could be something even more unique...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.
Code: Select all
function do_work()
{
touch /tmp/$$
rsycn stuff here
rm /tmp/$$
}
if [[ -e /tmp/$$ ]] ; then exit ; else do_work ; fi
Re: Is it OK if while loop runs every 5 - 10 seconds constan
so this is what i did, this includes my error logging method.
the formatting has been slightly disrupted, i dont think it likes Tab's.
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