undefinedfix
Sign in

PHP is executed under crontab. Under what conditions does it end automatically

mpeder17 edited in Mon, 24 Oct 2022

Now there is a demand in my project, which needs to send more than 10000 short messages, so I wrote a timer. However, it is found that only 400 messages can be sent at a time. What is the reason? Or how do you deal with this requirement in the project? In addition, if PHP runs under crontab request, will it run out of time or exceed the memory limit of the script

2 Replies
Googie
commented on Mon, 24 Oct 2022

First question:

  1. First rule out the cause of the program, the program log or view PHP log
  2. Find the crontab log again and find out why

The second problem: for long-term business needs, queue processing is generally more appropriate. For temporary needs, such as the crontab you mentioned, queue can also be simulated. For example, your program executes once a second, sending 10 short messages each time

The third problem: crontab's execution of PHP scripts is affected by the timeout and memory constraints in the PHP configuration file, and crontab itself does not limit it

jet_black82
commented on Tue, 25 Oct 2022

If it is found that only more than 400 messages can be sent each time, then the PHP script execution may have timed out.

You can set the script timeout in PHP code.

set_time_limit(0);

The above code means code execution time is infinite.

There is another problem to be noted: if a task script takes too long to execute, it will cause task conflict. For example, like sending text messages now, suppose the script is set to be executed once a minute. If the previous task is not finished, the second task script has been started, it may cause the phenomenon of "resending" of text messages. The lock can be set in crontab

flock -xn /tmp/test.lock -c "php /home/app/sms.php"

Refer to the article for details https://learnku.com/articles/... .

You can even output script execution errors to a log file. The command is as follows:

flock -xn /tmp/test.lock -c "php /home/app/email.php" >> /home/log/laravel.log 2>&1

2> & 1 means to redirect standard error output to standard correct output.

lock This question has been locked and the reply function has been disabled.