views:

62

answers:

3

How can we trigger a shell script on an unix server through an email with particular subject?

+4  A: 

procmail allows you to act on incoming mails, including filtering and starting external commands.

Some useful links:

Wim
Can you provide me an URl where the process of doing so is documented.
Arkid
A: 

You could in theory, by writing a program to monitor/poll the incoming email server and check the subject line using standard POP3 protocol, if the subject line has a particular trigger keywords, invoke the shell script... This is the order of approach that would suit... there may be an open source solution already out there...

  • Using sockets, connect to the incoming email server by IP and port (usually 25), non-blocking that is not to seize up and chew up CPU time, within a thread looping forever
  • List the emails using the POP3 protocol
  • Pull down the headers via POP3 protocol and do a regexp on the subject line
  • If the regexp matches the subject line, issue a trigger perhaps a system call to invoke the shell script
tommieb75
+1  A: 

More general, but to my mind less useful than Wim's procmail suggestion: You can even just point your .forward at an executable with "|scrip.sh".

dmckee
Actually, barring a system-wide procmail install that integrates directly with the mailer, that's how you'd get procmail started in the first place. But if you do use procmail you can leverage its filtering features and avoid having to write the subject filter in your own script.
Wim