我正在尝试使用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 (我是一个全新的脚本编写新手,请耐心等待)
#!/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的安装是全系统的,我错过了什么?
答案 0 :(得分:4)
cronjob不会运行加载.bash_profile
,也不运行.bashrc
,这些都是由shell提供的。但是cron
不会在您的shell中运行,因此您必须从正在运行的脚本中手动设置所需的环境变量(例如PATH
)。
答案 1 :(得分:0)
使用fabric和python
me @ local $ crontab cron_file.txt
PATH =的/ usr / local / sbin中:在/ usr / local / bin中:/ usr / sbin目录:在/ usr / bin中:/ sbin目录:/ bin中 SHELL = /斌/庆典