LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ARRGGG! Frikkin Crontab >:- (( (https://www.linuxquestions.org/questions/linux-newbie-8/arrggg-frikkin-crontab-633672/)

forrestt 04-14-2008 12:05 PM

You are correct. I don't know who decided that those to fields should be combined with an OR, but I'd like to slap them with a herring. If they were combined with an AND what you are wanting would be trivial, and to get an OR would only require two entries (one for the DOM and one for the DOW). The way it was configured results in an impossible situation where a certain numbered weekday (first Tuesday, third Friay, etc.) cannot be described in cron. What you must do is call the script every Monday (for example) and then inside the script determine if the DOM is in the right date range (for example 1-7 for the first or 8-14 for the second week).

HTH

Forrest

matthewg42 04-14-2008 01:54 PM

I think you're right. However, you could get your script to run each Monday using the cron line like this:
Code:

0 9 * * 1 /home/hashbang/my_script.sh
Then you can have this at the start of your script to quit immediately if the day of the month is greater than 7. The effect being that the script will only do what you want it to do on the first Monday of each month:

Code:

#!/bin/bash
[ $(date +%d |sed 's/^0//') -le 7 ] || exit 0

# the rest of your code here

One dis-advantage of this approach (apart from it being a bit ugly) is that the script will not work when invoked manually after the 7th of the month. You can get around this by setting an environment variable in the crontab, and then testing for it when doing the "in the first week" check in the script. Here's how to do the crontab part:
Code:

0 9 * * 1 RUNFROMCRON=yes /home/hashbang/my_script.sh
And the script might look like this:
Code:

#!/bin/bash
if [ "$RUNFROMCRON" = "yes" ]; then
    [ $(date +%d |sed 's/^0//') -le 7 ] || exit 0
fi

# the rest of your code here


colucix 04-14-2008 03:21 PM

Quote:

Originally Posted by matthewg42 (Post 3121088)
One dis-advantage of this approach (apart from it being a bit ugly) is that the script will not work when invoked manually after the 7th of the month.

There are other ways to check if a script is invoked from a terminal or from crontab: for example you can simply check if the standard input is associated with a terminal (script invoked from a terminal) or not (script invoked from crontab)
Code:

tty -s || exit
or
Code:

test -t 0 || exit

matthewg42 04-14-2008 04:10 PM

Quote:

Originally Posted by colucix (Post 3121164)
There are other ways to check if a script is invoked from a terminal or from crontab: for example you can simply check if the standard input is associated with a terminal (script invoked from a terminal) or not (script invoked from crontab)
Code:

tty -s || exit
or
Code:

test -t 0 || exit

...although this might be the case for other reasons, like running with nohup or xargs.

hashbangbinbash 04-15-2008 10:32 AM

Thanks for the responses here, I didn't see them before I settled on this...

Code:

* * * * 1 /home/hasbang/my_script.sh
and my_script.sh being...

Code:

#!/bin/bash
n=$( date +%d );
if [ $n -le 7 ]; then
echo "do the thing";
fi

It is pretty amazing the flexibility of bash, various ways of solving the one problem... really shows up why it's best to learn command line and not just settle for a nice gui.

fatra2 04-15-2008 11:03 AM

Hi there,

Just to add to this discussion.

If you are like me, hashbangbinbash, and can never remember the rules of crontab. I write my cron file, then let "kcron" tell me about the set of rules I applied. Since you are using KDE, you should have access to "kcron" which will give you many details of the cron jobs you have registered.

Cheers


All times are GMT -5. The time now is 01:23 AM.