109 lines
4.1 KiB
Java
109 lines
4.1 KiB
Java
|
|
package com.tongran.agent.client.utils;
|
||
|
|
|
||
|
|
import java.io.*;
|
||
|
|
import java.util.concurrent.TimeUnit;
|
||
|
|
|
||
|
|
public class CrontabManager {
|
||
|
|
|
||
|
|
// // 要添加的定时任务
|
||
|
|
// private static final String CRON_JOB = "*/10 * * * * /usr/local/tongran/sbin/test2.sh >/dev/null 2>&1";
|
||
|
|
// // 用于判断是否已存在的关键标识(可以是脚本路径)
|
||
|
|
// private static final String JOB_IDENTIFIER = "/usr/local/tongran/sbin/test2.sh";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 检查并添加定时任务
|
||
|
|
*/
|
||
|
|
public static boolean ensureCronJobExists(String CRON_JOB, String JOB_IDENTIFIER) {
|
||
|
|
try {
|
||
|
|
// 1. 读取当前用户的 crontab
|
||
|
|
ProcessBuilder pb = new ProcessBuilder("crontab", "-l");
|
||
|
|
pb.redirectErrorStream(true);
|
||
|
|
Process process = pb.start();
|
||
|
|
|
||
|
|
// 设置超时(防止卡死)
|
||
|
|
if (!process.waitFor(5, TimeUnit.SECONDS)) {
|
||
|
|
process.destroy();
|
||
|
|
throw new IOException("crontab -l timeout");
|
||
|
|
}
|
||
|
|
|
||
|
|
StringBuilder crontabContent = new StringBuilder();
|
||
|
|
try (BufferedReader reader = new BufferedReader(
|
||
|
|
new InputStreamReader(process.getInputStream()))) {
|
||
|
|
String line;
|
||
|
|
while ((line = reader.readLine()) != null) {
|
||
|
|
crontabContent.append(line).append("\n");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int exitCode = process.exitValue();
|
||
|
|
if (exitCode != 0 && exitCode != 1) {
|
||
|
|
// exit code 1 表示没有 crontab 文件(正常)
|
||
|
|
throw new IOException("crontab -l failed with exit code: " + exitCode);
|
||
|
|
}
|
||
|
|
|
||
|
|
String content = crontabContent.toString();
|
||
|
|
|
||
|
|
// 2. 检查是否已存在该任务
|
||
|
|
if (content.contains(JOB_IDENTIFIER)) {
|
||
|
|
System.out.println("Crontab 任务已存在,无需添加。");
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. 如果不存在,追加任务
|
||
|
|
String newCrontab;
|
||
|
|
if (content.trim().isEmpty()) {
|
||
|
|
// 原来没有 crontab
|
||
|
|
newCrontab = CRON_JOB + "\n";
|
||
|
|
} else {
|
||
|
|
// 原来有 crontab,在末尾添加新任务
|
||
|
|
newCrontab = content;
|
||
|
|
if (!content.endsWith("\n")) {
|
||
|
|
newCrontab += "\n";
|
||
|
|
}
|
||
|
|
newCrontab += CRON_JOB + "\n";
|
||
|
|
}
|
||
|
|
|
||
|
|
// 4. 写入新的 crontab
|
||
|
|
ProcessBuilder pbWrite = new ProcessBuilder("crontab", "-");
|
||
|
|
pbWrite.redirectErrorStream(true);
|
||
|
|
Process writeProcess = pbWrite.start();
|
||
|
|
|
||
|
|
try (OutputStreamWriter writer = new OutputStreamWriter(writeProcess.getOutputStream())) {
|
||
|
|
writer.write(newCrontab);
|
||
|
|
writer.flush();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!writeProcess.waitFor(5, TimeUnit.SECONDS)) {
|
||
|
|
writeProcess.destroy();
|
||
|
|
throw new IOException("crontab - write timeout");
|
||
|
|
}
|
||
|
|
|
||
|
|
int writeExitCode = writeProcess.exitValue();
|
||
|
|
if (writeExitCode != 0) {
|
||
|
|
throw new IOException("crontab - write failed with exit code: " + writeExitCode);
|
||
|
|
}
|
||
|
|
|
||
|
|
System.out.println("Crontab 任务添加成功:\n" + CRON_JOB);
|
||
|
|
return true;
|
||
|
|
|
||
|
|
} catch (IOException | InterruptedException e) {
|
||
|
|
System.err.println("操作 crontab 失败:" + e.getMessage());
|
||
|
|
e.printStackTrace();
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// === 使用示例 ===
|
||
|
|
public static void main(String[] args) {
|
||
|
|
// 要添加的定时任务
|
||
|
|
String CRON_JOB = "*/10 * * * * /usr/local/tongran/sbin/test2.sh >/dev/null 2>&1";
|
||
|
|
// 用于判断是否已存在的关键标识(可以是脚本路径)
|
||
|
|
String JOB_IDENTIFIER = "/usr/local/tongran/sbin/test2.sh";
|
||
|
|
// boolean success = ensureCronJobExists(CRON_JOB,JOB_IDENTIFIER);
|
||
|
|
// if (success) {
|
||
|
|
// System.out.println("✅ Crontab 状态正常。");
|
||
|
|
// } else {
|
||
|
|
// System.err.println("❌ 操作失败,请检查权限或路径。");
|
||
|
|
// }
|
||
|
|
}
|
||
|
|
}
|