Solved Running sanic (python server) in background

I try to run sanic server in backgound (to close ssh session) but it terminates:

Code:
(sanic) root@vps1:/usr/local/www/new-zmanim # python -m sanic main.app --host=0.0.0.0 --port=8080 --workers=4 &
[2] 12596
[1]    Terminated                    python -m sanic main.app --host=0.0.0.0 --port=8080 --workers=4

Why it terminates?
 
Last edited by a moderator:
Code:
(sanic) root@vps1:/usr/local/www/new-zmanim # sanic main.app --host=0.0.0.0 --port=8080 --workers=4
[2024-02-26 12:27:39 +0300] [14620] [INFO]
  ┌──────────────────────────────────────────────────────────────────────────────┐
  │                                Sanic v23.12.1                                │
  │                       Goin' Fast @ [URL]http://0.0.0.0:8080[/URL]                       │
  ├───────────────────────┬──────────────────────────────────────────────────────┤
  │                       │      app: SynangogueScreen                           │
  │     ▄███ █████ ██     │     mode: production, w/ 4 workers                   │
  │    ██                 │   server: sanic, HTTP/1.1                            │
  │     ▀███████ ███▄     │   python: 3.10.5                                     │
  │                 ██    │ platform: FreeBSD-13.2-RELEASE-p10-amd64-64bit-ELF   │
  │    ████ ████████▀     │ packages: sanic-routing==23.12.0, sanic-ext==23.12.0 │
  │                       │                                                      │
  │ Build Fast. Run Fast. │                                                      │
  └───────────────────────┴──────────────────────────────────────────────────────┘

INFO:sanic.root:
  ┌──────────────────────────────────────────────────────────────────────────────┐
  │                                Sanic v23.12.1                                │
  │                       Goin' Fast @ [URL]http://0.0.0.0:8080[/URL]                       │
  ├───────────────────────┬──────────────────────────────────────────────────────┤
  │                       │      app: SynangogueScreen                           │
  │     ▄███ █████ ██     │     mode: production, w/ 4 workers                   │
  │    ██                 │   server: sanic, HTTP/1.1                            │
  │     ▀███████ ███▄     │   python: 3.10.5                                     │
  │                 ██    │ platform: FreeBSD-13.2-RELEASE-p10-amd64-64bit-ELF   │
  │    ████ ████████▀     │ packages: sanic-routing==23.12.0, sanic-ext==23.12.0 │
  │                       │                                                      │
  │ Build Fast. Run Fast. │                                                      │
  └───────────────────────┴──────────────────────────────────────────────────────┘

[2024-02-26 12:27:39 +0300] [14620] [WARNING] Sanic is running in PRODUCTION mode. Consider using '--debug' or '--dev' while actively developing your application.
WARNING:sanic.error:Sanic is running in PRODUCTION mode. Consider using '--debug' or '--dev' while actively developing your application.
[2024-02-26 12:27:42 +0300] [14625] [INFO] Sanic Extensions:
INFO:sanic.root:Sanic Extensions:
[2024-02-26 12:27:42 +0300] [14625] [INFO]   > injection [0 dependencies; 0 constants]
INFO:sanic.root:  > injection [0 dependencies; 0 constants]
[2024-02-26 12:27:43 +0300] [14625] [INFO]   > openapi [[URL]http://0.0.0.0:8080/docs[/URL]]
INFO:sanic.root:  > openapi [[URL]http://0.0.0.0:8080/docs[/URL]]
[2024-02-26 12:27:43 +0300] [14625] [INFO]   > http
INFO:sanic.root:  > http
[2024-02-26 12:27:43 +0300] [14625] [INFO]   > templating [jinja2==3.1.3]
INFO:sanic.root:  > templating [jinja2==3.1.3]
[2024-02-26 12:27:43 +0300] [14626] [INFO] Sanic Extensions:
INFO:sanic.root:Sanic Extensions:
[2024-02-26 12:27:43 +0300] [14626] [INFO]   > injection [0 dependencies; 0 constants]
INFO:sanic.root:  > injection [0 dependencies; 0 constants]
[2024-02-26 12:27:43 +0300] [14626] [INFO]   > openapi [[URL]http://0.0.0.0:8080/docs[/URL]]
INFO:sanic.root:  > openapi [[URL]http://0.0.0.0:8080/docs[/URL]]
[2024-02-26 12:27:43 +0300] [14626] [INFO]   > http
INFO:sanic.root:  > http
[2024-02-26 12:27:43 +0300] [14626] [INFO]   > templating [jinja2==3.1.3]
INFO:sanic.root:  > templating [jinja2==3.1.3]
[2024-02-26 12:27:44 +0300] [14628] [INFO] Sanic Extensions:
INFO:sanic.root:Sanic Extensions:
[2024-02-26 12:27:44 +0300] [14628] [INFO]   > injection [0 dependencies; 0 constants]
[2024-02-26 12:27:44 +0300] [14627] [INFO] Sanic Extensions:
INFO:sanic.root:  > injection [0 dependencies; 0 constants]
INFO:sanic.root:Sanic Extensions:
[2024-02-26 12:27:44 +0300] [14628] [INFO]   > openapi [[URL]http://0.0.0.0:8080/docs[/URL]]
[2024-02-26 12:27:44 +0300] [14627] [INFO]   > injection [0 dependencies; 0 constants]
INFO:sanic.root:  > openapi [[URL]http://0.0.0.0:8080/docs[/URL]]
INFO:sanic.root:  > injection [0 dependencies; 0 constants]
[2024-02-26 12:27:44 +0300] [14628] [INFO]   > http
[2024-02-26 12:27:44 +0300] [14627] [INFO]   > openapi [[URL]http://0.0.0.0:8080/docs[/URL]]
INFO:sanic.root:  > http
INFO:sanic.root:  > openapi [[URL]http://0.0.0.0:8080/docs[/URL]]
[2024-02-26 12:27:44 +0300] [14628] [INFO]   > templating [jinja2==3.1.3]
[2024-02-26 12:27:44 +0300] [14627] [INFO]   > http
INFO:sanic.root:  > templating [jinja2==3.1.3]
INFO:sanic.root:  > http
[2024-02-26 12:27:44 +0300] [14627] [INFO]   > templating [jinja2==3.1.3]
INFO:sanic.root:  > templating [jinja2==3.1.3]
[2024-02-26 12:27:44 +0300] [14625] [INFO] Starting worker [14625]
INFO:sanic.server:Starting worker [14625]
[2024-02-26 12:27:44 +0300] [14626] [INFO] Starting worker [14626]
INFO:sanic.server:Starting worker [14626]
[2024-02-26 12:27:44 +0300] [14628] [INFO] Starting worker [14628]
INFO:sanic.server:Starting worker [14628]
[2024-02-26 12:27:44 +0300] [14627] [INFO] Starting worker [14627]
INFO:sanic.server:Starting worker [14627]
 
Last edited by a moderator:
the only way I found is creating service
for some reason
service sanic stop
doesn't work, it looks like it creates empty pidfile

C-like:
#!/bin/sh

# PROVIDE: sanic
# REQUIRE: DAEMON
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable sanic:
# sanic_enable (bool):      Set to "NO" by default.
#                             Set it to "YES" to enable sanic

. /etc/rc.subr

name="sanic"
rcvar="sanic_enable"

start_cmd="${name}_start"

pidfile="/var/run/${name}.pid"

sanic_start()
{
        cd /usr/local/www/new-zmanim/
        /usr/local/www/new-zmanim/sanic/bin/python /usr/local/www/new-zmanim/sanic/bin/sanic main.app --port=8085 --host=0.0.0.0 --workers=4 & > /dev/null 2>&1
        echo ${rc_pid} > ${pidfile}
}

load_rc_config $name


run_rc_command "$1"
 
Back
Top