0

I have a small script that uses flask to listen to API requests and forwards them to Home Assistant and InfluxDB. It currently has a dummy name, "script.py" and is placed in /usr/local/bin. When I run it standalone, by using python3 script.py, it works fine.

Since I want this script to be always running, I'd like it as a service. I created the following service in /usr/local/etc/rc.d/

#!/bin/sh

# PROVIDE: rest
# REQUIRE: DAEMON NETWORKING
# BEFORE: LOGIN
# KEYWORD: shutdown

. "/etc/rc.subr"
location="/usr/local/bin"
name="script"
rcvar=rest_enable

: ${rest_enable:="YES"}
logfile="/var/log/rest/${name}.log"
command="$location/$name.py"
command_args="$1"
command_interpreter="/usr/local/bin/python3"

load_rc_config $name
run_rc_command "$1"

The output is

Starting script.
Traceback (most recent call last):
 File /usr/local/bin/script.py", line 4, in <module>
  from flask import Flask, request, Response
ModuleNotFoundError: No module named 'flask'
/usr/local/etc/rc.d/rest: WARNING: failed to start script

Running a whereis on python3 produces the following:

root@grafana:/usr/local/etc/rc.d # whereis python3
python3: /usr/local/bin/python3

So from my understanding this should be fine. What am I missing?

1 Answers1

0

There's broadly speaking three alternatives:

  1. You have installed flask as a user by running pip3 install flask
  2. You have a virtual env as user.

Install the flask module system wide, specify an absolute path to flask in your script, or set up a proper venv.

vidarlo
  • 6,654
  • 2
  • 18
  • 31