import java.io.BufferedReader; import java.io.InputStreamReader; public class ShellScriptExecutor implements Runnable { private String scriptPath; public ShellScriptExecutor(String scriptPath) { this.scriptPath = scriptPath; } @Override public void run() { try { Process process = Runtime.getRuntime().exec(scriptPath); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } process.waitFor(); } catch (Exception e) { e.printStackTrace(); } } }
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Main { @SuppressWarnings("AlibabaThreadPoolCreation") public static void main(String[] args) { // 替换为您的shell脚本路径 String scriptPath = "/path/to/your/shell/script.sh"; ShellScriptExecutor shellScriptExecutor = new ShellScriptExecutor(scriptPath); ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); // 每小时执行一次 scheduledExecutorService.scheduleAtFixedRate(shellScriptExecutor, 0, 1, TimeUnit.HOURS); } }