Search code examples
pythonflasksystemd

Gunicorn3's service unit file throws errors from a shell command defined in a python script


I have a flask app that is now served by gunicorn3 + ngingx , the flask app uses a python script that uses an internal command to perform a few actions and throws the result on to the html page .

Everything works fine, except that it seems gunicorn3 calls the flask app, it throws an error(perl based i think because of the internal command) . I suspect a permission issue , but i'm not sure.

root@dev-elk-app01:/etc/init.d# service nexus status
● nexus.service - Gunicorn instance For Serving the flask App
   Loaded: loaded (/etc/systemd/system/nexus.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2021-12-24 10:23:42 UTC; 29s ago
 Main PID: 2427 (gunicorn3)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/nexus.service
           ├─2427 /usr/bin/python3 /usr/bin/gunicorn3 --log-syslog nexus:app
           └─2451 /usr/bin/python3 /usr/bin/gunicorn3 --log-syslog nexus:app

Dec 24 10:23:42 dev-elk-app01 systemd[1]: Started Gunicorn instance For Serving the Nexus App.
Dec 24 10:23:42 dev-elk-app01 gunicorn3[2427]: [2021-12-24 10:23:42 +0000] [2427] [INFO] Starting gunicorn 19.7.1
Dec 24 10:23:42 dev-elk-app01 gunicorn3[2427]: [2021-12-24 10:23:42 +0000] [2427] [INFO] Listening at: http://127.0.0.1:8000 (2427)
Dec 24 10:23:42 dev-elk-app01 gunicorn3[2427]: [2021-12-24 10:23:42 +0000] [2427] [INFO] Using worker: sync
Dec 24 10:23:42 dev-elk-app01 gunicorn3[2427]: [2021-12-24 10:23:42 +0000] [2451] [INFO] Booting worker with pid: 2451
Dec 24 10:24:06 dev-elk-app01 gunicorn3[2427]: Can't locate object method "ReadHistory" via package "Term::ReadLine::Stub" at /usr/local/share/perl/5.26.1/Shell
lines 1-15/15 (END)

To test the outcome , i stopped the service and used just the gunicorn to host the app and that worked , no errors this time .

root@dev-elk-app01:/var/www/html/flask_nexus# /usr/bin/gunicorn3 --log-syslog nexus:app
[2021-12-24 10:37:18 +0000] [2592] [INFO] Starting gunicorn 19.7.1
[2021-12-24 10:37:18 +0000] [2592] [INFO] Listening at: http://127.0.0.1:8000 (2592)
[2021-12-24 10:37:18 +0000] [2592] [INFO] Using worker: sync
[2021-12-24 10:37:18 +0000] [2595] [INFO] Booting worker with pid: 2595


Solution

  • Update : i have fixed the problem ,i had enabled verbose in all the scripts and it seemed gunicorn3 had trouble with huge data that it was receiving from our internal perl script . as seen below with the BLOB data .

    Dec 24 18:53:33 dev-elk-app01 gunicorn3[26598]: [1B blob data]
    Dec 24 18:53:33 dev-elk-app01 gunicorn3[26598]: Can't locate object method "ReadHistory" via package "Term::ReadLine::Stub" at /usr/local/share/perl/5.26.1/Shel
    Dec 24 18:53:34 dev-elk-app01 gunicorn3[26598]: Can't locate object method "ReadHistory" via package "Term::ReadLine::Stub" at /usr/local/share/perl/5.26.1/Shel
    Dec 24 18:53:35 dev-elk-app01 gunicorn3[26598]: Can't locate object method "ReadHistory" via package "Term::ReadLine::Stub" at /usr/local/share/perl/5.26.1/Shel
    Dec 24 18:53:36 dev-elk-app01 gunicorn3[26598]: Can't locate object method "ReadHistory" via package "Term::ReadLine::Stub" at /usr/local/share/perl/5.26.1/Shel
    

    since the same script worked when we invoked from shell, i used TERM=linux in the systemd unitfile under the [service] tag, in an attempt to emulate the linux terminal and it worked . NO more errors ,the file fetch now happens successfully .