Run Python3 module on Rasberry Pi reboot

Viewed 387

I've dusted off my Raspberry Pi (3 B+) and decided to make a home server using FastAPI.

FastAPI requires Uvicorn to run which is a Python module. Running it looks like so

python3 -m uvicorn --host 0.0.0.0 main:app

I would like this to run every time the Pi is powered on, so crontab seemed like the right solution. using sudo crontab -e, I add the line

@reboot python3 -m uvicorn --host 0.0.0.0 main:app &

Online resources show how to execute a script where you can specify an absolute path, but not how for a module

What am I doing wrong here?

2 Answers

As mentioned in the RasPi docs, setting up a systemd service is an option as well.

Aside, we use this to run our Flask sites, as well as other background services.

So just after finishing writing this question it hit me, I need to set the home variable like so

HOME=/home/pi
@reboot python3 -m uvicorn --host 0.0.0.0 main:app &

and then it works!

Related