- 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.
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-livedserve
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:prefect
account.
Next, log in to the prefect
account by running:
Step 2: Install Prefect
Run: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:
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.
my-prefect-service.service
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:
my_file.py
flow.from_source().serve()
,
as in the example below.
my_remote_flow_code_file.py
/etc/systemd/system
folder and open a file for editing.
This example below uses Vim.
my-prefect-service.service
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:
systemctl status my-prefect-service
.
You now have a systemd service that starts when your system boots, which will restart if it ever crashes.