Is it wise to use PHP for a daemon?

2020-01-25 06:52发布

I wish to create a background process and I have been told these are usually written in C or something of that sort. I have recently found out PHP can be used to create a daemon and I was hoping to get some advice if I should make use of PHP in this way.

Here are my requirements for a daemon.

  • Continuously check if a row has been added to MySQL database table
  • Run FFmpeg commands on what was retrieved from database
  • Insert output into MySQL table

I am not sure what else I can offer to help make this decision. Just to add, I have not done C before. Only Java and PHP and basic bash scripting.

Does it even make that much of a performance difference?

Please allow for my ignorance, I am learning! :)

Thanks all

标签: php c linux daemon
17条回答
疯言疯语
2楼-- · 2020-01-25 07:16

If you do decided to go down the daemon route, there is a great PEAR module called System_Daemon which I've recently used successfully on a PHP v5.3.0 installation. It is documented on the authors blog: http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php

If you have PEAR installed, you can install this module using:

pear install -f System_Daemon

You will also need to create a initialisation script: /etc/init.d/<your_daemon_name>

Then you can:

  • Start Daemon: /etc/init.d/projNotifMailDaemon start
  • Stop Daemon: /etc/init.d/projNotifMailDaemon stop

Logs are kept at: /var/log/<your_daemon_name>.log

查看更多
Root(大扎)
3楼-- · 2020-01-25 07:17

A cron-job would probably work just fine, if near-instant actions is not required.

I'm just about to put live, a system I've built, based on the queueing daemon 'beanstalkd'. I send various small messages from (in this case, PHP) webpage calls to the daemon, and a PHP script then picks them up from the queue and performs various tasks, such as resizing images or checking databases (often passing info back via a Memcache-based store).

To avoid long-running processes, I've wrapped it in a BASH script, that, depending on the value returned from the script ("exit(1);") will restart the script, for every (say) 50 tasks it's performed. If it's restarting because I plan it to, it will do so instantly, any other exit value (the default is 0, so I don't use that) would pause a few seconds before it was restarted.

查看更多
The star\"
4楼-- · 2020-01-25 07:17

If you do so, pay attention to memory leaks. PHP 5.2 has some problems with its garbage collector, according to this (fixed in 5.3). Perhaps its better to use cron, so the script starts clean every run.

查看更多
何必那么认真
5楼-- · 2020-01-25 07:19

Php isn't any better or worse for this kind of thing than any of the other common scripting languages. It has fairly complete access to all of the system calls and library utilities you would need to do this sort of work. If you are most comfortable using PHP for scripting, then php will do the job for you.

The only down side is that php is not quite as ubiquitous as, say, perl or python, which is installed on almost every flavor of unix. Php is only found on systems that are going to be serving dynamic web content. Not that a Php interpreter is too large or costly to install also, but if your biggest concern is getting your program to many systems, that may be a slight hurdle.

查看更多
叼着烟拽天下
6楼-- · 2020-01-25 07:20

I would be inclined to perform this task with a cron job, rather than polling the database in a daemon.

It's likely that your FFmpeg command will take a while to do it's thing, right? In that case, is it really necessary to be constantly polling the database? Wouldn't a cronjob running each minute (or every five, ten or twenty minutes for that matter) be a simpler way to achieve the same thing?

查看更多
登录 后发表回答