Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timeout for Laravel artisan

Tags:

php

laravel

How to set timeout for an artisan job? And when time out the job will be killed.

I'm using Laravel 5.0 on my application.I scheduled some artisan jobs every minute in app/Console/Kernel.php. When I grep those jobs

ps -ef | grep artisan

It seems that every job is running in separate process.

root 23322 1 0 13:44 ? 00:00:00 xxx/php-5.5.7/bin/php artisan fetchTopic 0 10 1

root 23324 1 0 13:44 ? 00:00:00 xxx/php-5.5.7/bin/php artisan fetchTopic 0 10 2

root 23326 1 0 13:44 ? 00:00:00 xxx/php-5.5.7/bin/php artisan fetchComment

And when one job finish its task, the process will be killed automatically. But it is strange that some of the processes are not killed normally. And over time, more and more strange processes are permanent which lead to CPU 100%. So I want to set an timeout for the artisan job, when time out the job will be killed automatically.

like image 423
JackYe Avatar asked Apr 25 '15 06:04

JackYe


2 Answers

I checked the Laravel code and there is no graceful way of killing these long artisan processes.

However you can limit overall php execution time on your own risk. You can use function set_time_limit() for this or add max_execution_time in your php.ini

like image 81
Margus Pala Avatar answered Nov 14 '22 01:11

Margus Pala


I had the same issue and changing the value of max_input_time in the cli php.ini file lead to the desired effect.

Example: (/etc/php/7.3/cli/php.ini)

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 99

like image 27
Attila Fulop Avatar answered Nov 14 '22 01:11

Attila Fulop