LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-21-2012, 05:00 PM   #1
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 23

Rep: Reputation: Disabled
Exclamation 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
 
Old 12-21-2012, 05:30 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Quote:
Originally Posted by kumarjohn View Post
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 View Post
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.
 
Old 12-21-2012, 10:11 PM   #3
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-21-2012, 10:57 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
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.
 
1 members found this post helpful.
Old 12-21-2012, 11:54 PM   #5
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
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).
 
2 members found this post helpful.
Old 12-22-2012, 06:09 PM   #6
kumarjohn
LQ Newbie
 
Registered: Oct 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
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
 
  


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
[SOLVED] cron in debian is not executing a script at the scheduled time educated__fool Linux - Newbie 8 09-20-2012 02:37 AM
[SOLVED] Running scheduled script with cron snadjari Linux - Newbie 5 03-04-2011 03:39 PM
[SOLVED] Multithreaded tcp server - thread not executing as expected. Lantzvillian Programming 19 02-13-2011 08:28 PM
Apache/PHP: Executing 'at' scheduled jobs? bomix Linux - Software 11 01-23-2009 02:02 PM
sh script not executing as expected ryzor Linux - General 4 12-18-2006 10:03 AM

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

All times are GMT -5. The time now is 09:15 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration