结构不适用于 cron 作业

Fabric not working with cron job

我正在尝试 运行 使用 pexpect 和 Fabric 的部署脚本。这是正在发生的事情:

$ crontab -l
# m h  dom mon dow   command
30 04 * * * /bin/bash /home/pedregal/Deploy/deploy.sh

$ ls -l
total 20
-rwxrwxr-- 1 pedregal pedregal  239 Nov 11 09:00 deploy.sh
-rw-rw-r-- 1 pedregal pedregal  271 Nov  9 21:33 expectfile.py
-rw-rw-r-- 1 pedregal pedregal  900 Nov 10 07:28 fabfile.py

deploy.sh(本人一共shell脚本新手,多多包涵)

#!/bin/bash

EXPECT_FILE=/home/pedregal/Deploy/expectfile.py
LOG_FILE=/home/pedregal/Deploy/deploy.log

touch $LOG_FILE
python $EXPECT_FILE > $LOG_FILE 2>&1
mail -s "Auto-update log" cbustios@elpedregalsa.com < $LOG_FILE

expectfile.py

# -*- coding: utf-8 -*-
import pexpect
import sys

child = pexpect.spawn('fab piura deploy', echo=False)
child.logfile = sys.stdout
child.expect('user:')
child.sendline('cesarbustios')
child.expect('password:')
child.sendline('************')
child.expect(pexpect.EOF)

fabfile.py

# -*- coding:  utf-8 -*-
from __future__ import with_statement
import os
from os.path import join, dirname

from dotenv import load_dotenv
from fabric.api import cd, env, prefix, run, sudo, warn_only


DOTENV_PATH = join(dirname(__file__), '.env')
GESTAGRO_PATH = '/home/pedregal/Projects/gestagro/gestagro/'
VENV_PATH = '/home/pedregal/Environments/gestagro/venv/bin/activate'
ACTIVATE_VENV = 'source %s' % (VENV_PATH)
load_dotenv(DOTENV_PATH)


def piura():
    env.hosts = [
        'pedregal@192.168.5.20:22',
    ]
    env.passwords = {
        'pedregal@192.168.5.20:22': os.environ.get('PASSWORD_PIURA'),
    }


def deploy():
    with cd(GESTAGRO_PATH), prefix(ACTIVATE_VENV), warn_only():
        run('hg pull -u')
        run('python manage.py migrate --no-color')
        run('python manage.py collectstatic --noinput --no-color')
        sudo('nginx -s reload')
        sudo('supervisorctl restart gestagro')

如果我 运行 deploy.sh 手动 运行 完全没问题,但是,我收到一封电子邮件,其中包含来自 cron 作业的以下回溯:

Traceback (most recent call last):
  File "/home/pedregal/Deploy/expectfile.py", line 5, in <module>
    child = pexpect.spawn('fab piura deploy', echo=False)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/pty_spawn.py", line 194, in __init__
    self._spawn(command, args, preexec_fn, dimensions)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/pty_spawn.py", line 267, in _spawn
    'executable: %s.' % self.command)
pexpect.exceptions.ExceptionPexpect: The command was not found or was not executable: fab.

我以为 Fabric 的安装是系统范围的,我错过了什么?

cronjob 不会 运行 加载 .bash_profile 也不会加载您的 .bashrc,这些是由您的 shell 提供的。但是 cron 通过您的 shell 而不是 运行 所以您必须从运行宁脚本。

得到它与织物一起使用 python

me@local $ crontab cron_file.txt

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin SHELL=/bin/bash

          • cd /home/me/scriptz && fab server_backup