简体   繁体   中英

Crontab starting gunicorn installed with pip, command not found

I want to start a django application in gunicorn at reboot.

All commands below are run as user simernes

I have installed gunicorn with pip3:

pip3 install gunicorn

crontab:

crontab -e

@reboot /home/simernes/run_gunicorn.sh > /home/simernes/logfile 2>&1 &

run_gunicorn.sh

#!/bin/bash
source /home/simernes/.bashrc
cd /home/simernes/djangoapp
gunicorn --bind localhost:8000 config.wsgi

However, when I go and reboot and check the log file it says: line 4: gunicorn: command not found

Running the script on it's own from a ssh logged in terminal works fine.

Do I need to source the python environment for cron to be able to see the apps installed through pip, or something of the like?

cron runs your script in a shell with minimal environment variables and path, usually the following:

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=username>
X-Cron-Env: <USER=username>
X-Cron-Env: <HOME=/Users/username>

Which means gunicorn or anything else not in /usr/bin:/bin wont be available to your script.

What you can do is export the path to gunicorn as an environment variable by adding something like this to your crontab :

@reboot export GUNICORN=/path/to/gunicorn && /home/simernes/run_gunicorn.sh > /home/simernes/logfile 2>&1 &

And in your script you execute gunicorn thusly:

#!/bin/bash
source /home/simernes/.bashrc
cd /home/simernes/djangoapp
$GUNICORN --bind localhost:8000 config.wsgi

Maybe give full path to gunicorn in the script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM