LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 01-03-2018, 10:04 AM   #1
businesscat
Member
 
Registered: Jun 2017
Location: Spain
Distribution: RedHat 6.9 /Centos 8
Posts: 42

Rep: Reputation: Disabled
configure crontab for every 2 days?


Hello,

I need to configure a crontab every 2 days, but only obtain every 2nd day of the week/month

Has anyone an idea?

Thanks and happy new year to everyone
 
Old 01-03-2018, 10:39 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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.
 
Old 01-03-2018, 11:10 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by businesscat View Post
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:
  1. Every other day
  2. The second day of each week
  3. The second day of each month
So which is it?
 
Old 01-03-2018, 03:17 PM   #4
eager
Member
 
Registered: Aug 2004
Location: Palo Alto, CA
Distribution: Fedora 8/9, Ubuntu
Posts: 50

Rep: Reputation: 18
Periodic crontab entry

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

*/12 * * * * hello.sh

or alternately

0,5,10,15,20,25,30,35,40,45,50,55 * * * * hello.sh
 
Old 01-03-2018, 04:10 PM   #5
jtison
LQ Newbie
 
Registered: Dec 2005
Location: Connecticut, USA
Distribution: Fedora Cores 9, 10, 15, 17; SLES 10 & 11; RHEL 6.x
Posts: 19

Rep: Reputation: 0
Periodic cron job

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).

HTH.
 
Old 01-03-2018, 04:32 PM   #6
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
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.
Code:
perl -e '$day = int(time()/86400); print $day % 2,"\n"'
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.
 
Old 01-04-2018, 04:43 AM   #7
businesscat
Member
 
Registered: Jun 2017
Location: Spain
Distribution: RedHat 6.9 /Centos 8
Posts: 42

Original Poster
Rep: Reputation: Disabled
By the moment, I configure by the following:


Code:
50 7 */2 * * [execution file]
Every 48h to the 7,50AM hours


Quote:
Originally Posted by TB0ne View Post
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:
  1. Every other day
  2. The second day of each week
  3. The second day of each month
So which is it?
sorry for my ambiguity, I need execute a cron every 48h (2 days).
 
Old 01-06-2018, 12:13 AM   #8
Fjor
LQ Newbie
 
Registered: Oct 2004
Location: Jalisco, Mexico
Distribution: Slackware
Posts: 24

Rep: Reputation: 5
Quote:
Originally Posted by eager View Post
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

*/12 * * * * hello.sh

or alternately

0,5,10,15,20,25,30,35,40,45,50,55 * * * * hello.sh
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''."
 
  


Reply

Tags
crontabs



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 entry to schedule a script every 14 days senrooy Linux - General 18 03-27-2019 12:49 PM
[SOLVED] Crontab and days of the week kikinovak Slackware 5 11-10-2017 05:51 PM
Correct syntax for days in crontab kaplan71 Linux - General 1 03-12-2009 12:46 PM
crontab every 2 days winlinfix Linux - Newbie 4 10-09-2007 09:49 AM
Crontab every 15 days, isn't possible? deqmacrom Linux - General 3 04-07-2005 02:31 AM

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

All times are GMT -5. The time now is 01:42 PM.

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
Open Source Consulting | Domain Registration