3

I have a bash script to restart a service that runs every 3am to guarantee that the service isn't crashed when users require it. However, I need to provide a simpler way for my partner (that isn't from IT area) to run this script when something go wrong.

Is there any way to configure this script to run when a email is sent to some address?

João Daniel
  • 349
  • 3
  • 6
  • 10
  • 3
    As an aside, you can looking into things like Monit to keep services up, instead of relying on some sort of human intervention (via some duct-tape-and-baling-wire scripts). – cjc Apr 20 '12 at 14:36
  • 2
    I'd DEFINITELY look into configuring Monit rather than relying on a rube-goldberg-esque solution that requires a non-tech person to keep things running. Monit not only can give you the status of services, but can restart them *for* you when the service actually fails and notify you that it has failed. – Bart Silverstrim Apr 20 '12 at 14:55
  • If you can use monit then you can use cron - although monit is much more sophisticated than cron tehre is a learning curve there. If you can configure your MTA to handle emails like this then you should be able to configure cron to do the same - which will be much *MUCH* safer. – symcbean Apr 20 '12 at 15:29
  • I was able to configure Monit and it really solved my problem! It not just allow me to restart the service in a simple click of button, but it does a much better job: it monitors the process for me! Thanks! – João Daniel Apr 20 '12 at 18:54

3 Answers3

3

Absolutely!

In your /etc/aliases file, do this:

servicerestart: |/usr/local/bin/kickservice

... where the email will be sent to servicerestart@yourdomain.com and the script you've written is the path above. The email and all the headers will be passed to the stdin of your script which you can process as needed (look for password, etc.)

Jeff Ferland
  • 20,547
  • 2
  • 62
  • 85
2

If the MTA obeys a ~/.forward file, then you can have procmail process the incoming mail and run a script as soon as the message arrives.

See http://www.panix.com/~elflord/unix/procmail.html

For one of my accounts, I have the following ~/.procmailrc

# procmail tutorial: http://tldp.org/LDP/LG/issue14/procmail.html

PATH=/usr/local/bin:/bin:/usr/bin
MAILDIR=$HOME/Mail
DEFAULT=$HOME/Mail/inbox
LOGFILE=$HOME/procmail.`date +%Y-%m`.log
SHELL=/usr/bin/ksh

MY_XLOOP='X-Loop: user@example.com'
MY_RECIPIENT='mailing_list@example.com'


#############################################################################
# 3rd party request processing
# send a copy of the message to the processing script, and carry on
# with the next recipe

:0c
* ^From:.*@3rdparty\.invalid
* ^Subject:.*ABC/DEF.*(Request|Access|Approval)
| $HOME/bin/process_request_email.pl | \
  mailx -s "3rd party request results" $MY_RECIPIENT


#############################################################################
# forward all mail to mailing list
:0
* ! ^$MY_XLOOP
{
    # add a header
    # 'f' = filter: continue processing results of program
    # 'w' = wait for program to return
    # 'h' = pass message headers to program
    :0fwh
    | formail -A "$MY_XLOOP"

    # then forward the message
    # 'c' = send a copy to recipient and continue processing
    :0c
    ! $MY_RECIPIENT
}

# if we get here, then the message has an X-Loop header.
# let it fall into $DEFAULT
glenn jackman
  • 4,630
  • 1
  • 17
  • 20
  • If the local MTA obeys a .forward-file, you don't ***need*** procmail: just forward directly to a script. – adaptr Apr 20 '12 at 14:20
  • but procmail allows you to add additional logic like forwarding a copy to a mailbox, avoiding mail loops, and validating the origin / authenticating the email - which is pretty essential if you expose functionality like this via email. – symcbean Apr 20 '12 at 15:27
1

Absolutely.

You need:

  • an MTA to receive these mails,
  • a processor to process the mail,
  • rules and restrictions to make sure it can't be abused (easily)

Based on the requirements, I would advise postfix as the MTA; it has built-in capabilities to execute scripts on message reception, plus easy to configure restrictions on mail reception and connectivity.

Of course, most sysadmins would just configure cron.

adaptr
  • 16,576
  • 23
  • 34
  • I'll take a look on postfix! Regarding cron, I'm using this to run the script on schedule (every 3am). Is it also useful for this email question? – João Daniel Apr 20 '12 at 14:03
  • No, but you provide no information as to why this doesn't work for you. – adaptr Apr 20 '12 at 14:07