user3
May 20, 2012, 9:12pm
1
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
agama
May 20, 2012, 9:41pm
3
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
user3
May 21, 2012, 9:05pm
4
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
user3
May 22, 2012, 6:11pm
6
Jim, thank you so much for the nice suggestion
user3
June 1, 2012, 9:28pm
7
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
agama
June 2, 2012, 1:44pm
8
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
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
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
user3
June 4, 2012, 9:28pm
10
agama, Chubler_XL , thanks so much. I'll try this.