LinuxQuestions.org
Visit Jeremy's Blog.
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 01-02-2012, 07:11 PM   #1
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Rep: Reputation: 0
compare file cron reload


i have a vhost list that i would like to check every 30 minutes and see if there has been a change

it is placed in the directory conf.d/domainList_1.conf

can anyone help me proofread this code and let me know if it makes sense
Code:
#!/bin/bash
# apache reload command
RELOAD="/sbin/service httpd reload"
HOME="/etc/httpd/conf.d"
#
if ( cmp -s $HOME/domainList_1.conf $HOME/domainList_1.conf ) ; then
#
	 #echo files match
#
else
#
	#echo files do not match
		sleep 10
		$RELOAD
#
fi

Last edited by ggalan; 01-05-2012 at 06:09 PM.
 
Old 01-02-2012, 07:35 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
You compare a file against itself ?

The if condition is wrong syntax, in bash you simply do :

Code:
if /usr/bin/cmp $file1 $file2; then
   echo files match
else
   ...
fi
or if you don't need action if files match...
Code:
# if files don't match
if ! /usr/bin/cmp $file1 $file2; then
    # do action
    sleep 10
    $RELOAD
fi
Also, better add full program path if using the script with cron, just in case

Last edited by Cedrik; 01-02-2012 at 07:40 PM.
 
1 members found this post helpful.
Old 01-02-2012, 07:46 PM   #3
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
yes i would like to see if there was any change in the file since the last check

would i have to define here?
$file1 = ??
$file2 = ??
 
Old 01-02-2012, 07:52 PM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
Or maybe a generic script would be more flexible ... you're better off with checksums:

Code:
#!/bin/bash

if [[ $# -ne 2 ]]
then
    echo "<*> usage: $0 <service> <target_file_to_monitor>"
    exit 0
fi

SERVICE=$1
TARGET=$2
SHORTNAME=$(basename $TARGET)
RELOAD="/sbin/service $SERVICE reload"

touch ./last_checksum_$SHORTNAME
md5sum $TARGET > ./current_checksum_$SHORTNAME
$(diff -q ./last_checksum_$SHORTNAME ./current_checksum_$SHORTNAME > /dev/null 2>&1 )
if [[ $? -eq 0 ]]
then
    echo "[*] no change"
else
    echo "[*] $TARGET has been modified, reloading $SERVICE"
    $RELOAD
    cp ./current_checksum_$SHORTNAME ./last_checksum_$SHORTNAME
fi
.. modify as you see fit.

Last edited by kbp; 01-02-2012 at 07:54 PM.
 
1 members found this post helpful.
Old 01-02-2012, 08:03 PM   #5
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
kbp, from your example, which variables would i need to define
to check on my file "/etc/httpd/conf.d/domainList_1.conf"?

Last edited by ggalan; 01-02-2012 at 08:05 PM.
 
Old 01-02-2012, 08:32 PM   #6
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
Code:
./check.sh httpd /etc/httpd/conf.d/domainList_1.conf
.. replace 'check.sh' with whatever you called the script

Note: if you intend to run this as a cron job it's advisable to remove any extraneous 'echo' or other output commands

Last edited by kbp; 01-02-2012 at 08:34 PM.
 
Old 01-02-2012, 08:48 PM   #7
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
so the final code

so where do these go?
./check.sh
httpd
/etc/httpd/conf.d/domainList_1.conf


check.sh
Code:
#!/bin/bash
if [[ $# -ne 2 ]]
then
    exit 0
fi

SERVICE=$1
TARGET=$2
SHORTNAME=$(basename $TARGET)
RELOAD="/sbin/service $SERVICE reload"

touch ./last_checksum_$SHORTNAME
md5sum $TARGET > ./current_checksum_$SHORTNAME
$(diff -q ./last_checksum_$SHORTNAME ./current_checksum_$SHORTNAME > /dev/null 2>&1 )
if [[ $? -eq 0 ]]
then

else
    $RELOAD
    cp ./current_checksum_$SHORTNAME ./last_checksum_$SHORTNAME
fi

Last edited by ggalan; 01-02-2012 at 09:24 PM.
 
Old 01-03-2012, 06:40 AM   #8
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
You're missing my point, 'httpd' and '/etc/httpd/conf.d/domainList_1.conf' are arguments to 'check.sh'. And when I said 'extraneous' I meant unnecessary output, maybe you should leave it as it was originally.

Create a cronjob something like:

Code:
* */2 * * * /path/to/check.sh httpd /etc/httpd/conf.d/domainList_1.conf > /dev/null 2>&1
 
1 members found this post helpful.
Old 01-05-2012, 12:19 AM   #9
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
running this i didnt get an output from log

crontab
Code:
*/2 *  *  *  *  /usr/local/bin/check.sh httpd /etc/httpd/conf.d/domainList_1.conf > /dev/null 2>&1
bash
Code:
#!/bin/bash

if [[ $# -ne 2 ]]
then
    echo "<*> usage: $0 <service> <target_file_to_monitor>"
    exit 0
fi

SERVICE=$1
TARGET=$2
SHORTNAME=$(basename $TARGET)
RELOAD="/sbin/service $SERVICE reload"

touch ./last_checksum_$SHORTNAME
md5sum $TARGET > ./current_checksum_$SHORTNAME
$(diff -q ./last_checksum_$SHORTNAME ./current_checksum_$SHORTNAME > /dev/null 2>&1 )
if [[ $? -eq 0 ]]
then
    echo "[*] no change"
else
    echo "[*] $TARGET has been modified, reloading $SERVICE"
    $RELOAD
    cp ./current_checksum_$SHORTNAME ./last_checksum_$SHORTNAME
fi
but when i ran this
Code:
#!/bin/bash
# apache reload command
RELOAD="/sbin/service httpd reload"
HOME="/etc/httpd/conf.x/test.conf"
if ! /usr/bin/cmp $file1 $file2; then
    # do action
    sleep 10
    $RELOAD
fi
the log gave
Code:
/usr/bin/cmp: missing operand after `/usr/bin/cmp'
/usr/bin/cmp: Try `/usr/bin/cmp --help' for more information.
Reloading httpd:
all the paths are correct, whats going on?
 
Old 01-05-2012, 10:57 AM   #10
ggalan
Member
 
Registered: Oct 2010
Posts: 57

Original Poster
Rep: Reputation: 0
i did some tests and found that this cron doesnt execute. any thoughts why?
Code:
*/2 *  *  *  *  /usr/local/bin/check.sh httpd /etc/httpd/conf.d/domainList_1.conf > /dev/null 2>&1
made sure all the *.sh files are
sudo chmod -R 744 /usr/local/bin/myFiles.sh


re: everything works, this suppressed the output and i didnt see it in the log
Code:
> /dev/null 2>&1

Last edited by ggalan; 01-05-2012 at 06:10 PM.
 
Old 01-05-2012, 07:28 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Quote:
i did some tests and found that this cron doesnt execute. any thoughts why?
1. cron will normally email the cronjob owner or root if it has an issue, so check those.

2. change
Code:
> /dev/null 2>&1

# to a log eg
/home/someuser/prog.log
3. check also /var/log/cron, /var/log/messages
 
  


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
vim file.tex !latex file.tex & pdfdvi file.dvi & xpdf -remote sname -reload Mol_Bolom Linux - General 0 07-09-2009 08:46 PM
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 09:09 AM
delete file and reload it ??? mrlinux2000 Linux - Newbie 2 02-28-2008 02:48 PM
cron file 000-delay.cron (it is on my FC5) - what is the purpose? jtmoon Linux - Server 1 03-08-2007 12:15 PM
Tomcat reload servlet is very slow, but reload jsp is fast and good? gsbarry Programming 2 04-28-2006 10:34 PM


All times are GMT -5. The time now is 06:37 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