LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Crontab configuration question (https://www.linuxquestions.org/questions/linux-newbie-8/crontab-configuration-question-784893/)

RIPNHL2005 01-26-2010 01:30 PM

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

cmnorton 01-26-2010 02:16 PM

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


Tinkster 01-26-2010 02:48 PM

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

RIPNHL2005 01-26-2010 02:55 PM

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

Tinkster 01-26-2010 03:16 PM

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



All times are GMT -5. The time now is 04:37 PM.