linux中使用定时任务用java调用python程序

8 篇文章 0 订阅
7 篇文章 0 订阅

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 查看定时任务日志

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值