如何让 Laravel Queue 系统在服务器上运行 [英] How to keep Laravel Queue system running on server

查看:35
本文介绍了如何让 Laravel Queue 系统在服务器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近设置了一个 Laravel 队列系统.基础是 cronjob 调用一个命令,将作业添加到队列中,并调用发送电子邮件的第二个命令.

I recently setup a Laravel Queue system. The basics are a cronjob calls a command which adds jobs to a queue and calls a second command which sends an email.

当我 ssh 进入我的服务器并运行 php artisan queue:listen 时,系统工作,但是如果我关闭我的终端,监听器将关闭,作业堆积并排入队列,直到我 ssh 重新进入并再次运行监听.

The system works when I ssh into my server and run php artisan queue:listen, but if I close my terminal the listener shuts down and the jobs stack up and sit in queue until I ssh back in and run listen again.

让我的队列系统在后台运行而无需通过 ssh 保持连接打开的最佳方法是什么?

What is the best way to keep my queue system running in the background without needing to keep my connection open via ssh?

我尝试运行 php artisan queue:work --daemon,它完成了队列中的作业,但是当我关闭终端时,它关闭了连接和后台进程.

I tried running php artisan queue:work --daemon, and it completed the jobs in the queue, but when I closed my terminal it closed the connection and the background process.

推荐答案

运行

nohup php artisan queue:work --daemon &

当你退出时会阻止命令退出.

Will prevent the command exiting when you log out.

尾随符号 (&) 导致进程在后台启动,因此您可以继续使用 shell,而不必等到脚本完成.

The trailing ampersand (&) causes process start in the background, so you can continue to use the shell and do not have to wait until the script is finished.

nohup

nohup - 运行一个不受挂起影响的命令,输出到非 tty

nohup - run a command immune to hangups, with output to a non-tty

这会将信息输出到您运行命令的目录中名为 nohup.out 的文件中.如果您对输出不感兴趣,您可以将 stdout 和 stderr 重定向到/dev/null,或者类似地,您可以将其输出到您的普通 Laravel 日志中.例如

This will output information to a file entitled nohup.out in the directory where you run the command. If you have no interest in the output you can redirect stdout and stderr to /dev/null, or similarly you could output it into your normal laravel log. For example

nohup php artisan queue:work --daemon > /dev/null 2>&1 &

nohup php artisan queue:work --daemon > app/storage/logs/laravel.log &

但您还应该使用诸如 Supervisord 之类的东西来确保服务保持运行并在崩溃/失败后重新启动.

But you should also use something like Supervisord to ensure that the service remains running and is restarted after crashes/failures.

这篇关于如何让 Laravel Queue 系统在服务器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆