You could start your php script from the command line (i.e. bash) by using

nohup php myscript.php &

the & puts your process in the background.

Edit:
Yes, there are some drawbacks, but not possible to control? That's just wrong.
A simple kill processid will stop it. And it's still the best and simplest solution.

Answer from Henrik P. Hessel on Stack Overflow
🌐
GitHub
github.com › lifo101 › php-daemon
GitHub - lifo101/php-daemon: PHP Multiprocessing Daemon. Easily create stable and robust daemons with minimal boilerplate code. · GitHub
PHP Multiprocessing Daemon. Easily create stable and robust daemons with minimal boilerplate code. - lifo101/php-daemon
Starred by 51 users
Forked by 15 users
Languages   PHP
🌐
Nomad PHP
nomadphp.com › blog › 50 › creating-a-php-daemon-service
Creating a PHP Daemon Service | dmamontov's Blog - Nomad PHP
To create demons in PHP you need to use the extensions pcntl and posix. To implement the fast communication withing daemon scripts it is recommended to use the extension libevent for asynchronous I/O.
🌐
GitHub
github.com › shaneharter › PHP-Daemon
GitHub - shaneharter/PHP-Daemon: Build production-ready Daemons using PHP 5.3+. Build fault-tolerant applications without the boilerplate. · GitHub
Build production-ready Daemons using PHP 5.3+. Build fault-tolerant applications without the boilerplate. - shaneharter/PHP-Daemon
Starred by 760 users
Forked by 129 users
Languages   PHP 99.8% | Shell 0.2%
🌐
New Relic
docs.newrelic.com › docs › apm › agents › php-agent › advanced-installation › starting-php-daemon-advanced
Starting the PHP daemon (advanced) | New Relic Documentation
Unless instructed otherwise, the standard installation for the PHP agent will automatically start the daemon if it detects that it is not running. The license key is configured for the agent in a PHP INI file, and it can be modified on a per-directory or per-virtual host basis.
🌐
Reddit
reddit.com › r/php › developing a daemon in php
r/PHP on Reddit: Developing a daemon in PHP
February 29, 2012 - Most of the things over the years that I have thought I needed a daemon for can be done in small pieces on a schedule or just as a cleanup step done in the course of running a script. ... The GC improvements in 5.3 are substantial. You can also call them on-demand. You have the same memory-leak concerns in every other language as well, some fare better than others but none let the programmer just do whatever they want without regard to resource usage. Events? http://php.net/manual/en/book.libevent.php - Works extremely well for event-driven applications, if that's your thing.
🌐
PHP
pear.php.net › manual › en › package.system.system-daemon.what-is-a-daemon.php
Manual :: What is a Daemon?
A daemon is a Linux program that run in the background, just like a 'Service' on Windows. It can perform all sorts of tasks that do not require direct user input. Apache is a daemon, so is MySQL. All you ever hear from them is found in somewhere in /var/log, yet they silently power over 40% ...
🌐
Datemill
datemill.com › home › blog › introduction to php daemons
PHP Daemons: Implementing Persistent Background Scripts
April 29, 2024 - In contrast, background PHP scripts, or daemons, operate independently of user interactions. They are typically invoked via the command line and focus on tasks like database maintenance or data analysis.
Find elsewhere
🌐
Docker Hub
hub.docker.com › r › newrelic › php-daemon
newrelic/php-daemon - Docker Image
New Relic’s PHP daemon gathers APM data from New Relic PHP agents and relays it back to New Relic.
🌐
PHP
php.net › manual › en › swoole-process.daemon.php
PHP: Swoole\Process::daemon - Manual
Swoole\Process::daemon — Change the process to be a daemon process.
🌐
RAW
raw.org › article › how-to-write-a-php-daemon
How to write a PHP Daemon • RAW
April 19, 2024 - This function prints the given string in a certain color to the console, if someone is reading on the tty device, e.g. the daemon was triggered with kill -HUP. ... An extensive PHP fork with changes to the core in order to improve performance, usability and flexibility.
🌐
Medium
medium.com › beyn-technology › maintain-the-php-apps-as-daemon-f8f4d68963d4
Maintain the PHP Apps as Daemon. In this post, we will see the chains of… | by Mert Simsek | Beyn Technology | Medium
June 2, 2022 - This means that you will write code statements (lines of code) and when a page is requested, the PHP interpreter will load your PHP code, parse it and then execute it. Finally, erase all of context, objects and variables from the memory. The problem is here. A daemon is always running and it refreshes anything on-demand.
🌐
PHP Classes
phpclasses.org › blog › post › 270-Creating-a-PHP-Daemon-Service.html
Creating a PHP Daemon Service - PHP Classes
March 27, 2015 - Daemons are special applications that can monitor and process an important activity on a machine in the background. Read this article to learn how to create a daemon in pure PHP, handling signals, handle multiple asynchronous I/O and events ...
🌐
GitHub
github.com › kakserpom › phpdaemon
GitHub - kakserpom/phpdaemon: Asynchronous server-side framework for network applications implemented in PHP using libevent
Asynchronous server-side framework for network applications implemented in PHP using libevent - kakserpom/phpdaemon
Starred by 1.5K users
Forked by 231 users
Languages   PHP 99.5% | Shell 0.5% | PHP 99.5% | Shell 0.5%
🌐
Packagist
packagist.org › packages › lifo › php-daemon
lifo/php-daemon - Packagist
Create robust and stable PHP multiprocess daemons without the boilerplate code. The core Daemon class handles the main loop and events and can run at any frequency desired (within the limits of PHP).
Top answer
1 of 16
28

As others have noted, various versions of PHP have issues with their garbage collectors. Of course, if you know that your version does not have such issues, you eliminate that problem. The point is, you don't know (for sure) until you write the daemon and run it through valgrind to see if the installed PHP leaks or not on any given machine. So on that hand, you may write it just to discover that what Zend thinks is fixed might still be buggy, or you are dealing with a slightly older version of PHP or some extension. Icky.

The other problem is somewhat buggy signals. In my experience, signal handlers are not always entered correctly with PHP, especially when the signal is queued instead of merged. That may not be an issue for you, i.e. if you just need to handle SIGINT/SIGUSR1/SIGUSR2/SIGHUP.

So, I suggest:

If the daemon is simple, go ahead and use PHP. If it looks like its going to get rather complex, or allocate lots of memory, you might consider writing it in C after prototyping it in PHP.

I am a pretty die hard C person. However, I see nothing wrong with hammering out something quick using PHP (beyond the cases that I explained). I also see nothing wrong with using PHP to prototype something that may or may not be later rewritten in C. For instance, handling database stuff is going to be much simpler if you use PHP, versus managing callbacks using other interfaces in C. So in that instance, for a 'one off', you will surely get it done much faster.

2 of 16
16

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?

🌐
PHP
pear.php.net › manual › en › package.system.system-daemon.system-daemon.php
Manual :: System_Daemon
Because the Process Control Extensions' documentation is a bit rough, I decided to figure it out once, and then wrap my knowledge and the required code inside a PEAR class called: System_Daemon. And so now you can just: ... <?php require_once "System/Daemon.php"; // Include the Class System_Daemon::setOption("appName", "mydaemon"); // Minimum configuration System_Daemon::start(); // Spawn Deamon!
🌐
Wordpress
joellubrano.wordpress.com › 2014 › 08 › 14 › running-a-daemon-via-a-php-web-page
Running a Daemon Via a PHP Web Page | The Code Less Traveled
August 15, 2014 - For those who do not know, a daemon is a long-running process that, ideally, will never stop; web servers, ssh servers, and other socket listeners are daemons. You see, shell_exec and exec wait for the child processes that they spawn to stop ...
🌐
Phpconference
phpconference.com › php-core-coding › daemons-with-php-should-you-really-do-that
Daemons with PHP: Should you really do that? - International PHP Conference
January 25, 2022 - In today's event driven world, asynchronous background processing is a core requirement. Nothing new you say - just setup a cron job! But while the rather traditional approach of periodically running scripts to handle queued up events is of course technically an option, the comparatively long delay coming with that quite often is not.
🌐
New Relic
docs.newrelic.com › docs › apm › agents › php-agent › troubleshooting › verifying-php-daemon
Verifying the PHP daemon | New Relic Documentation
Verify the daemon is running by checking the output from the previous command for two running daemon processes that look like this.