1.在java代码里利用quartz开始定时任务用java调用python程序
1.1 利用Runtime
public void jySentimentsWeibo1(){
String[] arguments = new String[] {"python3", "/root/python/sentimentsWeibo.py"};
// String[] arguments = new String[] {"D:\\Users\\cjy\\AppData\\Local\\Programs\\Python\\Python37\\python.exe", "E:\\毕业设计\\毕设项目\\covid-backend\\src\\main\\java\\io\\renren\\pyservice\\sentimentsWeibo.py"};
try{
Process process = Runtime.getRuntime()
.exec(arguments);
//检验异常情况
InputStream errorStream = process.getErrorStream();
BufferedReader error = new BufferedReader(new InputStreamReader(errorStream,"utf-8"));
String lineError;
while ( (lineError=error.readLine()) != null){
System.out.println(lineError);
}
error.close();
//获得输出
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"utf-8"));
// BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"utf-8"));
String lineIn;
while ((lineIn=in.readLine())!= null) {
System.out.println(lineIn);
}
in.close();
//程序是否正确
int re = process.waitFor();
//错误码 1是错误 0是正确
if(re==0){
System.out.println("sentimentsWeibo.py正确");
}else{
System.out.println("sentimentsWeibo.py错误");
}
} catch (IOException | InterruptedException e) {
System.out.println("调用sentimentsWeibo.py脚本并读取结果时出错:" + e.getMessage());
}
}
1.2 利用jython-standalone
public void jySentimentsWeibo2(){
System.out.println("jyWeibo2");
// //System.setProperty("python.home","/usr/bin/python3");
// Properties props = new Properties();
// props.put("python.home", "/usr/bin/python3");
// String[] s = {};
// PythonInterpreter.initialize(System.getProperties(),props,s);
// String python = "/root/python/sentimentsWeibo.py";
// PythonInterpreter interp = new PythonInterpreter();
// interp.execfile(python);
// interp.cleanup();
// interp.close();
Properties props = new Properties();
props.put("python.console.encoding", "UTF-8"); // Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
props.put("python.security.respectJavaAccessibility", "false"); //don't respect java accessibility, so that we can access protected members on subclasses
props.put("python.import.site","false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interp = new PythonInterpreter();
String python = "/root/python/sentimentsWeibo.py";
interp.exec("import sys");
interp.exec("sys.path.append('/usr/local/python3/lib')");//jython自己的
interp.exec("sys.path.append('/usr/local/python3/lib/python3.7/site-packages')");//jython自己的
interp.exec("sys.path.append('/root/python/sentimentsWeibo.py')");//我们自己写的
interp.execfile(python);
}
2.在linux中开启定时任务直接调用python程序
crontab -e 编辑定时任务
另外新建日志文件,把定时任务的结果输出到日志文件中
例如,python3 /root/python/sentimentsSina.py执行的结果输出到/root/log/sentSina.log中,便于查看代码执行结果
有异常就去相应的log文件中查看日志
*/5 * * * * flock -xn /tmp/stargiate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &'
0 3 * * * python3 /root/python/sentimentsSina.py>>/root/log/sentSina.log 2>&1 &
0 3 * * * python3 /root/python/jiebaSina.py>>/root/log/jiebaSina.log 2>&1 &
0 3 * * * python3 /root/python/sentimentsWeibo.py>>/root/log/sentWeibo.log 2>&1 &
0 3 * * * python3 /root/python/jiebaWeibo.py>>/root/log/jiebaWeibo.log 2>&1 &
0 4 * * * python3 /root/python/hubeiPredictLstm1_oneVar.py>>/root/log/hubeiLstm1OneVar.log 2>&1 &
crondtab -l 查看定时任务
systemctl restart crond 重启定时任务
tail -f /var/log/cron 查看定时任务日志