LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   scheduled script not executing as expected (https://www.linuxquestions.org/questions/linux-newbie-8/scheduled-script-not-executing-as-expected-4175442489/)

kumarjohn 12-21-2012 04:00 PM

scheduled script not executing as expected
 
Hi,

Greetings!!

I wrote a small script to delete Jboss logs. Script is running fine and deleting log files when it is executed manually. But when I schedule it using cron Script is executing but logs are not being deleted.

Unable to figure out what mistake I am doing.

Please find the script below for reference.

---------------------------------------------------

#!/bin/bash
EXECDATE=`date +%d-%b-%Y,%l:%M%p`
echo -e "\n Deleting Log files older then 3 days at $EXECDATE" >> /root/scripts/ dellogs.txt
echo -e "\n##########################################################" >> /root/ scripts/dellogs.txt
find $JBOSS_HOME/server/default/log -name "*.log.*" -mtime +3 -exec rm -f {} \;
# End of script

---------------------------------------------------

Also find the cron Entry

00 3 * * * /scripts/Jbosslog_del.sh

Best Regards,
KJ

unSpawn 12-21-2012 04:30 PM

Quote:

Originally Posted by kumarjohn (Post 4854626)
Code:

$JBOSS_HOME/server/default/log -name "*.log.*"

If files are kept open by processes then deleting these files will not have the desired effect.


Quote:

Originally Posted by kumarjohn (Post 4854626)
Code:

echo -e "\n Deleting Log files older then 3 days at $EXECDATE" >> /root/scripts/dellogs.txt

BTW don't use /root for that: use /var/log or the unprivileged users home directory. And no need to echo either: see 'man logger'.


*BTW[1]: you marked this thread solved without presenting the solution. Doing that is called reciprocity and it's good for the whole of LQ, so please do.

kumarjohn 12-21-2012 09:11 PM

Hi,

Thanks for the reply Unspawn.

Quote:

If files are kept open by processes then deleting these files will not have the desired effect
Files are not kept opened by any process. Only present day log file is used by Jboss process remaining all log files are not used.

Quote:

BTW don't use /root for that: use /var/log or the unprivileged users home directory. And no need to echo either: see 'man logger'.
Thanks for the suggestion will change script as suggested. I just started to write the scripts so to understand the script was executing or not when scheduled I am echoing.

Quote:

*BTW[1]: you marked this thread solved without presenting the solution. Doing that is called reciprocity and it's good for the whole of LQ, so please do.
Thanks for the above suggestion I updated the thread and will do the same for future threads :)

Best Regards,
KJ

unSpawn 12-21-2012 09:57 PM

Two things: check the mail of the owner of the crontab. Any errors cron will mail to its owner. Else try this cronjob instead:
Code:

#!/bin/bash --
set -vx # <<== Run this cron job once with debugging enabled, then check email, then comment out this line.
PROG=${0//*\//}; MYNAMEIS=$(id -u); LOGGER=$(which logger 2>/dev/null)
[ -x ${LOGGER} ] || echo "${PROG}: couldnt find the \"logger\" binary."
find $JBOSS_HOME/server/default/log -type f -name "*.log.*" -mtime +3 | while read OLDLOG; do
 LOGSPECS="${MYNAMEIS}:$(stat -c "%a:%u:%g:%s" "${OLDLOG}" 2>/dev/null)"; RMMSG=$(rm -f "${OLDLOG}" 2>&1)
 ${LOGGER} -t ${PROG:=cron} "Deleted \"${OLDLOG}\" (LOGSPECS:"$?":${RMMSG})"; done
exit 0

Now syslog (/var/log/cron or /var/log/messages or wherever /etc/*syslog*.conf is configured to log to) should contain messages stating the owner of the cron job, the files access rights, ownership, size, exit value of 'rm' and any error message.

tonyfreeman 12-21-2012 10:54 PM

First thing I see is this:

Code:

$JBOSS_HOME
Where is that defined in your script? It looks like you are expecting the environment containing that value to be passed into the script ... but that does not happen with cron jobs.

The easiest thing to do is to define JBOSS_HOME in the script ... the second thing to do if you are adventurous is to try placing the -l (dash el) option to the she-bang line of your script:

Code:

#!/bin/bash -l
The -l will cause the script to import the environment of the user executing the script ... SO ... you have to make sure the user executing the script has JBOSS_HOME defined someplace (likely in /etc/profile.d/, or the user's home directory .bashrc, etc).

kumarjohn 12-22-2012 05:09 PM

Hi,

Greetings!!

Thanks unSpawn and tonyfreeman for the suggestions.

Sendmail service was stopped so I could no receive any mails from cron. After restarting I could found out that script was trying to find in /server/default/log path instead of $JBOSS_HOME/server/default/log.

I updated the script as per tonyfreeman suggestion and it was working fine.

Best Wishes,
KJ


All times are GMT -5. The time now is 11:06 AM.