How to set timeout for Laravel artisan
Solution 1
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
Solution 2
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
Related videos on Youtube
JackYe
Updated on September 14, 2022Comments
-
JackYe over 1 year
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.
-
JackYe almost 9 yearsThanks a lot for the nice answer. I think this a pretty good solution at the present stage
-
andcl about 6 yearsThere is no such functionality because you are responsible of coding your own stable (in terms of cpu and memory usage) commands. Of course, you can control the execution of the script following good programming practices and avoiding memory leaks by leaving a command unstably running through time.
-
miken32 over 2 yearsIt says right in the comments "Note: This directive is hardcoded to 0 for the CLI SAPI" changing the setting has no effect.