电子邮件运行一个bash脚本

我有一个bash脚本来重新启动一个每3am运行一次的服务,以保证服务不会在用户需要时崩溃。 但是,我需要为我的合作伙伴(不是来自IT领域)提供一个更简单的方法,以便在出现问题时运行此脚本。

有什么办法可以configuration这个脚本在电子邮件发送到某个地址时运行?

绝对!

在/ etc / aliases文件中,执行以下操作:

servicerestart: |/usr/local/bin/kickservice 

…电子邮件将发送到[email protected] ,您所写的脚本就是上面的path。 电子邮件和所有标题将被传递到脚本的stdin ,您可以根据需要进行处理(查找密码等)

如果MTA服从〜/ .forward文件,那么您可以让procmail处理收到的邮件,并在邮件到达后立即运行脚本。

请参阅http://www.panix.com/~elflord/unix/procmail.html

对于我的帐户之一,我有以下〜/ .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: [email protected]' MY_RECIPIENT='[email protected]' ############################################################################# # 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 

绝对。

你需要:

  • MTA收到这些邮件,
  • 一个处理器来处理邮件,
  • 规则和限制,以确保它不会被滥用(容易)

根据要求,我会build议postfix作为MTA; 它具有内置的function来执行邮件接收的脚本,还可以轻松configuration邮件接收和连接的限制。

当然,大多数系统pipe理员只会configurationcron