crontab puzzlement - Solved
David Fletcher
dave at thefletchers.net
Thu Apr 20 10:40:55 UTC 2023
On Mon, 2023-04-03 at 21:22 +0100, Colin Law wrote:
> On Mon, 3 Apr 2023 at 21:11, David Fletcher <dave at thefletchers.net>
> wrote:
> > Hi,
> >
> > I thought I understood how this works, but....
> >
> > Running a 22.04.2 LTS headless server
> >
> > The following two lines are added to /etc/crontab
> > 0,20,40 7-10,16-19 25-30 4 1-5 dave play -qV0 fileA.flac
> > 10,30,50 7-10,16-19 25-30 4 1-5 dave play -qV0 fileB.flac
> >
> > Path to file locations removed to fit on one line
> >
> > Each audio file is 8 minutes or so long and the two play in turn at
> > the
> > designated hours of the day. cron appears to obey the two hours of
> > the
> > day ranges that I have set, so it plays them 07:00-11:00 and 16:00-
> > 20:00,
> >
> > BUT....
> >
> > I was expecting it to not start playing until Tuesday 25th April so
> > why
> > was it playing the sound files today? I thought it was OK to put in
> > day
> > of the month ranges same as hours of the day ranges, but, cron
> > seems to
> > behave here as if it's a * in the day of the month field.
>
> Cron ORs together dom and dow, so if the dom is 25-30 OR dow is 1-5
> then it will play.l
> If you want just 25th to 30th then use * for dow
>
> Colin
Jon LaBadie set me on the correct track, thanks, by suggesting using
the date command in a script.
I am a complete amateur at all this but the thought occurred to me this
morning to look at the date options again for something more useful to
me and found
date +%j
which returns the day of the year which is exactly what I needed all
along! Now I have a very simple script which checks that
[ $(date +%j) -ge $1 ] && [ $(date +%j) -le $2 ]
and $3 alternately plays each of two sound files.
The crontab file now calls the script on weekends with one line for
each sound file and the same at different times of the day on weekdays,
every day of every month. Everything is now far simpler and appears to
be working as intended so I think I am happy with it.
More information about the ubuntu-users
mailing list