django中执⾏shell脚本
最开始启动⼀个进程:
1:p = create_multiprocess(target=getsmoketest,
args=[intranetip, username, password])
def create_multiprocess(target, args=()):
p = multiprocessing.Process(target=target, args=args)
p.start()
return p
2script_path = os.path.join(BASE_DIR, "common/smoketestreport/runsmoketestcases.sh")
args = ["sh", "-x", script_path, self.ip, self.user, self.password, BASE_DIR]
exit_value = 1
try:
process = subprocess.Popen(args, bufsize=1, stdout=subprocess.PIPE, close_fds=True, preexec_fn=os.setsid,
universal_newlines=True)
while process.poll() is None:
out_put_log = adline()
self.logger.output_shelllog_to_logger(out_put_log)
process.wait()
exit_value = urncode
except Exception, e:
(e.message)
if exit_value != 0:
err_msg = "[DevOps_Error]:Execute " + script_path + " error!!!"
(err_msg)
return self.EXCEPTION
return self.NORMAL
这样算是开始执⾏脚本了,但是在shell脚本有个问题:
1脚本要识别这个Django框架路径,也就是⽤到了BASE_DIR这个路径
2在shell脚本开启spawn的时候,最好设置set timeout -1这句话
代码:
/usr/bin/expect <<EOF
set timeout -1
spawn ssh -o "StrictHostKeyChecking no" $user@$ip \[ \! -d $SMOKETESTCASESSAVEDIR/ \]  && mkdir $SMOKETESTCASESSAVEDIR/  expect {
"$ip's password:" { send "$password\r"; exp_continue}
}
spawn scp -o "StrictHostKeyChecking no" $script_path/$sshFileName $user@$ip:$SMOKETESTCASESSAVEDIR
expect {
"$ip's password:" { send "$password\r"; exp_continue}
}
spawn scp -o "StrictHostKeyChecking no" $script_l $user@$ip:$SMOKETESTCASESSAVEDIR
expect {
"$ip's password:" { send "$password\r"; exp_continue}
}
spawn ssh -o "StrictHostKeyChecking no" $user@$ip sh $SMOKETESTCASESSAVEDIR/$sshFileNamegetsavefilename
expect {
"$ip's password:" { send "$password\r"; exp_continue}
}
spawn scp -o "StrictHostKeyChecking no" $user@$ip:$SMOKETESTCASESSAVEDIR/checkprojectrun.log $SMOKETESTCASESSAVEDIR  expect {
"$ip's password:" { send "$password\r"; exp_continue}
}
EOF
后续再继续深⼊研究

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。