我用cron每隔30分钟执行一次python脚本,但它会在import语句处停止,我不明白为什么,因为我没有得到任何反馈。
在我的crontab中,我有*/30 * * * * sh exec_script.sh
文件exec_script.sh
是
cd /home/ziofil/python_scripts
python script_30_mins.py
在python脚本中我有
import logging
logging.basicConfig(filename="explicit_log.txt",level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.debug("script is executed")
# in the python_scripts folder there is a subfolder utilities/ that
# contains utility_1.py, were I define the class Utility
from utilities.utility_1 import Utility
logger.debug("Utility loaded")
utility = Utility()
logger.debug("object creation")
utility.do_your_thing()
logger.debug("done its thing")
如果我从终端执行python script_30_minutes.py
,一切正常,但是对于cron,执行在import
语句处停止(我读“脚本已执行”,但注意到其他)。
出了什么问题?
更新:我将import语句放在try/except
块中,并记录了异常:“没有名为utilities.utility_1的模块”。我还尝试将shell脚本中的PYTHONPATH变量设置为/home/ziofil/python_scripts
,但它仍然不起作用。
答案 0 :(得分:1)
cron
运行与终端环境不同的进程。您可能已在终端中设置了PYTHONPATH
,但未在您的cron环境中设置export PYTHONPATH=/home/ziofil/python_scripts/whatever:$PYTHONPATH
cd /home/ziofil/python_scripts
python script_30_mins.py
。如果是这种情况,您可以将其添加到shell脚本中:
5!/3!
答案 1 :(得分:1)
我发现了问题所在。 @UweMannl一直都是对的。在@mhawke指出cron的环境与我的终端不同之后,我认为也许python二进制文件可能不同,实际上它是:我想要exports.GetPostList = function (req, res) {
var SkipCoun = 0;
SkipCoun = parseInt(req.params.Limit) * 10;
QuestionsPostModel.QuestionsPostType.find({}, {}, { sort: { createdAt: -1 }, skip: SkipCoun, limit: 10 }, function (err, result) {
if (err) {
res.status(500).send({ status: "False", message: "Some error occurred while Find Following Users ." });
} else {
const GetUserData = (result) =>
Promise.all(
result.map(info => getPostInfo(info))
).then( result =>{ console.log(result); res.send({ status: "True", data: result }) }
).catch(err => res.send({ status: "False", Error: err }));
const getPostInfo = info =>
Promise.all([
UserModel.UserType.findOne({ '_id': info.UserId }, usersProjection).exec(),
FollowModel.FollowUserType.count({ 'UserId': info.UserId }).exec(),
RatingModel.QuestionsRating.count({ 'PostId': info._id, 'ActiveStates': 'Active' }).exec(),
RatingModel.QuestionsRating.count({ 'UserId': req.params.UserId, 'PostId': info._id, 'PostUserId': info.UserId, 'ActiveStates': 'Active' }).exec(),
AnswerModel.QuestionsAnwer.count({ 'PostId': info._id, 'ActiveStates': 'Active' }).exec(),
AnswerModel.QuestionsAnwer.find({ 'PostId': info._id }, 'AnswerText UserId Date').exec()
]).then(data => {
let UserData = data[0];
let followCount = data[1];
let ratingCount = data[2];
let UserRating = data[3];
let AnswerCount = data[4];
let Answerdata = data[5];
var AnswersArray= new Array();
return GetAnsUserData();
async function GetAnsUserData(){
for (let ansInfo of Answerdata) {
await getAnswerInfo(ansInfo);
}
let result = {
_id: info._id,
UserId: UserData._id,
UserName: UserData.UserName,
UserCategoryId: UserData.UserCategoryId,
UserCategoryName: UserData.UserCategoryName,
UserImage: UserData.UserImage,
UserCompany: UserData.UserCompany,
UserProfession: UserData.UserProfession,
Followers:followCount,
PostTopicId: info.PostTopicId,
PostTopicName: info.PostTopicName,
PostDate: info.PostDate,
PostText: info.PostText ,
PostLink: info.PostLink,
PostImage: info.PostImage,
PostVideo: info.PostVideo,
RatingCount: ratingCount,
UserRating: UserRating,
AnswersCount: AnswerCount,
Answers: AnswersArray,
};
return result;
}
function getAnswerInfo(ansInfo){
return new Promise(( resolve, reject )=>{
UserModel.UserType.findOne({'_id': ansInfo.UserId }, usersProjection, function(err, AnsUserData) {
if(err) {
res.send({status:"Fale", Error:err });
reject(err);
} else {
FollowModel.FollowUserType.count({'UserId': AnsUserData._id}, function(newerr, count) {
if(newerr){
res.send({status:"Fale", Error:newerr });
reject(newerr);
}else{
var newArray = [];
newArray.push( {
_id: ansInfo._id,
UserId: AnsUserData._id,
UserName: AnsUserData.UserName,
UserCategoryId: AnsUserData.UserCategoryId,
UserCategoryName: AnsUserData.UserCategoryName,
UserImage: AnsUserData.UserImage,
UserCompany: AnsUserData.UserCompany,
UserProfession: AnsUserData.UserProfession,
Followers: count,
Date: ansInfo.Date,
PostId: ansInfo.PostId,
PostUserId: ansInfo.PostUserId ,
AnswerText: ansInfo.AnswerText
}
);
AnswersArray.push(newArray[0]);
resolve(newArray[0]);
}
});
}
});
});
}
}).catch(error => {
console.log(error)
})
GetUserData(result);
}
});
};
而cron正在使用{{ 1}}。
我将脚本的最后一行修改为/home/ziofil/anaconda3/bin/python
,一切正常!