PHPackages                             wrep/daemonizable-command - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. wrep/daemonizable-command

ActiveLibrary[CLI &amp; Console](/categories/cli)

wrep/daemonizable-command
=========================

Daemonizable (endless running) commands for Symfony.

6.0.1(10mo ago)2271.5M—8.9%41[1 issues](https://github.com/mac-cain13/daemonizable-command/issues)[2 PRs](https://github.com/mac-cain13/daemonizable-command/pulls)6MITPHPPHP &gt;=8.2

Since May 17Pushed 10mo ago10 watchersCompare

[ Source](https://github.com/mac-cain13/daemonizable-command)[ Packagist](https://packagist.org/packages/wrep/daemonizable-command)[ Docs](https://github.com/mac-cain13/daemonizable-command)[ RSS](/packages/wrep-daemonizable-command/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (27)Used By (6)

Daemonizable Commands for Symfony [![Build Status](https://camo.githubusercontent.com/4a3ca395b506c63238df4caa0dfc9f72a79159c1b0373ba179a605a73011eed5/68747470733a2f2f7472617669732d63692e6f72672f6d61632d6361696e31332f6461656d6f6e697a61626c652d636f6d6d616e642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mac-cain13/daemonizable-command)
==============================================================================================================================================================================================================================================================================================================================================================

[](#daemonizable-commands-for-symfony-)

**A small bundle to create endless running commands with Symfony.**

These endless running commands are very easy to daemonize with something like Upstart or systemd.

Why do I need this?
-------------------

[](#why-do-i-need-this)

Because you want to create long running PHP/Symfony processes! For example to send mails with large attachment, process (delayed) payments or generate large PDF reports. They query the database or read from a message queue and do their job. This bundle makes it very easy to create such processes as Symfony commands.

How to install?
---------------

[](#how-to-install)

Use composer to include it into your Symfony project:

`composer require wrep/daemonizable-command`

### What version to use?

[](#what-version-to-use)

Symfony did make some breaking changes, so you should make sure to use a compatible bundle version:

- Version 6.\* for Symfony 7.3 and higher
- Version 5.\* for Symfony 7
- Version 4.\* for Symfony 6
- Version 3.0.\* for Symfony 4 and 5
- Version 2.0.\* for Symfony 3
- Version 1.3.\* for Symfony 2.8+

How to use?
-----------

[](#how-to-use)

Just create a Symfony command that extends from `EndlessCommand` and off you go. Here is a minimal example:

```
namespace Acme\DemoBundle\Command;

use Wrep\Daemonizable\Command\EndlessCommand;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;

class MinimalDemoCommand extends EndlessCommand
{
	// This is just a normal Command::configure() method
	protected function configure()
	{
		$this->setName('acme:minimaldemo')
		     ->setDescription('An EndlessCommand implementation example');
	}

	// Execute will be called in a endless loop
	protected function execute(InputInterface $input, OutputInterface $output)
	{
		// Tell the user what we're going to do.
		// This will be a NullOutput if the user doesn't want any output at all,
		//  so you don't have to do any checks, just always write to the output.
		$output->write('Updating timestamp... ');

		// Do some work
		file_put_contents( '/tmp/acme-timestamp.txt', time() );

		// Tell the user we're done
		$output->writeln('done');
	}
}
```

Run it with `php app/console acme:minimaldemo`.

An [example with all the bells and whistles](examples/ExampleCommand.php) is also available and gives a good overview of best practices and how to do some basic things.

How to daemonize?
-----------------

[](#how-to-daemonize)

Alright, now we have an endless running command *in the foreground*. Usefull for debugging, useless in production! So how do we make this thing a real daemon?

You should use [systemd](http://www.freedesktop.org/wiki/Software/systemd) to daemonize the command. They provide very robust daemonization, start your daemon on a reboot and also monitor the process so it will try to restart it in the case of a crash.

If you can't use Upstart or systemd, you can use `.lock` file with [LockHandler](http://symfony.com/doc/current/components/filesystem/lock_handler.html) with [crontab](https://wikipedia.org/wiki/Cron) wich start script every minute.

An [example Upstart script](https://github.com/mac-cain13/daemonizable-command/blob/master/examples/example-systemd.service) is available, place your script in `/etc/init/` and start the daemon with `start example-daemon`. The name of the `.conf`-file will be the name of the daemon. A systemd example is not yet available, but it shouldn't be that hard to [figure out](http://patrakov.blogspot.nl/2011/01/writing-systemd-service-files.html).

Command line switches
---------------------

[](#command-line-switches)

A few switches are available by default to make life somewhat easier:

- Use `-q` to suppress all output
- Use `--run-once` to only run the command once, usefull for debugging
- Use `--detect-leaks` to print a memory usage report after each run, read more in the next section

Memory usage and leaks
----------------------

[](#memory-usage-and-leaks)

Memory usage is very important for long running processes. Symfony is not the smallest framework around and if you leak some memory in your execute method your daemon will crash! The `EndlessCommand` classes have been checked for memory leaks, but you should also check your own code.

### How to prevent leaks?

[](#how-to-prevent-leaks)

Always start your command with the `-e prod --no-debug` flags. This disables all debugging features of Symfony that will eat up more and more memory.

Make sure you cleanup in the `execute`-method, make sure you're not appending data to an array every iteration or leave sockets/file handles open for example.

In case you are using the fingers-crossed handler in Monolog, this will also be a source of memory leaks. The idea of this handler is to keep all below-threshold log entries in memory and only flush those in case of an above-threshold entry. You can still use the fingers-crossed handler as long as you manually flush it at the end of the `execute`-method:

```
foreach ($this->getContainer()->get('logger')->getHandlers() as $handler)
{
    if ($handler instanceof FingersCrossedHandler) {
        $handler->clear();
    }
}

```

### Detecting memory leaks

[](#detecting-memory-leaks)

Run your command with the `--detect-leaks` flag. Remember that debug mode will eat memory so you'll need to run with `-e prod --no-debug --detect-leaks` for accurate reports.

After each iteration a memory report like this is printed on your console:

```
== MEMORY USAGE ==
Peak: 30038.86 KByte stable (0.000 %)
Cur.: 29856.46 KByte stable (0.000 %)

```

The first 3 iterations may be unstable in terms of memory usage, but after that it should be stable. *Even a slight increase of memory usage will crash your daemon over time!*

If you see an increase/stable/decrease loop you're probably save. It could be the garabage collector not cleaning up, you can fix this by using unset on variables to cleanup the memory yourself.

### Busting some myths

[](#busting-some-myths)

Calling `gc_collect_cycles()` will not help to resolve leaks. PHP will cleanup memory right in time all by itself, calling this method may slow down leaking memory, but will not solve it. Also it makes spotting leaks harder, so just don't use it.

If you run Symfony in production and non-debug mode it will not leak memory and you do not have to disable any SQL loggers. The only leak I runned into is the one in the MonologBundle mentioned above.

### Working with Doctrine

[](#working-with-doctrine)

For reasons EndlessContainerAwareCommand clears after each Iteration Doctrine's EntityManager. Be aware of that. You can override finishIteration() to avoid this behaviour but you have to handle the EM on your own then.

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance53

Moderate activity, may be stable

Popularity58

Moderate usage in the ecosystem

Community36

Small or concentrated contributor base

Maturity86

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 73.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~177 days

Recently: every ~141 days

Total

26

Last Release

326d ago

Major Versions

1.3.2 → 2.1.02018-03-27

2.1.0 → 3.0.02020-04-17

3.1.0 → 4.0.02021-11-30

4.1.1 → 5.0.02024-06-21

5.1.0 → 6.0.02025-06-02

PHP version history (6 changes)1.0.0PHP &gt;=5.3.3

1.3.0PHP &gt;=5.5.9

3.0.0PHP ^7.1

3.1.0PHP ^7.2|^8.0

4.0.0PHP &gt;=8.0

5.0.0PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/d6f5d02841cb19cc8a3bbe2f4e0840049de49bee9ce1cda8725a38a308d32d26?d=identicon)[wrep](/maintainers/wrep)

![](https://www.gravatar.com/avatar/bdc7ee634dc6a354a8c989df62bb7e589a59948344b71116693459cfa5a40461?d=identicon)[mac-cain13](/maintainers/mac-cain13)

---

Top Contributors

[![mac-cain13](https://avatars.githubusercontent.com/u/618233?v=4)](https://github.com/mac-cain13 "mac-cain13 (79 commits)")[![Jean85](https://avatars.githubusercontent.com/u/6729988?v=4)](https://github.com/Jean85 "Jean85 (4 commits)")[![umpirsky](https://avatars.githubusercontent.com/u/208957?v=4)](https://github.com/umpirsky "umpirsky (3 commits)")[![ovr](https://avatars.githubusercontent.com/u/572096?v=4)](https://github.com/ovr "ovr (2 commits)")[![server-may-cry](https://avatars.githubusercontent.com/u/5962057?v=4)](https://github.com/server-may-cry "server-may-cry (2 commits)")[![Icewild](https://avatars.githubusercontent.com/u/8243173?v=4)](https://github.com/Icewild "Icewild (2 commits)")[![h4cc](https://avatars.githubusercontent.com/u/2981491?v=4)](https://github.com/h4cc "h4cc (2 commits)")[![ossinkine](https://avatars.githubusercontent.com/u/2588533?v=4)](https://github.com/ossinkine "ossinkine (1 commits)")[![pieterocp](https://avatars.githubusercontent.com/u/68863060?v=4)](https://github.com/pieterocp "pieterocp (1 commits)")[![r-martins](https://avatars.githubusercontent.com/u/191149?v=4)](https://github.com/r-martins "r-martins (1 commits)")[![sc0rp10](https://avatars.githubusercontent.com/u/597762?v=4)](https://github.com/sc0rp10 "sc0rp10 (1 commits)")[![sivanovDespark](https://avatars.githubusercontent.com/u/172366229?v=4)](https://github.com/sivanovDespark "sivanovDespark (1 commits)")[![thomasvargiu](https://avatars.githubusercontent.com/u/732012?v=4)](https://github.com/thomasvargiu "thomasvargiu (1 commits)")[![one-smart](https://avatars.githubusercontent.com/u/852986?v=4)](https://github.com/one-smart "one-smart (1 commits)")[![acim](https://avatars.githubusercontent.com/u/2791202?v=4)](https://github.com/acim "acim (1 commits)")[![fogs](https://avatars.githubusercontent.com/u/2734563?v=4)](https://github.com/fogs "fogs (1 commits)")[![madmis](https://avatars.githubusercontent.com/u/1774703?v=4)](https://github.com/madmis "madmis (1 commits)")[![michnovka](https://avatars.githubusercontent.com/u/16553087?v=4)](https://github.com/michnovka "michnovka (1 commits)")[![olegpro](https://avatars.githubusercontent.com/u/1092443?v=4)](https://github.com/olegpro "olegpro (1 commits)")[![111ypuk](https://avatars.githubusercontent.com/u/13017017?v=4)](https://github.com/111ypuk "111ypuk (1 commits)")

---

Tags

background-jobsdaemonphpsymfonysymfony-commandsymfonySymfony2daemonsymfony3background processlong running processbackgrounding

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/wrep-daemonizable-command/health.svg)

```
[![Health](https://phpackages.com/badges/wrep-daemonizable-command/health.svg)](https://phpackages.com/packages/wrep-daemonizable-command)
```

###  Alternatives

[aws/aws-sdk-php-symfony

A Symfony bundle for v3 of the AWS SDK for PHP

36517.7M22](/packages/aws-aws-sdk-php-symfony)[coresphere/console-bundle

This bundle allows you accessing the symfony2 console via your browser

146337.3k5](/packages/coresphere-console-bundle)[phlib/console-process

Console implementation.

1833.5k2](/packages/phlib-console-process)[ibrahimgunduz34/maria-bundle

A Rule Engine Implementation For Symfony Projects

151.1k](/packages/ibrahimgunduz34-maria-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
