Symfony Console
PHP
Generates crontab, Supervisor, systemd and Kubernetes configs for cron jobs and workers from your Symfony commands. The setup lives in the code, not on each server.
composer require precision-soft/symfony-consoleSymfony Console is a Symfony bundle that generates the configuration for cron jobs and workers. You declare your commands once, in the bundle configuration. Depending on the template, that becomes a crontab, Supervisor programs, systemd service units or Kubernetes CronJob and Worker values files.
It also ships a small toolkit for long-running commands: an AbstractCommand base with output helpers, and traits for memory limits, time limits and parallel instances. It is for Symfony applications with scheduled jobs and queue consumers that want this setup versioned with the code. No more editing it by hand on each machine.
Installation
Requires PHP 8.2+ and Symfony 7 or 8 (Symfony 8 itself requires PHP 8.4+, so a PHP 8.2 or 8.3 install resolves Symfony 7).
composer require precision-soft/symfony-consoleGenerating cron jobs
Declare the cron jobs in precision_soft_symfony_console.yaml:
precision_soft_symfony_console:
cronjob:
config:
template_class: PrecisionSoft\Symfony\Console\Template\CrontabTemplate
conf_files_dir: '%kernel.project_dir%/generated_conf/cron'
logs_dir: '%kernel.logs_dir%/cron'
settings:
log: true
destination_file: 'crontab'
heartbeat: true
commands:
list:
command: '%kernel.project_dir%/bin/console list'
user: 'www-data'
schedule:
minute: '*'
hour: '*'
day_of_month: '*'
month: '*'
day_of_week: '*'Then generate the files:
bin/console precision-soft:symfony:console:cronjob-createWith heartbeat: true, a heartbeat command that touches a file under logs_dir every minute is added to each generated crontab, so monitoring can alert when cron stops running. A command can override destination_file to land in a separate crontab, and destination_files lists crontab files that must exist even when no command targets them. A % in a command is escaped as \% for you.
Generating workers
Workers use the same shape. With Supervisor, each command becomes its own .conf file:
precision_soft_symfony_console:
worker:
config:
template_class: PrecisionSoft\Symfony\Console\Template\SupervisorTemplate
conf_files_dir: '%kernel.project_dir%/generated_conf/worker'
logs_dir: '%kernel.logs_dir%/worker'
settings:
number_of_processes: 1
auto_start: true
auto_restart: true
prefix: 'app-name'
user: 'root'
commands:
messenger-consume:
command: '%kernel.project_dir%/bin/console messenger:consume async'
settings:
number_of_processes: 2Run bin/console precision-soft:symfony:console:worker-create to write them. Settings are declared at config level and overridden per command. destination_sub_dir and destination_suffix split the files across subdirectories, which helps when one application config describes the workers of several machines.
With SystemdServiceTemplate, the bundle writes one concrete .service unit per instance, so nothing has to be templated at install time. The command must start with an absolute executable path (/usr/bin/php, not php), and the template handles systemd quoting and the escaping of $, ; and %. Do not point conf_files_dir at /etc/systemd/system: generation replaces the whole destination directory, so generate into a directory the bundle owns and copy or symlink the units from there.
The available templates:
| Template class | Output format |
|---|---|
CrontabTemplate | Standard crontab file |
SupervisorTemplate | Supervisor .conf files (one per command) |
SystemdServiceTemplate | systemd .service units (one per instance) |
KubernetesCronjobTemplate | Values file containing CronJobs.jobs |
KubernetesWorkerTemplate | Values file containing Jobs.workers |
Both Kubernetes templates write values files for your deployment templates, not manifests to apply directly. Set destination_file for both: the worker template has no default, and the cron template falls back to a file named crontab. Custom templates implement TemplateInterface and are tagged precision-soft.symfony.console.template.
Previewing and drift checks
Both generation commands accept three read-only modes, none of which writes anything:
| Option | Behaviour |
|---|---|
--dry-run | Lists the destination paths that would be added, changed or removed, with their status |
--diff | The same list, each pending path followed by a unified diff of its content |
--check | The same list, exiting with failure when anything is pending, for a CI drift check |
--check and --diff combine, so a failing drift check can show what drifted. A third command, precision-soft:symfony:console:logs-dir-create, creates the configured logs directories idempotently and can run at any point of a deployment.
Limits for long-running commands
MemoryLimitTrait adds a --memory-limit option, TimeLimitTrait a --time-limit option in seconds, and MemoryAndTimeLimitsTrait combines both, throwing LimitExceededException from stopScriptIfLimitsReached() so you can clean up before exiting:
use PrecisionSoft\Symfony\Console\Command\AbstractCommand;
use PrecisionSoft\Symfony\Console\Command\Trait\MemoryAndTimeLimitsTrait;
use PrecisionSoft\Symfony\Console\Exception\LimitExceededException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCommand extends AbstractCommand
{
use MemoryAndTimeLimitsTrait;
protected function configure(): void
{
$this->configureMemoryAndTimeLimits('512M', 600);
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
parent::initialize($input, $output);
$this->initializeMemoryAndTimeLimits();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
foreach ($this->getItems() as $item) {
$this->stopScriptIfLimitsReached();
$this->processItem($item);
}
} catch (LimitExceededException $limitExceededException) {
$this->warning($limitExceededException->getMessage());
}
return self::SUCCESS;
}
}InstancesTrait adds --max-instances and --instance-index for running several copies of the same command in parallel; computeInstances() returns the pair. AbstractCommand initialises $this->input, $this->output and $this->style, and provides writeln(), error(), info(), warning() and success().
Safety
Generated paths cannot escape conf_files_dir: destination_file, destination_files, destination_sub_dir and destination_suffix reject .. and backslashes at container build time, and ConfFileWriter re-checks every written path, including symlink escapes. Any value carrying a newline or another control character is rejected with InvalidConfigurationException, since it would start a new directive in the generated file. Command parts for crontab and Supervisor remain shell text, so never pass untrusted input as a command. A ; in a Supervisor command opens an INI comment and is neither quoted nor rejected; wrap such a command in a script instead.
Documentation
- README: the full configuration reference, systemd settings, contracts, services, exceptions and troubleshooting
- Example application: a runnable slice that generates and asserts every output format
- CHANGELOG