How to Run Crontab Job Every Week on Sunday

How to run crontab job every week on Sunday

Here is an explanation of the crontab format.

# 1. Entry: Minute when the process will be started [0-60]
# 2. Entry: Hour when the process will be started [0-23]
# 3. Entry: Day of the month when the process will be started [1-28/29/30/31]
# 4. Entry: Month of the year when the process will be started [1-12]
# 5. Entry: Weekday when the process will be started [0-6] [0 is Sunday]
#
# all x min = */x

So according to this your 5 8 * * 0 would run 8:05 every Sunday.

Run CRON job everyday at specific time

Cron utility is an effective way to schedule a routine background job at a specific time and/or day on an on-going basis.

Linux Crontab Format

MIN HOUR DOM MON DOW CMD

Sample Image

Example::Scheduling a Job For a Specific Time

The basic usage of cron is to execute a job in a specific time as shown below. This will execute the Full backup shell script (full-backup) on 10th June 08:30 AM.

Please note that the time field uses 24 hours format. So, for 8 AM use
8, and for 8 PM use 20.

30 08 10 06 * /home/yourname/full-backup
  • 30 – 30th Minute
  • 08 – 08 AM
  • 10 – 10th Day
  • 06 – 6th Month (June)
  • *– Every day of the week

In your case, for 2.30PM,

30 14 * * * YOURCMD
  1. 30 – 30th Minute
  2. 14 – 2PM
  3. *– Every day
  4. *– Every month
  5. *– Every day of the week

To know more about cron, visit this website.

Running a cron job at 2:30 AM everyday


crontab -e

add:

30 2 * * * /your/command

Cron Job to run on a range of days only on a particular day of week

This is the crontab format:

* * * * *
| | | | |
| | | | +---- Day of the Week (range: 0-6, 0 standing for Sunday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)

Ubuntu man 5 crontab says:

  field          allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

So, this should work for you:

0 0 2-31 * 0         /home/ubuntu/x.h
0 0 2-31 * 1-6 /home/ubuntu/y.h

I'm not sure why 7 would run on Saturday--is your system time accurate and in the right timezone?

Edit: Ah, yes, unfortunately you cannot specify both the day of the week and the day of the month. From man 5 crontab:

Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

So, the answer is:

0 0 2-31 * *       test $(date +\%u) -eq 7 && /home/ubuntu/x.h
0 0 2-31 * * test $(date +\%u) -ne 7 && /home/ubuntu/y.h

$(date '+%u') returns 1-7 representing Monday thru Sunday. Try echo $(date '+%u') for an example.

Crontab Day of the Week syntax

0 and 7 both stand for Sunday, you can use the one you want, so writing 0-6 or 1-7 has the same result.

Also, as suggested by @Henrik, it is possible to replace numbers by shortened name of days, such as MON, THU, etc:

0 - Sun      Sunday
1 - Mon Monday
2 - Tue Tuesday
3 - Wed Wednesday
4 - Thu Thursday
5 - Fri Friday
6 - Sat Saturday
7 - Sun Sunday

Graphically, * * * * * command to be executed stands for:































minutehourday of monthmonthday of week
(0-59)(0-23)(1-31)(1-12)(1-7)
*****command to be executed

Crontab skip run once a week

Make it 2 lines:

0 0,8,16 * * 0-5 At minute 0 past hour 0, 8, and 16 on every day-of-week from Sunday through Friday.

And

0 8,16 * * 6 At minute 0 past hour 8 and 16 on Saturday.

You can change the day and hour which you want to skip, but there is no way to do this in 1 line as far as I know.

how to create a cron expression for every 2 weeks

There is no direct cron expression for every 2 weeks but I kept following cron expression , which is similar to 2 weeks but not exactly for 2 weeks

cron for every 2weeks(on 1st and 15th of every month at 1:30AM) - 30 1 1,15 * *



Related Topics



Leave a reply



Submit