Blog

If you’re using Laravel’s Nightwatch, you’ll want to make sure the Nightwatch agent runs continuously, even after system restarts or unexpected crashes.

A robust way to do this is to run the nightwatch:agent command under Supervisor — a process control system that can monitor and automatically restart your Laravel background workers.

In this guide, I’ll walk you through setting up Supervisor to manage your Laravel Nightwatch agent.

Step 1: Edit Supervisor Configuration

Open the Supervisor config file using a text editor. On many systems, this is done via:

sudo nano /etc/supervisord.conf

Or, depending on your OS and Supervisor version, you may prefer to create a separate config file under /etc/supervisord.d/.

Add the following block (replace paths and user as needed)

[program:davidvandertuijn_nightwatch]
process_name=%(program_name)s
directory=/var/www/vhosts/davidvandertuijn.nl
command=/opt/plesk/php/8.3/bin/php artisan nightwatch:agent
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=davidvandertuijn
redirect_stderr=true
stdout_logfile=/var/www/vhosts/davidvandertuijn.nl/storage/logs/nightwatch.log
startsecs=0
stopsignal=TERM
stopwaitsecs=3600

What this does:

  • command: Runs the Laravel Nightwatch agent using your PHP 8.3 binary.
  • autorestart: Automatically restarts the process if it fails.
  • stdout_logfile: Logs Nightwatch output to storage/logs/nightwatch.log.
  • user: Ensures the process runs as the correct system user (not root).
  • stopwaitsecs=3600: Gives the agent time to gracefully shut down when needed (especially during long tasks).

Step 2: Reload Supervisor

After saving your config file, tell Supervisor to reread and apply the changes:

sudo supervisorctl reread
sudo supervisorctl update

Then, start the Nightwatch agent:

sudo supervisorctl start davidvandertuijn_nightwatch

You can also check its status:

sudo supervisorctl status

You’re Done!

Your Laravel Nightwatch agent is now running continuously in the background, and it will automatically restart if it crashes or when the server reboots.
Using Supervisor is a simple but powerful way to ensure your background processes stay alive — especially for production environments.

Zoeken