Daemonize worker processes
Learn how Prefect flow deployments enable configuring flows for scheduled and remote execution with workers.
When running workflow applications, it’s helpful to create long-running processes that run at startup and are resilient to failure.
This guide shows you how to set up a systemd service to create long-running Prefect processes that poll for scheduled flow runs, including how to:
- create a Linux user
- install and configure Prefect
- set up a systemd service for the Prefect worker or
.serve
process
Prerequisites
- An environment with a linux operating system with systemd and Python 3.9 or later.
- A superuser account that can run
sudo
commands. - A Prefect Cloud account, or an instance of a Prefect server running on your network.
If using an AWS t2-micro EC2 instance with an
AWS Linux image, you can install Python and pip with sudo yum install -y python3 python3-pip
.
Steps
A systemd service is ideal for running a long-lived process on a Linux VM or physical Linux server.
You will use systemd and learn how to automatically start a
Prefect worker or
long-lived serve
process when Linux starts.
This approach provides resilience by automatically restarting the process if it crashes.
Step 1: Add a user
Create a user account on your Linux system for the Prefect process. You can run a worker or serve process as root, but it’s best practice to create a dedicated user.
In a terminal, run:
When prompted, enter a password for the prefect
account.
Next, log in to the prefect
account by running:
Step 2: Install Prefect
Run:
This guide assumes you are installing Prefect globally, rather than a virtual environment.
If running a systemd service in a virtual environment, change the ExecPath.
For example, if using venv, change the ExecPath
to target the prefect
application in the bin
subdirectory of your virtual environment.
Next, set up your environment so the Prefect client knows which server to connect to.
If connecting to Prefect Cloud, follow the instructions to obtain an API key, and then run the following:
When prompted, choose the Prefect workspace to log in to.
If connecting to a self-hosted Prefect server instance instead of a Prefect Cloud account, run the following command, substituting the IP address of your server:
Run the exit
command to sign out of the prefect
Linux account.
This command switches you back to your sudo-enabled account where you can run the commands in the
next section.
Step 3: Set up a systemd service
See the section below if you are setting up a Prefect worker.
Skip to the next section if you are setting up a
Prefect .serve
process.
Setting up a systemd service for a Prefect worker
Move into the /etc/systemd/system
folder and open a file for editing.
We use the Vim text editor below.
Make sure you substitute your own work pool name.
Setting up a systemd service for .serve
Copy your flow entrypoint Python file and any other files needed for your flow to run into the
/home
directory (or the directory of your choice).
Here’s a basic example flow:
To make changes to your flow code without restarting your process, push your
code to git-based cloud storage (GitHub, BitBucket, GitLab) and use flow.from_source().serve()
,
as in the example below.
Make sure you substitute your own flow code entrypoint path.
If you change the flow entrypoint parameters, you must restart the process.
Move into the /etc/systemd/system
folder and open a file for editing.
This example below uses Vim.
Step 4: Save, enable, and start the service
To save the file and exit Vim, hit the escape key, type :wq!
, then press the return key.
Next, make systemd aware of your new service by running:
Then, enable the service by running:
This command ensures it runs when your system boots.
Next, start the service:
Run your deployment from the UI and check the logs on the Flow Runs page.
You can see if your daemonized Prefect worker or serve process is running, and the
Prefect logs with systemctl status my-prefect-service
.
You now have a systemd service that starts when your system boots, which will restart if it ever crashes.
Was this page helpful?