JackYe
JackYe

Reputation: 122

How to set timeout for Laravel artisan

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.

Upvotes: 6

Views: 25933

Answers (3)

Alies
Alies

Reputation: 749

You can specify max_execution_time as an option.

Example, instead of

php artisan your-slow-command

You can use:

php -d max_execution_time=900 artisan your-slow-command

Upvotes: 2

Attila Fulop
Attila Fulop

Reputation: 7011

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

Upvotes: 1

Margus Pala
Margus Pala

Reputation: 8673

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

Upvotes: 6

Related Questions