简体   繁体   English

我们可以通过发送邮件来触发UNIX中的Perl脚本吗

[英]Can we trigger a perl script in unix by sending a mail

I want to know if we can trigger a perl script in unix through a sending a mail. 我想知道我们是否可以通过发送邮件来触发unix中的perl脚本。 basically the script should check the incoming mail then trigger the perl script. 基本上,脚本应该检查传入的邮件,然后触发perl脚本。

Also can some1 out on setting the mail access like reading and saving mail on my unix home. 也可以在设置邮件访问权限方面进行一些设置,例如在我的UNIX家庭中阅读和保存邮件。

-Thanks -谢谢

There are two basic approaches you can take. 您可以采用两种基本方法。

  1. Configure your SMTP server to run incoming email through your script. 配置您的SMTP服务器以通过脚本运行传入的电子邮件。 Procmail is the usual tool for choice for this. Procmail是为此选择的常用工具。
  2. Poll (by using cron, or writing your script as a daemon) your IMAP/POP server/Maildir/Mbox/etc. 轮询(通过使用cron或将脚本作为守护程序编写)IMAP / POP服务器/ Maildir / Mbox / etc。

The former is usually the better option. 前者通常是更好的选择。

You can hack it like this: 您可以这样修改它:

inotifywait -m  /var/mail/$USER | grep --line-buffered MODIFY | while read _unused_; do 
    #your perl script here
done

Explanation: It monitors /var/mail/$USER for changes & prints events on stdout. 说明:它监视/ var / mail / $ USER的更改并在stdout上打印事件。 On every MODIFY event, it will trigger the script. 在每个MODIFY事件上,它将触发脚本。

Note: This will work only for unix mails on localhost. 注意:这仅适用于localhost上的unix邮件。 Not on external server. 不在外部服务器上。

Known bugs: A mail read activity will also trigger the script. 已知的错误:邮件读取活动也会触发脚本。

You can keep polling (unix) mail for any new mails. 您可以继续轮询(unix)邮件以查找任何新邮件。

while true; do
    echo "\nq" | mail >/tmp/new_mail 2>&1 && /path/to/your/perl_script.pl arg1 arg2 ...
    sleep 60
done

If you want, you can use contents of /tmp/new_mail in your perl program. 如果需要,可以在perl程序中使用/tmp/new_mail内容。 If you don't need it, you can redirect mail output to /dev/null instead. 如果不需要,可以将mail输出重定向到/dev/null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM