# Background processes and scheduled jobs

Keep programs such as queue workers running all the time, run commands at set times, and get told when a scheduled job stops running or starts failing.

Two pages in a server's **Services** tab run commands for you without anyone signed in:

- **Background processes** (technical name: daemons, Supervisor, PM2) keep a program running all the time, such as the worker that sends your shop's emails. If one stops, it is started again for you, and they all start again when the server boots.
- **Scheduled jobs** (technical name: cron) run a command at set times, such as a nightly report.

## Background processes

![Background processes on a server: four processes, each running, with the site it belongs to and its number of copies](https://internal.fuzops.com/docs-img/server-background-processes.webp "Background processes: each one with its site and how many copies run.")

### Add one

1. Open the server's **Services** tab, then **Background processes**, and choose **Add background process**.
2. Choose **For which site?** and the kind. A **Queue worker** for Laravel and a **Message worker** for Symfony come with settings that suit most sites; you can also run **My own command**.
3. Fill in the **Command**: one line, exactly as you would type it on the server.
4. Check **Run as**: the site's account is chosen for you. A program running as root can change anything on the server, so choose root only when the command really needs it.
5. Set **Copies** (1 to 20 running side by side) and keep **Restart after each site update** on, so it always runs the newest version of your site. It finishes the job it is doing first.
6. Save. It is started straight away and checked to see that it stays running. If it does not, the previous setup is put back.

**More options** holds **Folder to run it in**, **If it stops**, **Time to finish before stopping** and, for queue workers, **Connection**, **Queues**, **Attempts per job**, **Time limit per job** and **Memory limit**.

A row that runs normally shows only a green mark; one that does not says **Not all running**. Its menu has **Restart**, **Stop**, **Start**, **Check it now**, **Edit** and **Remove…**. When one fails, **See what went wrong** shows its last messages.

Whether each process runs is read again every five minutes while the server is online, and the row says when (**checked 2 minutes ago**). If the server has not been read for more than twelve minutes, the row does not claim it runs: it says **Last checked …** and what it was then. A process that stops without being stopped from Fuzops, or that keeps stopping and starting again, shows on the server's **Health** page within one reading and on **Home** after ten minutes, with a link back here.

### Coming from PM2 or Supervisor

Fuzops already keeps your apps and background jobs running, restarts them if they stop and starts them when the server boots. This does the job of PM2 and Supervisor, so there is nothing to install. Bring your programs over in one of two ways, from the **Coming from PM2 or Supervisor?** panel at the top of the page:

- **Bring in from Supervisor or PM2**: paste a Supervisor program (the `[program:name]` part of a file in `/etc/supervisor/conf.d`) or a PM2 ecosystem file (JSON, or the usual `module.exports = { apps: [...] }` form). Fuzops reads it as text, never runs it, and shows what it would create, with a note for each setting it cannot carry over. Tick what you want and choose **Bring in and start**. Then stop the old programs yourself so the jobs do not run twice: Fuzops shows the exact command to copy for each one.
- **Find them on this server**: Fuzops looks for Supervisor programs and PM2 apps on the server without changing anything, and lists them. **Move to Fuzops** (or **Move all**) does the switch for you, safely: see the next section.

What carries over: the command (for PM2: the script, its arguments and the program that runs it, such as node or bash), the folder, the account, the number of copies (Supervisor's `numprocs`, PM2's `instances`), whether it starts again when it stops, the extra settings (environment) and the time it gets to finish. A file whose values are computed when it runs (for example `process.env` or `require`) cannot be read as text: paste it as JSON with the values written out. A Supervisor value such as `%(ENV_HOME)s` is refused for the same reason: write the real value. Log files are not needed: what a program prints is kept for you and **Check it now** shows the last lines.

PM2's cluster mode becomes copies: each copy is its own program and knows its number (1, 2, …). A website that runs as several copies keeps that on its own **Update settings** page (**How the app runs**).

### Move a job from Supervisor or PM2

**Move to Fuzops** never stops a job and never lets it run twice:

1. Fuzops starts its own copy and checks that it keeps running.
2. Only then is the old one stopped and switched off in Supervisor or PM2. Its settings are kept on the server.
3. If the Fuzops copy does not start, or stops during the check, the old one is left exactly as it was and the copy is removed.

A job that must never run twice at the same moment, such as a scheduler, can be marked **Stop the old one first**: it pauses for the few seconds the new copy needs to start, and if that copy does not start, the old one is started again. Fuzops suggests this for jobs that look like schedulers; queue workers are fine either way.

**Put it back**, in the moved process's menu, undoes a move: the old program runs again from its kept settings, and only once it runs is the Fuzops copy removed. If the old one does not start, the Fuzops copy keeps running.

A website that runs under PM2 is moved from its own site page instead, with **Run this app with Fuzops**: Fuzops also switches the web server to the new copy there, without a break. See [Run a website's app with Fuzops](https://internal.fuzops.com/docs/bring-in-a-server#run-a-websites-app-with-fuzops). Nothing moves without you: every move and every undo is a click after a preview.

### Background workers of a site

A site's **Updates** tab has **Background workers**: the same processes, showing only that site's. See [Sites](https://internal.fuzops.com/docs/sites). Laravel sites turn on **Horizon**, **Reverb**, the **Queue worker** and **Scheduled tasks** from the site's **Laravel** tab, which checks the app first and sets everything up: see [Turn on Horizon and Reverb](https://internal.fuzops.com/docs/guides/turn-on-horizon-and-reverb). They then appear here like any other background process.

## Scheduled jobs

![Scheduled jobs: the Laravel schedule switch for two sites and one job running on time](https://internal.fuzops.com/docs-img/server-scheduled-jobs.webp "Scheduled jobs. Times follow the server's clock.")

### Add one

1. Open **Scheduled jobs** and choose **Add scheduled job**.
2. Give it a **Name** (only used to recognise it in the list) and the **Command**, one line.
3. Choose **How often?** from the list, or write a cron expression. Fuzops repeats it in words, for example "every day at 02:30".
4. Choose **Run as**. The site's account is safer than root.
5. Keep **Tell me when this job stops running or fails** ticked. Fuzops then records each run, whether it worked and how long it took.
6. Save.

Times follow the server's clock, shown at the top of the page (for example UTC). Change the time zone under **Maintenance** › **Time zone and server name**.

Each job shows **Running on time**, **Late** (a run did not finish within **How long may a run take?**) or **Failing**. Its menu has **Run now**, **See the last run**, **Edit** and **Remove…**. After **Run now**, the row says how that run went ("Run by hand 2 minutes ago: it failed (error code 3).") and a failed run turns the job red, with **See what went wrong**, even when the job is not watched. You are told about late and failing jobs as set on the **Notifications** page.

### Laravel sites with their own schedule

A Laravel site can have tasks of its own that need to run on time. **Laravel sites with their own schedule**, at the top of the page, lists your Laravel sites with **Turn on** beside each. It then checks every minute for work that is due.

## Related

- [Services and self-hosted tools](https://internal.fuzops.com/docs/services)
- [Monitoring, alerts and the health report](https://internal.fuzops.com/docs/monitoring)
