Java 在Linux使用crontab进行定时任务设置并执行jar

需求:通过java执行linux命令,通过crontab定时执行jar

通过java执行定时任务时需要监理shell文件和一个txt文件,通过将txt文件设置到crontab中,定时调用。

shell脚本内容: java -jar xxxx.jar

txt内容:* * * * * xxx/xxx/shell.sh

设置定时任务:crontab xxx.txt

思路很简单,代码结构也不难,但是在实现过程中有些须小问题很让人烦

首先看下面代码

    /**
     * 创建shell脚本和txt文件
     *
     * @param cron
     * @throws IOException
     */
    public void createShell(String cron) throws IOException {
        String path  = executeNewFlow("pwd");

        File file = new File(path + File.separator + "config" + File.separator+ "scheduleTask.txt");
        File shFile = new File(path  + File.separator + "config" + File.separator+  "scheduleTask.sh");
        FileWriter fileWriter = null;
        FileWriter shellWriter = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
                shFile.createNewFile();
            }
            fileWriter = new FileWriter(file);
            fileWriter.write("");
            fileWriter.write(cron + " " + shFile);
            fileWriter.flush();

            shellWriter = new FileWriter(shFile);
            shellWriter.write("#!/bin/bash" + "\n" + "source /etc/profile" + "\n"  + " java -Dserver.port=9999 -jar network-analyze-0.0.1-SNAPSHOT.jar");
            shellWriter.flush();

            executeNewFlow("chmod 777 " + shFile);
            executeNewFlow("crontab " + file);

        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            fileWriter.close();
            shellWriter.close();
        }


    }

本段代码已经实现了整个需求的思路,但是执行后查看定时任务列表crontab -l会发现找不到设置的定时任务,如果手动执行crontab xxx.txt,会发现报错

0:bad minute

errors in crontab file, can't install

此时查找问题,有的会设置环境为unix,但是我这里并没有这么解决,只是在shell中加了一行即可解决

           fileWriter.write(cron + " " + shFile + "\n");

然而改完之后还是无法执行jar,所以继续解决无法执行的问题

通过设置环境变量,进入jar所在路径再执行

            shellWriter.write("#!/bin/bash" + "\n" + "source /etc/profile" + "\n" + "cd " + path  + "\n" + " java -Dserver.port=9999 -jar network-analyze-0.0.1-SNAPSHOT.jar");

这样即可解决。

以下是完整代码块

    /**
     * 创建shell脚本和txt文件
     *
     * @param cron
     * @throws IOException
     */
    public void createShell(String cron) throws IOException {
        String path  = executeNewFlow("pwd");

        File file = new File(path + File.separator + "config" + File.separator+ "scheduleTask.txt");
        File shFile = new File(path  + File.separator + "config" + File.separator+  "scheduleTask.sh");
        FileWriter fileWriter = null;
        FileWriter shellWriter = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
                shFile.createNewFile();
            }
            fileWriter = new FileWriter(file);
            fileWriter.write("");
            fileWriter.write(cron + " " + shFile + "\n");
            fileWriter.flush();

            shellWriter = new FileWriter(shFile);
            shellWriter.write("#!/bin/bash" + "\n" + "source /etc/profile" + "\n" + "cd " + path  + "\n" + " java -Dserver.port=9999 -jar network-analyze-0.0.1-SNAPSHOT.jar");
            shellWriter.flush();

            executeNewFlow("chmod 777 " + shFile);
            executeNewFlow("crontab " + file);

        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            fileWriter.close();
            shellWriter.close();
        }


    }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值