Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

weird cron problem 2

Status
Not open for further replies.

signalsys

Technical User
Sep 5, 2005
44
CN

I once had place the following entry in crontab, I only want it to be automatically executed on August 14th 10:23 am.
23 10 14 8 1 date >> dat.out
But after a week, On August 21th also Monday it was run again. it seem that cron neglect the month and day in cron. Why could this happen ?

A similiar problem also occurs, if i add the following entry in crontab:
51 10 2-31 * 2 date >> dat.out
But it runs on 2nd-31th every month regardless of weekday.

 
Hi Signalsys,

This is how it should run because of the condition specified by the month and day of the week as said below:

Code:
The crontab File Entry Format
A crontab file contains entries for each cron job. Entries are separated by newline characters. Each crontab file entry contains six fields separated by spaces or tabs in the following form:

 
minute  hour  day_of_month  month  weekday  command
These fields accept the following values:

minute 0 through 59 
hour 0 through 23 
day_of_month 1 through 31 
month 1 through 12 
weekday 0 through 6 for Sunday through Saturday 
command a shell command 

You must specify a value for each field. Except for the command field, these fields can contain the following:

A number in the specified range. To run a command in May, specify 5 in the month field. 
Two numbers separated by a dash to indicate an inclusive range. To run a cron job on Tuesday through Friday, place 2-5 in the weekday field. 
A list of numbers separated by commas. To run a command on the first and last day of January, you would specify 1,31 in the day_of_month field. 
An * (asterisk), meaning all allowed values. To run a job every hour, specify an asterisk in the hour field.
Note: Any character preceeded by a backslash (including the %) causes that character to be treated literally. The specification of days may be made by two fields (day of the month and day of the week). If you specify both as a list of elements, both are adhered to. For example, the following entry: 
0 0 1,15 * 1 command
Code:
would run command on the first and fifteenth days of each month, as well as every Monday. To specify days by only one field, the other field should contain an * .

view this man page on

For the second case it will run from the 2nd to the 31th so the weekday, whether true or false, won't make a difference!


Regards,
Khalid
 
Khalid (and man page) is right.

Note: to run a program only once, you can schedule it to run at a specific time with the at command:

echo "date >> dat.out" | at 10:23 am august 14

To use cron to run a program at a specifc weekday in a certail period of the month, you need to check for the weekday in the script, because the date fields and weekday field in a crontab entry are logically OR-ed

So the first step in a script that you want run on a tuesday from the 2nd until the 31st needs to be

WDAY=$(date +'%w')
if [ ${WDAY} -ne 2 ]
then
exit
fi


HTH,

p5wizard
 
Hi,p5wizard and khalidaaa
Thanks for your helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top