2

I am programming a website on a Linux CentOS server (I am planning to upgrade to a VPS plan where I will have root access). Much of the website will rely on scripts that are automated.

I have 2 questions about starting automated processes.

  • Is there any way I can start a Daemon thread, or anything like that, which will constantly be running. I need to execute a script every time an email account gets a new e-mail. I am aware of cron jobs that can run every minute, but having a script that constantly runs would be ideal, so I can execute the script the moment a new e-mail arrives.

  • Is there any way from code (ideally PHP) to start a thread, which runs concurrently with the main program. In the script I am using, the imap_open is used to connect to an e-mail account, which takes a few seconds every time. However, if I could fire off multiple concurrent scripts at the same time, that would ideally reduce the program's time. Is there any way to do this?

Any help with these questions would be greatly appreciated.

smf7293
  • 55
  • 3
  • 9
  • There are solutions to trigger a PHP script the moment an E-Mail comes in - no need for daemons. Maybe I can find a link – Pekka Jun 01 '12 at 23:26
  • In your case, no need to have a daemon running. You could just add a kind of trigger when your email account receive a message. This trigger would run your script, no more check at regular intervals. Which email client do you use ? – ldiqual Jun 01 '12 at 23:27
  • @Idiqual he's on a server, I don't think he has a client running – Pekka Jun 01 '12 at 23:27
  • @Pekka I was going to modify my comment. I guess there is an imap server running, or something like that ? – ldiqual Jun 01 '12 at 23:28
  • Yes, there is an imap server running. I am using PHP's imap class to connect to the email inbox. And yes, I am on a server, so I don't have an e-mail client running. – smf7293 Jun 01 '12 at 23:32

3 Answers3

4

You can certainly write a daemon / service that runs constantly. For a starting tutorial see

http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html

Your daemon can implement SMTP (there are existing libraries available to support this) to periodically check the email account for new emails and act accordingly.

Here's a question with answers from SO that discusses how to accomplish all of this with Python

How to make a Python script run like a service or daemon in Linux

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
3

For the first part, there's two easy solutions:

  1. Use the Vixie cron @reboot start specification to start your daemon at reboot as a standard user. This and every-minute cron-jobs are the only mechanisms that make it easy to run a daemon-style service as a user.

  2. Use procmail to start a new script on every email delivery. The downside here is that procmail will run and then start a new program on every email -- when you're getting a hundred emails per second, this could be a serious hindrance compared to a daemon that uses inotify(7) to alert a long-lived program about new emails.

For the second part, look for a wrapper for the fork(2) system call. It cleaves a program cleanly in half -- parent and child -- and allows each to continue independent execution from then on. If the child and parent need to communicate again in the future, then perhaps see if PHP supports threaded execution.

sarnold
  • 102,305
  • 22
  • 181
  • 238
0

And what about incron? May be there is a way to use it in your case but you must produce a filesystem event (for example create a new file).

j0k
  • 22,600
  • 28
  • 79
  • 90