LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-14-2008, 01:05 PM   #16
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99

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
 
Old 04-14-2008, 02:54 PM   #17
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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
 
Old 04-14-2008, 04:21 PM   #18
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by matthewg42 View Post
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
 
Old 04-14-2008, 05:10 PM   #19
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by colucix View Post
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.
 
Old 04-15-2008, 11:32 AM   #20
hashbangbinbash
Member
 
Registered: Mar 2008
Posts: 108

Original Poster
Rep: Reputation: 16
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.
 
Old 04-15-2008, 12:03 PM   #21
fatra2
Member
 
Registered: Feb 2004
Location: Switzerland
Distribution: Fedora - RedHat - Mandrake - OpenSuse
Posts: 216

Rep: Reputation: 35
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Crontab kalinchuk Linux - Networking 1 02-22-2006 10:59 AM
crontab -e hoover93 Linux - Newbie 2 08-06-2004 05:05 PM
knoppix wont burn..read error...arrggg virtual_rose Linux - Software 4 01-22-2004 11:33 PM
system-wide crontab in /etc/crontab ner Linux - General 2 11-18-2003 01:35 PM
Crontab Help dvong3 Linux - General 2 08-27-2003 02:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration