bash - Crontab an alias which runs a specific virtualenv's Python interpreter, why doesn't work? -
hello.
i'm trying execute asynchronous django custom command using macos cron jobs nothing seems work.
first, tried write bash file sources virtualenv , executes manage.py custom command:
#!/bin/bash source "/users/airiefenix//workspaces/ytsm_container/venv/bin/activate" && python /.../manage.py my_command
but got
file "manage.py", line 17, in <module> "couldn't import django. sure it's installed , (...)
so source not working on bash script. tried many methods, including replacing source . (dot operator), splitting script in 2 or more lines, etc. apparently there's no way activate virtualenv bash script gave , made alias
$ alias python_ytsm="/users/airiefenix/workspace/ytsm_container/venv/bin/python3.6"
running "python_ytsm /.../manage.py my_command" works when add cron list:
$ crontab -e
cron file:
01 * * * * python_ytsm "/users/airiefenix/workspace/ytsm_container/project/manage.py my_command"
absolutely nothing happens. checked cron jobs list crontab -l , script there. added other lines crontab file basic commands such "touch file.txt" or "echo 'hello' >> somefile.txt" , jobs executing normally.
what's problem? alias? "custom/virtualenv" python interpreter? i'd love give more details macos extremely scarce full while logging cron jobs.
i'm using macos sierra, know cron supposedly deprecated me it's easier using launchdl unless can't make work through cron i'd keep using it. thanks.
aliases disabled default in noninteractive shells, if source file defines them, won't honored. moreover, dotfiles aren't sourced on noninteractive shell startup, .bashrc
, .bash_profile
, .profile
, or place might have defined alias won't evaluated when command called cron.
if really want alias, consider:
# instruct cron use bash shell shell=/bin/bash # . "$home/.bashrc" <- assumes .bashrc defines alias python_ytsm # shopt -s expand_aliases <- turns aliases on; off-by-default in noninteractive context # ...and regular command. 01 * * * * . "$home/.bashrc" && shopt -s expand_aliases && python_ytsm "/users/airiefenix/workspace/ytsm_container/project/manage.py my_command"
but it's better practice not rely on them.
Comments
Post a Comment