Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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).
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:
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)
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.