简体   繁体   English

布料无法用于Cron作业

[英]Fabric not working with cron job

I'm trying to run a deploy script using pexpect and Fabric. 我正在尝试使用pexpect和Fabric运行部署脚本。 This is what is happening: 这是正在发生的事情:

$ 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 (I'm a total shell scripting newbie, bear with me) 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 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 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')

If I run deploy.sh manually it runs perfectly fine, however, I'm receiving an email with the following traceback from the cron job: 如果我手动运行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.

I thought the installation of Fabric was system-wide, what am I missing? 我以为Fabric的安装是整个系统的,我缺少什么?

A cronjob doesn't run load .bash_profile nor your .bashrc , these are source'd by your shell. cronjob不会运行load .bash_profile或您的.bashrc ,这些都是由您的Shell来的。 But cron does not run through your shell so you have to setup the required environment variables (eg PATH ) manually from the running script. cron 通过你的shell,所以你必须设置所需的环境变量(例如运行PATH )手动运行的脚本中。

Got it working with fabric and python 使用了Fabric和python

me@local $ crontab cron_file.txt 我@本地$ crontab cron_file.txt

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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