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

2023-05-16

需求:通过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();
        }


    }

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java 在Linux使用crontab进行定时任务设置并执行jar 的相关文章

随机推荐

  • [解题报告] CSDN竞赛第23期

    CSDN编程竞赛报名地址 xff1a https edu csdn net contest detail 37 1 排查网络故障 题目 A地跟B地的网络中间有n个节点 xff08 不包括A地和B地 xff09 xff0c 相邻的两个节点是通
  • CSDN竞赛第24期

    CSDN编程竞赛报名地址 xff1a https edu csdn net contest detail 38 这次写完第一道题时遇到一个奇怪的情况 xff1a 一直在 运行中 xff0c 然后发现每道题输入做任意代码都出现一直运行中 跟小
  • [Python开发] 使用python读取图片的EXIF

    使用python读取图片的EXIF 方法 使用PIL Image读取图片的EXIF 使用https pypi python org pypi ExifRead 读取图片的EXIF xff0c 得到EXIF标签 xff08 dict类型 xf
  • Partial Least Squares Regression 偏最小二乘法回归

    介绍 定义 偏最小二乘回归 多元线性回归分析 43 典型相关分析 43 主成分分析 输入 xff1a n m 的预测矩阵 X n p 的响应矩阵 Y 输出 X 和 Y 的投影 分数 矩阵 T U R n l 目标 xff1a 最大化 cor
  • 使用TensorFlow-Slim进行图像分类

    参考 https github com tensorflow models tree master slim 使用TensorFlow Slim进行图像分类 准备 安装TensorFlow 参考 https www tensorflow o
  • 使用TensorFlow Object Detection API进行图像物体检测

    参考 https github com tensorflow models tree master object detection 使用TensorFlow Object Detection API进行图像物体检测 准备 安装Tensor
  • 【Java笔记】异常处理(try-catch-finally、throws、throw)

    在 Java 语言中 xff0c 将程序执行中发生的不正常情况称为 异常 注 xff1a 开发过程中的语法错误和逻辑错误不是异常 在编写程序时 xff0c 经常要在可能出现错误的地方加上检测的代码 xff0c 如进行 x y 运算时 xff
  • 常用Log抓取方法

    1 最常用抓取方法 尽可能保存issue现场 xff0c 记录issue时间 adb pull data logs 2 使用电脑cmd连接device xff0c 并使之正常输出logcat信息 adb logcat gt 路径 main
  • jq获取和设置标签的css样式、jq给标签增加或移除class属性

    1 jQuery获取和设置标签的css样式 jQuery既可以直接获取标签的css样式 xff0c 也可以设置样式 xff0c 包括行内 内部 外部样式 xff1b 思路 xff1a 先要选取这个标签 xff0c 然后再获取或者设置样式 x
  • LwIP多TCP连接问题

    多个TCP连接的问题困扰了我很久 xff0c 前段时间解决了这个问题 xff0c 现在写下我的感受 xff1a 多个TCP可以绑定多个端口 xff0c 这里我是绑定一个端口 xff0c 这样更加复合实际应用 xff08 我的多个TCP的功能
  • GitHub Pages 绑定个人域名

    文章目录 一 购买域名二 配置域名解析三 GitHub Pages 绑定个人域名四 本地设置 CNAME五 重新发布网站 之前我们已经使用 github 搭建好了个人网站 xff0c 可以通过 xxx github io 来访问自己的网站
  • Pycharm提示 Unresolved reference 的解决办法

    有时候a py和b py在一个目录里面 xff0c 但是在a py种写import b有时会提示Unresolved reference xff0c Pycharm常见 xff0c 解决办法是setting gt Project gt Pr
  • 解决idea新建maven项目时一直loading问题

    idea里新建maven项目时 xff0c 在create from archetype时 xff0c 一直显示loading archetype list 原因 idea一直读自己的配置里缓存导致的 解决 方案一 把 C Users Ad
  • 安装windows时install.wim文件过大的解决方案

    安装windows时install wim文件过大的解决方案 问题描述解决方法 问题描述 windows镜像文件中 xff0c install wim大于4GB 直接解压镜像到u盘制作启动盘的方法只能用fat32格式 xff0c 不支持大于
  • Ubuntu 14 桌面图标消失解决办法

    1 使用ctrl 43 alt 43 F1进入字符命令界面 xff0c 登录账户 2 使用命令mv config config bk xff0c 相当于删除备份文件 xff0c 重启后页面正常
  • 在vue项目中使用Lottie动画(随看随用)

    前言 xff1a Lottie是一个IOS xff0c Android和React Native库 xff0c 可以实时渲染动画 xff0c 动画被转化成JSON文件 xff0c 节省了很多资源 xff0c 允许应用程序像使用静态图像一样轻
  • 对于python中“FileNotFoundError: [Errno 2] No such file or directory”的解决办法

    在我们使用vscode运行Python代码时遇到的情况 一 出现原因 xff1a 这里是由于Vscode中 xff0c python里的路径是相对与工作目录来进行定位的 所以在多级目录情况下 xff0c 若不设置绝对路径 xff0c 往往找
  • 《构建Debian的精彩世界》

    2007 10 06 星期六 12 04 darkblue 这段时间在公司一直使用Ubuntu系统 xff0c 其实刚来的时候用的是Debian xff0c 也是我头一次安装 配置和使用Debian系统 后来为了统一开发环境 xff0c 才
  • 由于找不到VCRUNTIME140_1.dll,无法继续执行代码。重新安装程序可能会解决此问题

    重装office之后双击Excel和PowerPoint无法正常打开 并弹出如下提示 并且 docx文件和 xls文件图标变成了下图所示 双击 docx xff0c 弹出Global Labeling Management Print To
  • Java 在Linux使用crontab进行定时任务设置并执行jar

    需求 xff1a 通过java执行linux命令 xff0c 通过crontab定时执行jar 通过java执行定时任务时需要监理shell文件和一个txt文件 xff0c 通过将txt文件设置到crontab中 xff0c 定时调用 sh