SyncingFeeling
Posts: 45
Joined: Thu Jan 03, 2013 9:15 pm

Want to run a cron job at reboot. Sometimes.

Sat Apr 06, 2013 1:47 pm

I have set up cron to do a job at reboot, using @reboot. It makes a backup of my SD card to the external hard drive. It's already saved me the bother of starting from zero several times.

The problem is I don't want it to run at *every* reboot. I just want it to reboot once a week, and only then execute the backup (because it takes 7 hours and all the CPU to complete).

So, how can I get it to run at the scheduled reboot, but not every other reboot?

jamiesk
Posts: 95
Joined: Mon Nov 26, 2012 8:48 pm
Location: Ipswich, Suffolk, England, UK.

Re: Want to run a cron job at reboot. Sometimes.

Sat Apr 06, 2013 4:47 pm

I would create a file via the weekly cron, then reboot the pi. The startup script checks for the existance of the file (dont create it in /tmp !!!) then do the backup. The script has to then remove the file after the backup has completed.

This way if the file has not been created and it "just" a reboot then it wont kick of the backup.
Pi1 (Nov 2012 loft)= 1KW immersion controller for Solar panel
Pi2 (Jan 2013 living room)=Play thing
Pi3 (Feb 2013 mobile)= Play thing with Tandy Ladder board,breakout board,Nokia display
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=28193

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Want to run a cron job at reboot. Sometimes.

Sat Apr 06, 2013 5:03 pm

Or you could key it on the backup being "too old" - i.e., if the backup is more than a week old, it needs to be refreshed (you may have to play with the exact definition of "a week old").

So, in your backup script, you would do something like:

# Exit now if the backup file exists and is less than 7 days old
find -name mybackup.gz -mtime -7 | grep . && exit 0
# Else, go ahead and (re-) create it.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

SyncingFeeling
Posts: 45
Joined: Thu Jan 03, 2013 9:15 pm

Re: Want to run a cron job at reboot. Sometimes.

Sat Apr 06, 2013 7:33 pm

Hi joe, I like that option.

Just to be clear, what is "-name mybackup.gz"? This is the name of the SD card image file, yes? So this is going to be a .img file. Or is the .gz file something I have to create? Thanks again.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Want to run a cron job at reboot. Sometimes.

Sat Apr 06, 2013 9:11 pm

SyncingFeeling wrote:Hi joe, I like that option.

Just to be clear, what is "-name mybackup.gz"? This is the name of the SD card image file, yes? So this is going to be a .img file. Or is the .gz file something I have to create? Thanks again.
Right. "mybackup.gz" would be whatever file your backup process generates. I sort of assumed that it would be compressed - as these things usually are. Hence the guess at the .gz extension.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

SyncingFeeling
Posts: 45
Joined: Thu Jan 03, 2013 9:15 pm

Re: Want to run a cron job at reboot. Sometimes.

Sun Apr 07, 2013 11:38 am

Hmm, it's not working. I did what you suggested but it's still running every single reboot.

Here is the .sh file:
# exit now if backup is recent
find -/media/xdd/SDcardbackups/raspberryPiSDCardBackup.img -mtime -6 | grep . && exit 0

# if not, re-create
sudo /etc/init.d/cron stop &
wait $!
sudo dd if=/dev/mmcblk0 of=/media/xdd/SDcardbackups/raspberryPiSDCardBackup.img &
wait $!
sudo /etc/init.d/cron start
exit 0
===================

It still does what it did before, the backup at every reboot.

If you are wondering, I want it to execute the backup at reboot, because I know if it rebooted successfully then the card is not corrupted and is capable of booting.

What am I doing wrong here?

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Want to run a cron job at reboot. Sometimes.

Sun Apr 07, 2013 12:24 pm

What am I doing wrong here?
The short answer is: You didn't do "man find".

"find" is a very powerful, but quite persnickety Unix command. At least one well-respected poster on this board says that he never uses "find" without looking at the man page first.

In your case, I think the syntax you want is:

find -/media/xdd/SDcardbackups -name raspberryPiSDCardBackup.img -mtime -6 ...

Notes:
1) You must use "-name" (at some point).
2) The official find syntax is:
find {dirs} {options}
where the {dirs} are the directories to look in (at least one must be given). But the GNU version of "find" allows you to not specify any directories, in which case, it defaults to looking in the current directory. It turns out that in real life, this is usually what you want.

So, your script could be made slightly more efficeint by doing it like:

cd -/media/xdd/SDcardbackups
find -name raspberryPiSDCardBackup.img -mtime -6 ...
dd if=... of=raspberryPiSDCardBackup.img ...

BTW, you might want to thrown in a "-quit" as well. Again, "man find" is your friend.

And, finally, it did occur to me that "find" is kinda overkill for this. You could certainly whip something up in your-favorite-scripting-language (AWK/Perl/Python/Ruby/Haskell/whatever) if you were so inclined. But, "find" is reasonably universal, once you get used to it...
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
RichardBronosky
Posts: 82
Joined: Thu Feb 14, 2013 5:38 pm
Location: Atlanta, GA, USA

Re: Want to run a cron job at reboot. Sometimes.

Mon Apr 08, 2013 12:49 pm

Joe Schmoe wrote: And, finally, it did occur to me that "find" is kinda overkill for this. You could certainly whip something up in your-favorite-scripting-language (AWK/Perl/Python/Ruby/Haskell/whatever) if you were so inclined. But, "find" is reasonably universal, once you get used to it...
Genuine LOL:
Find is overkill, try Haskell. You slayed me. Well played, sir.
-- Bruno Bronosky
1x RPi 1 (B)
1x RPi 1+ (B)
2x RPi 2
11x RPi 3
4x RPi Zero W
2x RPi camera module
3x AIY Voice kits
https://github.com/RichardBronosky/pi-gen-extender
https://github.com/RichardBronosky/raspbian-boot-setup

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Want to run a cron job at reboot. Sometimes.

Mon Apr 08, 2013 1:33 pm

heh heh...
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

SyncingFeeling
Posts: 45
Joined: Thu Jan 03, 2013 9:15 pm

Re: Want to run a cron job at reboot. Sometimes.

Thu Apr 11, 2013 4:01 pm

That worked, thanks!

Return to “Raspbian”