LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-14-2009, 04:48 AM   #1
senrooy
LQ Newbie
 
Registered: Feb 2009
Posts: 20

Rep: Reputation: 0
Thumbs up Crontab entry to schedule a script every 14 days


I need to schedule a script through crontab so that it runs every 14 days.
Can anyone tell me what should be the crontab entry for doing this ?
 
Old 05-14-2009, 07:31 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
not sure if there is a nice crontab way of doing it, but you could have it run every 7 days and your script could just quit every other time instead of doing work
 
Old 05-14-2009, 07:42 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Something like this?

00 05 * * 1 [[ $(expr `date +%W` % 2) = 0 ]] && /path/to/script.sh

This will run every week (1 -> first day of the week) at 05:00, checks if the week is odd or even. If it is even => run script. If not -> do nothing.

Hope this helps.

--- edit ---

Take a look at colucix reply (#4), the % needs to be escaped!!

Correct command should be: 00 05 * * 1 [[ $(expr `date +\%W` \% 2) = 0 ]] && /path/to/script.sh

--- /edit ---

Last edited by druuna; 05-14-2009 at 08:08 AM. Reason: Command given is not entirely correct
 
Old 05-14-2009, 07:56 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
druuna, you have to escape the % signs in the crontab entry, otherwise they are interpreted as newline characters and all data after the first % are sent to the command as standard input. This is a special behavior of crontab.
Code:
00 05 * * 1 [[ $(expr `date +\%W` \% 2) = 0 ]] && /path/to/script.sh
 
Old 05-14-2009, 08:04 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@colucix: Of course!! Forgot all about that.
 
Old 05-20-2009, 04:00 AM   #6
senrooy
LQ Newbie
 
Registered: Feb 2009
Posts: 20

Original Poster
Rep: Reputation: 0
According to the requirement the schedule date should not be date specific i.e. on14th and 28th for example. It should run after every 14 days. I have tried with the above command. But it won't work as per requirement
 
Old 05-20-2009, 04:17 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I guess I'm not sure I understand the requirements.

The command given is not date specific, it runs every 7 days (and checks if the week is odd/even to make it every 14 days), in this specific example it always runs on the first day of the week (1 = monday). Without looking at my agenda I don't have a clue what mondays date is or the one that is 14(+) days ahead. After the fact you could make a schedule and get the dates it runs on, and you will get a nice stat that shows dates, 14 days apart. It seems to me that is what you want/asked for.

Please elaborate on the requirements.
 
Old 05-20-2009, 05:30 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In alternative you can try the at command, in place of crontab. To execute the same script after 14 days, try the following code at the end of your script:
Code:
at now + 14 days << EOF
$(readlink -f $0)
EOF
the command readlink -f $0 gives the full path of the script itself.
 
Old 05-20-2009, 05:38 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,592

Rep: Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880
How about just:
0 5 */14 * * /your/command/

Should run every 14 days regardless of a specific date.
 
1 members found this post helpful.
Old 05-20-2009, 05:40 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@colucix: Nice, I love self-writing code

There is one possible downside to this: Even if the script runs for a short while (say minutes), this will create a sliding window; Every next occurence will run 14 days and a few minutes (maybe hours?) later then the previous one.

Maybe you should place that code snippet in the beginning of the script instead of the end. The same 'problem' is still in effect, but we are talking about a second or so, which should keep you safe for a long while.
 
Old 05-20-2009, 06:15 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by druuna View Post
Maybe you should place that code snippet in the beginning of the script instead of the end. The same 'problem' is still in effect, but we are talking about a second or so, which should keep you safe for a long while.
You're right, druuna! Thank you for the clarification.
 
Old 05-26-2009, 06:50 AM   #12
senrooy
LQ Newbie
 
Registered: Feb 2009
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks a lot to you all.That command worked for me after some modifications.
 
Old 06-03-2009, 08:54 AM   #13
sureshk.ganesan
LQ Newbie
 
Registered: Jun 2009
Posts: 1

Rep: Reputation: 0
Hi Senrooy,
I too need the same thing which you have done. can you please post the script which you have done to schedule a job in crontab to run every 14 days.
Thanks in advance
 
Old 03-13-2019, 03:09 PM   #14
jamtat
Member
 
Registered: Oct 2004
Distribution: Debian/Ubuntu, Arch, Gentoo, Void
Posts: 138

Rep: Reputation: 24
Ok, I know this is a really old question but I'm trying to better understand the workings of the evaluation being done. Maybe rehashing it a bit will prove of use to someone searching for information on how to accomplish this sort of aim.

Some tests reveal that the evaluation logic will work for cron jobs that are supposed to run in even-numbered weeks (as calculated from the start of the year), as stipulated in one of the answers given and as the following seems to reveal:
Code:
[ $(expr `date +%W` % 2) = 0 ] && echo yes
The word "yes" will appear after running this command if it is run during an even-numbered week.

If it is desired for the cron job to run in odd-numbered weeks, then
Code:
[ $(expr `date +%W` % 2) = 1 ] && echo yes
seems the right evaluation logic to use. Have I understood correctly how this works?

If so, a sample cron entry that should run some command every two weeks in odd-numbered weeks would look like this:
Code:
00 05 * * 1 [[ $(expr `date +\%W` \% 2) = 1 ]] && /path/to/script.sh
Further input on this, anyone?

Last edited by jamtat; 03-13-2019 at 03:16 PM.
 
Old 03-13-2019, 03:39 PM   #15
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,770

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Quote:
Originally Posted by jamtat View Post
Code:
[ $(expr `date +%W` % 2) = 0 ] && echo yes
The word "yes" will appear after running this command if it is run during an even-numbered week.

If it is desired for the cron job to run in odd-numbered weeks, then
Code:
[ $(expr `date +%W` % 2) = 1 ] && echo yes
seems the right evaluation logic to use.
Those will still glitch at the beginning of each year.

If the goal is really "every 14 days" and not any particular day of the week, then
Code:
[ $((($(date +%s)/86400) % 14)) = 0 ] && echo yes
That computes the integer number of days since the epoch, modulo 14. That "0" could be any number {0..13}. (Yes, "date +%s" is a GNU extension.)
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using 'AT' and 'CRONTAB' to Schedule Without User Input Jesdisciple Programming 20 03-22-2008 08:53 PM
crontab every 2 days winlinfix Linux - Newbie 4 10-09-2007 09:49 AM
schedule a crontab rupesh_pulikool Solaris / OpenSolaris 7 09-04-2004 09:32 PM
crontab schedule help onlykims Linux - Newbie 2 08-28-2004 12:09 PM
Schedule TOP command in Crontab. imsajjadali Red Hat 2 05-20-2004 04:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:16 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
Open Source Consulting | Domain Registration