linuxtool/Config/Task/Installs/_action.sh
2025-05-19 22:41:57 +08:00

66 lines
1.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 接收参数
local_path=$1
selected_script=$2
# 脚本路径配置
read -p "请输入脚本存放路径(默认:/var/script" script_path
if [[ -z $script_path ]];then
script_path='/var/script'
fi
mkdir -p "$script_path"
# 定时任务配置说明
echo "执行日期"
echo "星号(*):表示匹配任意值"
echo "逗号(,):用于分隔多个值"
echo "斜线(/):用于指定间隔值"
echo "连字符(-):用于指定范围"
# 配置cron表达式
declare cron_expression
declare -a cron_fields=("分钟 059" "小时 023" "天数 131" "月份 112" "星期 07" )
for field in "${cron_fields[@]}";do
read -p "${field},默认为 * " tmp_time
if [[ $tmp_time =~ ^[0-9]+$ || $tmp_time == '*' ]];then
cron_expression+="${tmp_time} "
elif [[ -z ${tmp_time} ]];then
cron_expression+='* '
else
echo "输入错误"
exit
fi
done
# 验证cron表达式
if [[ "$cron_expression" == '* * * * * ' ]];then
read -p "该脚本会无时无刻执行,请重新输入"
exit
fi
# 检查脚本是否存在
if [[ -f "${script_path}/${selected_script}.sh" ]];then
echo "该路径文件已经存在"
read -p "是否覆盖?(y/n): " overwrite
if [[ ! $overwrite =~ [Yy] ]]; then
echo "已取消操作"
exit
fi
fi
# 执行原始脚本
bash "${local_path}/${selected_script}.sh" "$script_path"
# 设置脚本权限
chmod +x "${script_path}/${selected_script}.sh" && echo "脚本执行权限添加成功" || echo "脚本执行权限添加失败"
# 配置定时任务
declare cron_job="${cron_expression} ${script_path}/${selected_script}.sh"
(crontab -l 2>/dev/null | grep -v "${selected_script}.sh") | crontab -
(crontab -l 2>/dev/null; echo "$cron_job") | crontab -
# 重启cron服务
systemctl restart cron 2>> /dev/null && echo "自动任务配置成功" || echo "自动任务重启失败"
echo "配置完成"