Cron for end of month

Hello,

I'm on FreeBSD 8.2-STABLE shell and I was trying the following cron entry for end of month:

59 23 * * * [ `date +%d` -eq `echo \`cal\` | awk '{print $NF}'` ] && /home/user/eomt.sh >/dev/null 2>&1

I get the following error

Subject: Cron <user@shell> [ `date +

Syntax error: EOF in backquote substitution
Syntax error: Error in command substitution

eomt.sh contains the following codes

#!/bin/sh
  rm /home/user/tst/logs/* && rm /home/user/tst/stats/*
exit 0

your help will be greatly appreciated.

regards
user

Probably best to run job every night:

59 23 * * * /home/user/eomt.sh >/dev/null 2>&1

and check for end-of-month in eomt.sh:

#!/bin/sh
if [ `date +%d` -eq `awk '$NF{E=$NF} END{printf E}'` ]
then
         rm /home/user/tst/logs/* && rm /home/user/tst/stats/*
fi
exit 0
1 Like

The percent sign in a command must be escaped (\%). Alone it is treated as a newline which is likely the cause of the error indicated.

1 Like

Chubler_XL and agama, thanks so much, appreciate it. I'll try your suggestions and will give back my input.

eom=$(cal | tr -s '\n' ' ' | awk '{print $(NF)}')
if [ "$(date +%e)" = "$eom" ] ; then
    # do something
fi

Generally as Chubler points out, you want this script to run every day of the month, but just do some work on the last day. If you have GNU date or can install it, life will be easier for this kind of thing. And do not run it too close to midnight in case some other major system hog is running and pre-empts your process right into next month. We had that happen on an oracle server that gets the heck beat out of it.

1 Like

Jim, thank you so much for the nice suggestion

Hi, just wanted to give updates regarding the end of month script. I tried Chubler_XL codes and unfortunately it didn't work. I was waiting for 05/31.

This is what i have tried:

59 23 * * * /home/user/eomt.sh >/dev/null 2>&1

eomt.sh:

#!/bin/sh

if [ `date +%d` -eq `awk '$NF{E=$NF} END{printf E}'` ]
then
    rm /home/user/tst/logs/* && rm /home/user/tst/stats/*
fi
exit 0

I also tried to run the script manually by using ./eomt.sh and it doesn't even come out.

best regards
user

I would have done it this way:

if awk -v date="$(date "+%Y %m %d" )" '
    BEGIN {
        split( date, dt, " " );
        split( "1 -2 1 0 1 0 1 1 0 1 0 1", dim, " " );                                  # days in month; sort of
        dim[2] += dt[1]%4 != 0 ? 0 : (dt[1]%100 != 0 ? 1 : (dt[1]%400 != 0 ? 0 : 1));   # adjust for leap year

        exit( !( dt[3] == dim[dt[2]] + 30) );       #true if last day, invert because for shell, exit code 0 is success 
    }
'
then
    echo "last day"    # put your logic to remove files here
else
    echo "not last day"
fi

You can test (regardless of day) by replacing the initial if expression with something like the line below which has the date hard coded:

if awk -v date="2012 06 30" '
1 Like

Oops something seems to be missing here try:

#!/bin/sh
 
if [ `date +%d` -eq `cal | awk '$NF{E=$NF} END{printf E}'` ]
then
    rm /home/user/tst/logs/* && rm /home/user/tst/stats/*
fi
exit 0
2 Likes

agama, Chubler_XL , thanks so much. I'll try this.