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.
I would run in every day and create a filter to skip every second day or whatever you want.
You can use the command date (see format flags) to get day of week, day of month or anything you need.
Hello,
I need to configure a crontab every 2 days, but only obtain every 2nd day of the week/month Has anyone an idea?
Your question is ambiguous.
Your subject line is "configure crontab for every 2 days"...your question then asks to run it every 2nd day of the week/month. That describes THREE different scenarios, either:
Some versions of crontab allow you to put something like */2 or */3 to have the action taken ever two or three (in these examples) of whatever unit you are selecting. If you wanted to run "hello.sh" every 5 minutes, you could have a line in crontab like
To amplify on what eager said, the third field is "day of month". You could put */2 in that field, knowing that on a transition from a 31-day (or 29-day) month to the next, you won't get an execution until the third day (the 2nd of the next month) after the 30th.
Unfortunately, you can't use any value higher than 23 in the "hour of day" (the second) field, so that won't help you.
If it were me, I'd live with the three-day gap at the end of certain months just to make it easier on myself.
If I were interested in getting a tiny bit more complex and executing every second day, no matter which month transition is pending, I'd write a script that takes the Julian day of year, modulos it by two, and executes the job when the result is one. Execute that script at a fixed time of day every day in your crontab. The only irregularity you'll have to deal with is at end of year, when your job will run on subsequent days (except leap years).
What jtison said. But if you really want every 2 days even across year boundaries then this will print 0 on even days and 1 on odd days. Add an offset of half a day (43200) to time() if you run it close to midnight and are worried about leap seconds.
Another way to make odd time-dependent decisions is to touch a file when you run your command. Then check the file modification time using stat to see if it's time to run again.
Your subject line is "configure crontab for every 2 days"...your question then asks to run it every 2nd day of the week/month. That describes THREE different scenarios, either:
Every other day
The second day of each week
The second day of each month
So which is it?
sorry for my ambiguity, I need execute a cron every 48h (2 days).
Some versions of crontab allow you to put something like */2 or */3 to have the action taken ever two or three (in these examples) of whatever unit you are selecting. If you wanted to run "hello.sh" every 5 minutes, you could have a line in crontab like
According to the ISC cron manual (consulted using man 5 cron), the number x in */x indicates the number of steps skipped in the corresponding field. In the case depicted by eager, */12 in the minutes field will cause the action taken every 12 minutes, not five as stated, in this cron. Same way, analyzing the jtison's post, */2 in the day field is the same as 1-31/2, or "1,3,5...,29,31", so won't be two-day skipping at 31st of the month in this version of cron, i.e, go at 31st, then go at 1st of the next month. Conclusion: man is your friend, as always.
Quoting the man section referred to:
Quote:
The time and date fields are:
field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)
A field may be an asterisk (*), which always stands for ``first-last''.
(...)
Step values can be used in conjunction with ranges. Following a range with ``/<number>'' speci‐
fies skips of the number's value through the range. For example, ``0-23/2'' can be used in the
hours field to specify command execution every other hour (the alternative in the V7 standard is
``0,2,4,6,8,10,12,14,16,18,20,22''). Steps are also permitted after an asterisk, so if you want
to say ``every two hours'', just use ``*/2''."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.