1
#!/bin/bash
z=1 
b=$(date)    
while [[ $z -eq 1 ]]    
do    
        a=$(date)    
        if [ "$a" == "$b" ]    
        then    
                b=$(date -d "+7 days")    
               rsync -v -e ssh user@ip_address:~/sample.tgz /home/kartik2                
                sleep 1d     
        fi    
done    

I want to rsync a file every week !! But if I start this script on every boot the file will be rsynced every time the system starts !! How to alter the code to satisfy week basis rsync ? ( PS- I don't want to do this through cronjob - school assignment)

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
kartik
  • 55
  • 4

2 Answers2

1

You are talking about having this run for weeks, right? So, we have to take into account that the system will be rebooted and it needs to be run unattended. In short, you need some means of ensuring the script is run at least once every week even when no one is around. The options look like this

Option 1 (worst)
You set a reminder for yourself and you log in every week and run the script. While you may be reliable as a person, this doesn't allow you to go on vacation. Besides, it goes against our principle of "when no one is around".

Option 2 (okay)
You can background the process (./once-a-week.sh &) but this will not reliable over time. Among other things, if the system restarts then your script will not be operating and you won't know.

Option 3 (better)
For this to be reliable over weeks one option is to daemonize the script. For a more detailed discussion on the matter, see: Best way to make a shell script daemon?

You would need to make sure the daemon is started after reboot or system failure. For more discussion on that matter, see: Make daemon start up with Linux

Option 4 (best)
You said no cron but it really is the best option. In particular, it would consume no system resources for the 6 days, 23 hours and 59 minutes when it does not need to running. Additionally, it is naturally resilient to reboots and the like. So, I feel compelled to say that creating a crontab entry like the following would be my top vote: @weekly /full/path/to/script


If you do choose option 2 or 3 above, you will need to make modifications to your script to contain a variable of the week number (date +%V) in which the script last successfully completed its run. The problem is, just having that in memory means that it will not be sustained past reboot.

To make any of the above more resilient, it might be best to create a directory where you can store a file to serve as a semaphore (e.g. week21.txt) or a file to store the state of the last run. Something like once-a-week.state to which you would write a value when run:

date +%V > once-a-week.state # write the week number to a file

Then to read the value, you would:

file="/path/to/once-a-week.state" # the file where the week number is stored
read -d $'\x04' name < "$file"
echo "$name"

You would then need to check to see if the week number matched this present week number and handle the needed action based on match or not.

Community
  • 1
  • 1
John Mark Mitchell
  • 4,522
  • 4
  • 28
  • 29
1
#!/bin/bash
z=1
b=$(cat f1.txt)
while [[ $z -eq 1 ]]
do
        a=$(date +"%d-%m-%y")
        if [ "$a" == "$b" ] || [ "$b" == "" ] || [$a -ge $b ]
        then
                b=$(date +"%d-%m-%y" -d "+7 days")
                echo $b > f1.txt
               rsync -v -e ssh HOST@ip:~/sample.tgz /home/user
                if [ $? -eq 0 ]
                then
                        sleep 1d
                fi
        fi
done

This code seems to works well and good !! Any changes to it let me know

kartik
  • 55
  • 4