LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-14-2012, 03:50 PM   #1
OldDad
LQ Newbie
 
Registered: Jun 2012
Posts: 12

Rep: Reputation: Disabled
Cron Script To Auto-Start Spamassassin


I am running a Verio VPS runing with RHEL. They supplied me with the following cron script to auto-restart spamd if it stops running for whatever reason: (single line of course)

*/5 * * * * ( /bin/ps ax | /bin/grep -v /bin/grep | /bin/grep spamd ) || ( /etc/init.d/spamassassin start ; cat /dev/null | /bin/mail -s "spamd was restarted on `/bin/hostname`" root )

The script will run every 5 minutes and will restart spamd successfully. However, if spamd is already running the script will email the following:

11389 ? Ss 0:03 /usr/bin/spamd -d -c -m5 --socketpath=/var/run/spamd.sock -H -r /var/run/spamd.pid
3247 ? S 0:49 spamd child
1812 ? S 0:36 spamd child

I'm not interested in that email, only an email if spamd is successfully restarted. Verio will not support the script any further even tho they are the ones that supplied it to begin with.

Would appreciate some help here, even an entirely new script that will determine if spamd is running and if not will auto-restart it and email me.

Cheers, Jay
 
Old 06-14-2012, 03:54 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Add this:

Code:
/bin/ps ax | /bin/grep -v /bin/grep | /bin/grep spamd > /dev/null 2>&1
 
1 members found this post helpful.
Old 06-14-2012, 03:56 PM   #3
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Well I guess I should explain it a bit too...

Read this for redirection and what /dev/null is: http://www.cyberciti.biz/faq/how-to-...rs-to-devnull/
 
1 members found this post helpful.
Old 06-14-2012, 04:34 PM   #4
OldDad
LQ Newbie
 
Registered: Jun 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
I was aware of that but just had no idea where to put it. I'll give it a try and post back. Thanks for the quick reply.

Cheers, Jay
 
Old 06-14-2012, 04:44 PM   #5
OldDad
LQ Newbie
 
Registered: Jun 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
That worked but I get no mail at all if spamd is restarted, I tested by stopping the daemon. The most important is that spamd is auto-started if it fails.

Cheers, Jay
 
Old 06-14-2012, 05:00 PM   #6
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
That script is written pretty badly. Let me write a good one and I'll post it here, then you can send it to Verio's management to show how incompentent their support is(I worked for a very big hosting provider and I can tell you its probably some high school grad uploading test scripts and telling you to eff off if it works).

Create the below cron tab entries:

Code:
MAILTO=youremail@domain.com
*/5 * * * * /root/checkspamd.sh
Create a file /root/checkspamd.sh with the below:

Code:
#!/bin/bash

date >> /tmp/check_spamd.log
PROCS=`ps aux | grep "spamd" | egrep -v "cron||grep" | wc -l`

if [ $PROCS -gt 0 ]; then
    echo "Found $PROCS running spamd process(es), exiting" >> /tmp/check_spamd.log
    echo >> /tmp/check_spamd.log
    exit 0
elif [ $PROCS -eq 0 ]; then
/etc/init.d/spamassassin start >> /tmp/check_spamd.log
RET=$?
if [ $RET -gt 0 ]; then
    echo "Error: Spam Assassin Is not running and returned exit code $RET when attempting to start. Check log 
files." 
    echo "Error: /etc/init.d/spamd start returned $RET" >> /tmp/check_spamd.log
    exit 1
elif [ $RET -eq 0]; then
    echo "Spam Assassin was started successfully!"
fi
fi
 
1 members found this post helpful.
Old 06-14-2012, 05:13 PM   #7
OldDad
LQ Newbie
 
Registered: Jun 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Stopped spamd and the /checkspamd.sh restarted it ok. However, this error was emailed:

/checkspamd.sh: line 18: [: missing `]'

Cheers, Jay

BTW: forgot to mention that I did not get the restart successfully email, only the error mail. But spamd was restarted nonetheless.

Last edited by OldDad; 06-14-2012 at 05:16 PM.
 
Old 06-14-2012, 05:20 PM   #8
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
I'm not getting any syntax errors can you double check that it copied correctly? The line it looks to be refrencing is :
elif [ $RET -eq 0]; then
 
1 members found this post helpful.
Old 06-14-2012, 05:22 PM   #9
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
You know what add a space after the 0 and it should work. Sorry about that.
 
1 members found this post helpful.
Old 06-14-2012, 05:23 PM   #10
OldDad
LQ Newbie
 
Registered: Jun 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Same as posted.

Jay
 
Old 06-14-2012, 05:25 PM   #11
OldDad
LQ Newbie
 
Registered: Jun 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Ok, just did, will see what happens, thanks

Jay
 
Old 06-14-2012, 05:25 PM   #12
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Did you add the space after the 0 on the last elif line I posted? The missing space is making bash not see the "]" .I think I may have been posting that at the same time you replied.
 
2 members found this post helpful.
Old 06-14-2012, 05:28 PM   #13
OldDad
LQ Newbie
 
Registered: Jun 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Yes, just as you instructed and it worked. Got the success email this time and no error mails. Thanks

Whilst I have your attention :-)

I need to make pico the default editor. I can do this:

# export EDITOR=pico

But that only lasts one session, how do I make that permanent.

Cheers, Jay
 
Old 06-14-2012, 05:29 PM   #14
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Add that line to your .bashrc file in the home directory for the user you want it to be applied to.
 
1 members found this post helpful.
Old 06-14-2012, 05:32 PM   #15
OldDad
LQ Newbie
 
Registered: Jun 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Oh oh another issue - getting an email saying success starting spamd even tho it's already running. Can I get an email ONLY if it wasn't running and was restarted?

Cheers, Jay
 
  


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 doesn't start a script alex64 Linux - Newbie 9 06-11-2011 12:27 AM
using cron to start and stop a bash script monty Linux - Software 2 03-20-2006 02:30 PM
shell script fo run auto job in cron JolynnMarie LinuxQuestions.org Member Intro 0 04-28-2004 11:21 AM
Auto Start Script for Oracle sikandar Linux - Software 0 09-12-2003 10:43 PM
KDE auto start script problem Coolrunr Programming 2 09-08-2003 09:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:35 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
Open Source Consulting | Domain Registration