LinuxQuestions.org
Help answer threads with 0 replies.
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-26-2010, 01:30 PM   #1
RIPNHL2005
LQ Newbie
 
Registered: Jan 2010
Posts: 4

Rep: Reputation: 0
Crontab configuration question


Hello,

I have been tasked with rebooting all our Linux Fedora 9 servers on the 2nd Sunday of every month at 8:15am.
Since there was no way to specify 2nd Sunday in 1 configuration line I created 1 line for each month specifying the day of the month and month that the 2nd Sunday falls on each month.

Is this correct and will it work like this, bypassing each line that doesn't apply and moving down the list to a line that does apply?

# monthly reboot
#
15 8 14 2 * /sbin/shutdown -r now
15 8 14 3 * /sbin/shutdown -r now
15 8 11 4 * /sbin/shutdown -r now
15 8 9 5 * /sbin/shutdown -r now
15 8 13 6 * /sbin/shutdown -r now
15 8 11 7 * /sbin/shutdown -r now
15 8 8 8 * /sbin/shutdown -r now
15 8 12 9 * /sbin/shutdown -r now
15 8 10 10 * /sbin/shutdown -r now
15 8 14 11 * /sbin/shutdown -r now
15 8 12 12 * /sbin/shutdown -r now

~
~
"/tmp/crontab.XXXXS66Ltl" 15L, 396C


Thanks in advance!

Patrick
 
Old 01-26-2010, 02:16 PM   #2
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
Solved With DB Example

I suggest that you have one entry that launches itself every 0th day (Sunday). That program would run as root and encapsulate the shutdown. That program would make some decisions based on the day of the week. If it is Sunday and the second Sunday of the month, then reboot the system.

The following is very crude, but it works. I'm sure if you Google, you'll find something better, but this is what I used. You could substitute the database table with a file.

My problem to be solved is I need to know if today is Monday, then is this the 1st or 3rd Monday of the month. If it is either, I do something different depending on 1st or 3rd Monday.

Here is some crude C code to get the day out of time. Ignore the IBM Informix calls.

Code:
int get_day_of_week_month(int nargs)
{
    time_t now_time, timer;
    struct tm * pnow_time;

    int day_of_week;
    int day_of_month;

    now_time = time(NULL);

    pnow_time = localtime(&now_time);

    day_of_week = pnow_time->tm_wday;

    day_of_month = pnow_time->tm_mday;

    ibm_lib4gl_returnMInt(day_of_week);

    ibm_lib4gl_returnMInt(day_of_month);

    return 2;
}
If this returns that the day is a Monday, then

This select statement executes:

Code:
    select  d.monday
    into    first_monday
    from    day_of_week d
    where   d.week_number = 1
    and     d.monday = day_of_month
If not the first Monday --
Code:
where   d.week_number = 1
-- then see if it's the third Monday,

Code:
        select  d.monday
        into    third_monday
        from    day_of_week d
        where   d.week_number = 3
        and     d.monday = day_of_month
Here's the contents of day_of_week. Both columns are smallint.

Code:
monday week_number 

     7           1
     6           1
     1           1
     5           1
     4           1
     3           1
     2           1
     8           2
     9           2
    14           2
    10           2
    11           2
    13           2
    12           2
    15           3
    16           3
    19           3
    17           3
    20           3
    21           3
    18           3
    23           4
    26           4
    28           4
    22           4
    25           4
    24           4
    27           4
    31           5
    29           5
    30           5

Last edited by cmnorton; 01-26-2010 at 02:17 PM. Reason: Add info
 
Old 01-26-2010, 02:48 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I'd go with a slightly more simple approach.


Code:
if [[ $(date "+%u") -eq 7 && $(date "+%e") -ge 8 ]]; then echo shutdown -r now; fi
If the day is a Sunday, and it's day 8 or more of the month (which means
it must be the 2nd Sunday of that month), go and reboot.

[edit]Oh, and as far as crontab goes: run daily ;}[/edit]



Cheers,
Tink

Last edited by Tinkster; 01-26-2010 at 02:58 PM. Reason: [edit]
 
Old 01-26-2010, 02:55 PM   #4
RIPNHL2005
LQ Newbie
 
Registered: Jan 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Tinkster,

Thank you, that seems much simpler.
But where do I put this command? In crontab file or somewhere else?

I'm new to Linux and just starting to learn it, so pardon my ignorance if this is a dumb question. haha

Thanks again
 
Old 01-26-2010, 03:16 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not dumb at all. I'd slap that into a shell-script, and point
cron at it.

Code:
#!/bin/bash
if [[ $(date "+%u") -eq 7 && $(date "+%e") -ge 8 ]]; then echo shutdown -r now; fi
Make that e.g. /sbin/schedule_reboot.sh


And then either symlink into /etc/cron.daily, or make it run every Sunday from
roots crontab.
Code:
15 8 * * Sun /sbin/schedule_reboot.sh
And looking at that crontab entry, you can do away with the
test for Sunday anyway ;}

So:
Code:
#!/bin/bash
if [[ $(date "+%e") -ge 8 ]]; then echo shutdown -r now; fi

Oooops.... slight oversight ... we don't want to boot
every Sunday > 2nd ...

So
Code:
#!/bin/bash
if [[ $(date "+%e") -ge 8 && $(date "+%e") -le 14 ]]; then echo shutdown -r now; fi

Last edited by Tinkster; 01-26-2010 at 03:20 PM.
 
  


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 configuration for particular day ZAMO Linux - Server 2 12-13-2008 04:23 AM
Crontab Question ballistic509 Debian 4 12-22-2006 05:27 PM
Crontab Question systemhackerz Red Hat 4 06-28-2006 12:02 AM
crontab question kaplan71 Linux - General 4 07-18-2005 04:04 PM
crontab question linuxtesting2 Linux - General 5 09-21-2004 04:54 PM

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

All times are GMT -5. The time now is 08:24 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