Automating a script on a timely basis using Crontab. How?

Status
Not open for further replies.

Jayce

Fully Optimized
Messages
3,056
Location
/home/jason
I have a script located in /usr/local/bin called "backup."

It's simply an rsync script.

I'd like to set it up in crontab to run several times a day. Say I wanted to run it twice a day. How would I do that? I ran man crontab and read up on it but I still seem to be a little confused in this department.

Any help?
 
Thank you. If possible, could you explain a little bit more about that? I'm just curious if I wanted to set up my own custom time what I would be looking at in terms of adjusting the hours/minutes/whatever...

I'm just not a huge fan of "here, paste this, save it, you're good." I'd rather understand what I'm doing before I do it. :)
 
I'm no cron expert and the manual for crontab would be the best place to go but I'll give you a rundown of what I have found practical (I assume vixie cron on linux).
Jobs are listed as rows where;
column 1 is minutes in the hour 0-59 (I usually try to offset jobs rather than starting them all up on the hour but I think that cron has the ability to do this itself).
Column 2 is hours in the day 0-23.
Column 3 is day of the month 1-31, column 4 is month of the year 1-12 and column 5 is day of the week 0-7.
Column 6 takes up the rest of the row and is the command to run.
The command in column 6 is run when ALL of the time columns in that row are met EXCEPT the days fields (either day will cause the job to be run).

',' (without quotes) can be used to list ranges and values

r-r in any of the time fields means all values inclusive of r

* in any of the time fields is similar here to its use on the shell, basically it fills all available values for the field

/n can go after either r-r or * but it is not a denominator; it means run the command every (time interval of field * n) starting with the lowest value of your range. I only ever use this in the hours but I think it could go anywhere

@reboot in column 1 just put this and the command on the row and your command will run when the machine boots up (very useful)

Things to watch out for;
1) two days. day of the month and day of the week either will cause execution
2) columns 1 and 2 are 0 based, columns 2 and 3 are 1 based and column 5 can represent 8 values when there are only 7 days of the week (0 and 7 are Sunday)

Personally I use @reboot, the minutes to offset commands, hours r-r/n and days of week and when I want something to run when I am not available to run it eg. middle of the night I use the month field although I'm pretty sure there must be a better way than this.
I don't use words in the day of week and month (I prefer to stick to numbers) right the way through and I don't use day of the month.

Hope this helps and also while I remember, do `man 5 crontab`
 
Status
Not open for further replies.
Back
Top Bottom