Laravel Horizon stops after I close ssh

2.1k views Asked by At

I am just getting started with Laravel and Horizon so I am sorry if my question is a bit out there.

I have setup Laravel with Horizon and a Redis database. Everything works fine as long as I have my SSH connection open with the php artisan horizon command running in there. As soon as I close the SSH session, it stops working.

I am new with these things so I am wondering what solution there would be. I found someone saying you should do php artisan horizon & but that seems to work for a few minutes and then nothing.

The system is as setup on a webserver so maybe a cronjob can fix it. But my experience with those things is very limited. I hope someone out there can assist.

3

There are 3 answers

6
Hussein On

a fast solution would be to use the screen command if the server os is linux:

  1. ssh into the server.
  2. run screen.
  3. run the command php artisan horizon.
  4. hit Ctrl + a, then d, to detach from the current session, then exit from ssh
2
ceejayoz On

https://laravel.com/docs/5.7/horizon#deploying-horizon

If you are deploying Horizon to a live server, you should configure a process monitor to monitor the php artisan horizon command and restart it if it quits unexpectedly. When deploying fresh code to your server, you will need to instruct the master Horizon process to terminate so it can be restarted by your process monitor and receive your code changes.

Laravel recommends Supervisor for this:

[program:horizon]
process_name=%(program_name)s
command=php /home/forge/app.com/artisan horizon
autostart=true
autorestart=true
user=forge
redirect_stderr=true
stdout_logfile=/home/forge/app.com/horizon.log
0
Cy Rossignol On

When we start a long-running command (like Horizon) in a terminal session, and then exit that session, the terminal sends the "hang-up" signal (SIGHUP) to that process which usually causes it to exit.

For a basic solution, we can invoke the command using the nohup utility:

nohup php artisan horizon >> horizon.log 2>&1 &

This command starts Horizon in the background and sends output to horizon.log in the current working directory. Then, when we close the terminal, the process ignores the hang-up signal, so Horizon continues to run indefinitely. However, if Horizon crashes later, it won't restart itself. This is why Laravel recommends running Horizon with a process manager like Supervisor--we can configure supervisord to restart Horizon automatically when it exits unexpectedly to prevent a service outage.

A modification of the command above gives us a bit of the same functionality:

nohup sh -c 'while true; do php artisan horizon; done' >> horizon.log 2>&1 &

This command restarts Horizon each time it exits and may suffice for extremely-limited hosting environments. Of course, it won't start at boot without additional configuration, but I can't offer any useful advice without knowing more about the hosting environment.

As a final note, remember that we need to restart the master Horizon process when we deploy new code to the server so Horizon reloads any changes. We can execute the following command and Horizon will restart if we ran it using the second command above:

php artisan horizon:terminate