cocur/background-process
Run shell commands as detached background processes from PHP so they keep running after the request/script ends. Start jobs, optionally get PID, poll if running, and stop them (Unix). Basic Windows support for launching only.
php artisan task:run).user.created → spawn background task).createFromPID() may return defunct processes.posix_kill($pid, 0)).symfony/process, Laravel Queues) that better fit future needs?exec() calls in commands (e.g., php artisan optimize:images).OrderProcessed event).exec()/shell_exec() calls in the codebase.BackgroundProcess (e.g., image resizing).// Before (blocking)
exec('php artisan resize:image {path}');
// After (background)
$process = new BackgroundProcess('php artisan resize:image ' . $path);
$process->run();
background_processes table with:
pid (integer)command (string)created_at (timestamp)status (enum: pending, completed, failed).Cocur\BackgroundProcess).run()).getPid(), isRunning(), stop().symfony/process unless output capture is needed.BackgroundTaskService) to abstract process management.class BackgroundTaskService {
public function run(string $command): int {
$process = new BackgroundProcess($command);
$process->run();
return $process->getPid();
}
public function stop(int $pid): bool {
try {
$process = BackgroundProcess::createFromPID($pid);
return $process->stop();
} catch (Exception $e) {
return false;
}
}
}
BackgroundProcess for PID storage/cleanup logic.How can I help you explore Laravel packages today?