springboot 调用shell脚本,支持传参
生活随笔
收集整理的這篇文章主要介紹了
springboot 调用shell脚本,支持传参
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import org.apache.commons.lang3.ArrayUtils;import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;/*** 執行工具類,可以執行shell腳本* @Author: liyongjie* @Description:* @Date: 2022/8/11 4:58 PM*/
@Slf4j
public class ExecUtil {/*** 執行shell腳本,腳本放在項目的resource目錄下* @param scriptName 腳本文件名,帶不帶sh后綴都可以* @param para 參數數組*/public static void execShell(String scriptName, String ... para) {try {scriptName = scriptName.endsWith(".sh") ? scriptName : scriptName + ".sh";File file = ResourceUtil.getFile("classpath:" + scriptName);if (!file.exists()) {throw new BizException("腳本文件不存在");}String scriptPath = file.getAbsolutePath();ProcessBuilder pb = new ProcessBuilder("chmod", "+x", scriptPath);Process authCmd = pb.start();authCmd.waitFor();String[] cmd = new String[]{"./" + scriptName};//為了解決參數中包含空格cmd= ArrayUtils.addAll(cmd,para);// 添加操作系統cmd = ArrayUtils.add(cmd, getOS());pb = new ProcessBuilder(cmd);pb.directory(new File(scriptPath.substring(0, scriptPath.length() - scriptName.length() - 1)));Process ps = pb.start();ps.waitFor();BufferedReader stdInput = new BufferedReader(new InputStreamReader(ps.getInputStream()));BufferedReader stdError = new BufferedReader(new InputStreamReader(ps.getErrorStream()));String s = null;while ((s = stdInput.readLine()) != null) {log.info("{} script 日志:{}", scriptName, s);System.out.println(s);}while ((s = stdError.readLine()) != null) {log.error("{} script 異常:{}", scriptName, s);System.out.println(s);}} catch (Exception e) {log.error("exec shell e", e);}}private static String getOS() {String property = System.getProperty("os.name");if (property.startsWith("Mac")) {return "Mac";} else if (property.startsWith("Linux")) {return "Linux";} else if (property.startsWith("Windows")) {return "win";}return "";}
}
#!/bin/bash
db_user=root
db_pwd=pwd
db_host=localhost
db_port=3306
db_name=learn
db_table=$1
column=$2
curr_os=$3
time_in_loop="date +%s%N"echo "host:${db_host} database:${db_name} table:${db_table} column:${column}"
MySQL="/usr/local/mysql/bin/mysql -h${db_host} -u${db_user} -p${db_pwd} -P${db_port} -A -N ${db_name}"
#sql_for_ai="show create table ${db_name}.${db_table}"
#echo ${MySQL} ${sql_for_ai}
#str_temp=$($MySQL -e "$sql_for_ai")
#echo $?
#echo $str_tempfunction run() {sql_for_ai="show create table ${db_name}.${db_table}"echo ${MySQL} ${sql_for_ai}str_temp=$(${MySQL} -e "$sql_for_ai")if [[ $? -ne 0 ]]; thenecho "Executing first sql(find the value of auto_increment) wrong! Please check your parameters"echo ""exitfiai_value=$(echo ${str_temp} | grep -oE "AUTO_INCREMENT=[0-9]+" | grep -oE "[0-9]+")ai_value_plus_2k=$(($ai_value+2000))suffix=`date -d"30 days ago" +"%Y%m%d"`st=`date -d"30 days ago" +"%Y-%m-%d 00:00:00"`if [ "${curr_os}" == "Mac" ]; thenecho "兼容Mac系統的日期"suffix=`date -v-30d +"%Y%m%d"`st=`date -v-30d +"%Y-%m-%d 00:00:00"`time_in_loop="date +%s%S"fiorigin_table=${db_table}bak_table_name="${origin_table}_bak_${suffix}"# 獲取當前主表最小id作為歸檔起始范圍get_min_id_sql="use ${db_name}; select min(id) from ${origin_table};"echo "Get MinId Sql: ${get_min_id_sql}"min_id=$(${MySQL} -e "${get_min_id_sql}")if [ "${min_id}" == "NULL" ]; thenecho "minId get ${min_id} to exit"exitfiecho "minId get ${min_id}"# 獲取主表指定字段30天之前最大id作為歸檔結束范圍get_max_id_sql="SELECT max(id) FROM ${origin_table} where ${column} < '${st}';"echo "Get MaxId Sql: ${get_max_id_sql}"max_id=$(${MySQL} -e "${get_max_id_sql}")if [ "${max_id}" == "NULL" ]; thenecho "maxId get ${max_id} to exit"exitfiecho "maxId get ${max_id}"readySql="USE ${db_name}; \CREATE TABLE IF NOT EXISTS ${bak_table_name} LIKE ${origin_table};\"${MySQL} -e "${readySql}"page_size=2000if [[ $(( (max_id-min_id)%page_size )) -eq 0 ]]; thenindex=$(( (max_id-min_id)/page_size ))elseindex=$(( (max_id-min_id)/page_size+1 ))fiecho "minId:${min_id}, maxId:${max_id}, index:${index}, page_size:${page_size}"# 分批次將范圍id的數據從主表遷移至歸檔表for (( i = 0; i < ${index}; ++i )); dosync_begin=$((min_id+i*page_size))sync_end=$((sync_begin+page_size))if [[ ${sync_end} -gt ${max_id} ]]; thensync_end=$((max_id+1))fiinsert_sql="insert into ${bak_table_name} select * from ${origin_table} where id >= ${sync_begin} and id < ${sync_end};"echo "insert_sql: ${insert_sql}"delete_sql="delete from ${origin_table} where id >= ${sync_begin} and id < ${sync_end}"echo "delete_sql: ${delete_sql}"run_sql="use ${db_name}; ${insert_sql}; ${delete_sql}"start_time_in_loop=$[$(${time_in_loop})/1000000]${MySQL} -e "${run_sql}"if [[ $? -ne 0 ]]; thenecho "Executing the above sql wrong"exitfiend_time_in_loop=$[$(${time_in_loop})/1000000]echo "round: ${i} $((end_time_in_loop - start_time_in_loop))ms"done
}if [[ "dryrun" == "$3" ]];thenecho "dryrun"
elserunif [[ "$?" == "0" ]]; thenecho "finished";else echo "errors when connect to the mysql";fi
fi
調用
public static void main(String[] args) throws IOException, InterruptedException {execShell("上文的shell文件", "表名", "create_time");}總結
以上是生活随笔為你收集整理的springboot 调用shell脚本,支持传参的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日语高考的优势
- 下一篇: 计算机应用 专业优势,计算机应用专业有何