V2-202509
This commit is contained in:
@@ -0,0 +1,308 @@
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
# 脚本名称
|
||||
# SCRIPT_NAME="ktouch-fix-daemon"
|
||||
SCRIPT_NAME=$0
|
||||
service_name="ktouch_daemon.service"
|
||||
SERVICE_FILE="/etc/systemd/system/ktouch_daemon.service"
|
||||
|
||||
# 颜色定义
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# 打印函数
|
||||
print_info() {
|
||||
echo -e "${GREEN}[INFO ]${NC} $1"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[WARN ]${NC} $1"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
print_question() {
|
||||
echo -e "${BLUE}[ ? ]${NC} $1"
|
||||
}
|
||||
|
||||
# 检查并获取root权限
|
||||
check_root() {
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
print_info "需要root权限来修改系统服务"
|
||||
|
||||
|
||||
if sudo -n true 2>/dev/null; then
|
||||
print_info "权限已自动获取,重启脚本"
|
||||
# 已经有sudo权限,直接重新运行脚本
|
||||
exec sudo "$0" "$@"
|
||||
else
|
||||
# 需要输入密码
|
||||
print_info "请输入sudo密码"
|
||||
exec sudo "$0" "$@"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# 检查用户是否存在
|
||||
check_user_exists() {
|
||||
local username="$1"
|
||||
if id "$username" &>/dev/null; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 获取系统所有用户名列表
|
||||
get_all_users() {
|
||||
# 获取所有普通用户和系统用户(UID >= 1000 或 UID < 1000但具有登录shell)
|
||||
# getent passwd | cut -d: -f1 | sort
|
||||
awk -F: '$3 >= 1000 && $7 ~ /\/(bash|sh|zsh|csh|ksh)$/ {print $1}' /etc/passwd
|
||||
}
|
||||
|
||||
|
||||
|
||||
# 停止服务
|
||||
stop_service() {
|
||||
local service_name="ktouch_daemon.service"
|
||||
|
||||
print_info "停止 $service_name 服务..."
|
||||
|
||||
if systemctl is-active --quiet "$service_name"; then
|
||||
systemctl stop "$service_name"
|
||||
if [[ $? -eq 0 ]]; then
|
||||
print_info "服务已停止"
|
||||
else
|
||||
print_error "停止服务失败"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
print_info "服务未运行"
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# 禁用服务自启动
|
||||
disable_service() {
|
||||
local service_name="ktouch_daemon.service"
|
||||
|
||||
print_info "禁用服务自启动..."
|
||||
|
||||
if systemctl is-enabled --quiet "$service_name" 2>/dev/null; then
|
||||
systemctl disable "$service_name"
|
||||
if [[ $? -eq 0 ]]; then
|
||||
print_info "服务自启动已禁用"
|
||||
else
|
||||
print_error "禁用服务自启动失败"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
print_info "服务自启动未启用"
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# 修改服务文件中的用户名
|
||||
modify_service_user() {
|
||||
local service_file="$1"
|
||||
local new_user="$2"
|
||||
local backup_file="${service_file}.backup.$(date +%Y%m%d_%H%M%S)"
|
||||
|
||||
print_info "备份服务文件: $backup_file"
|
||||
cp "$service_file" "$backup_file"
|
||||
|
||||
print_info "修改服务文件中的用户名..."
|
||||
|
||||
# 检查服务文件是否包含User=行
|
||||
if grep -q "^User=" "$service_file"; then
|
||||
# 替换现有的User=行
|
||||
sed -i "s/^User=.*/User=$new_user/" "$service_file"
|
||||
else
|
||||
# 在[Service]部分添加User=行
|
||||
if grep -q "\[Service\]" "$service_file"; then
|
||||
sed -i "/\[Service\]/a User=$new_user" "$service_file"
|
||||
else
|
||||
# 如果没有[Service]部分,在文件末尾添加
|
||||
echo -e "\n[Service]\nUser=$new_user" >> "$service_file"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
print_info "服务文件修改成功"
|
||||
return 0
|
||||
else
|
||||
print_error "服务文件修改失败"
|
||||
# 恢复备份
|
||||
cp "$backup_file" "$service_file"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 重新加载systemd配置
|
||||
reload_systemd() {
|
||||
print_info "重新加载systemd配置..."
|
||||
systemctl daemon-reload
|
||||
if [[ $? -eq 0 ]]; then
|
||||
print_info "systemd配置已重新加载"
|
||||
return 0
|
||||
else
|
||||
print_error "重新加载systemd配置失败"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 启用服务自启动
|
||||
enable_service() {
|
||||
local service_name="ktouch_daemon.service"
|
||||
|
||||
print_info "启用服务自启动..."
|
||||
|
||||
systemctl enable "$service_name"
|
||||
if [[ $? -eq 0 ]]; then
|
||||
print_info "服务自启动已启用"
|
||||
return 0
|
||||
else
|
||||
print_error "启用服务自启动失败"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 启动服务
|
||||
start_service() {
|
||||
local service_name="ktouch_daemon.service"
|
||||
|
||||
print_info "启动服务..."
|
||||
|
||||
systemctl start "$service_name"
|
||||
if [[ $? -eq 0 ]]; then
|
||||
print_info "服务已启动"
|
||||
return 0
|
||||
else
|
||||
print_error "启动服务失败"
|
||||
print_info "可以使用 'systemctl status $service_name' 查看详细错误信息"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# 显示服务状态
|
||||
show_service_status() {
|
||||
local service_name="ktouch_daemon.service"
|
||||
|
||||
print_info "当前服务状态:"
|
||||
systemctl status "$service_name" --no-pager -l
|
||||
}
|
||||
|
||||
# 主函数
|
||||
main() {
|
||||
print_info "=== ktouch_daemon 服务用户修改工具 ==="
|
||||
|
||||
if [ ! -f $SERVICE_FILE ];then
|
||||
print_error "服务文件不存在,请重新安装ktouch软件!"
|
||||
exit 1
|
||||
|
||||
fi
|
||||
|
||||
# 检查root权限
|
||||
check_root "$@"
|
||||
|
||||
# 显示当前服务配置
|
||||
print_info "当前服务配置:"
|
||||
grep -E "User=" "$SERVICE_FILE" 2>/dev/null || print_warning "无法获取当前配置"
|
||||
|
||||
# 获取用户名
|
||||
while true; do
|
||||
echo
|
||||
print_question "正在修改 ktouch_daemon 监控服务的执行用户,请在下面输入需要执行的用户名"
|
||||
echo -n "用户名: "
|
||||
read -r username
|
||||
|
||||
# 检查输入是否为空
|
||||
if [[ -z "$username" ]]; then
|
||||
print_error "用户名不能为空,请重新输入"
|
||||
continue
|
||||
fi
|
||||
|
||||
# 检查用户是否存在
|
||||
if check_user_exists "$username"; then
|
||||
# print_info "用户 '$username' 存在"
|
||||
break
|
||||
else
|
||||
print_error "用户 '$username' 不存在,请重新输入"
|
||||
print_info "可用的用户列表:"
|
||||
get_all_users | head -10 | tr '\n' ' '
|
||||
echo -e "\n(显示前10个用户,完整列表请查看 /etc/passwd)"
|
||||
fi
|
||||
done
|
||||
|
||||
# 确认操作
|
||||
echo
|
||||
print_info "即将执行以下操作:"
|
||||
print_info "1. 停止 ktouch_daemon.service 服务"
|
||||
print_info "2. 禁用服务自启动"
|
||||
print_info "3. 修改服务文件中的用户为: $username"
|
||||
print_info "4. 重新启用服务自启动"
|
||||
print_info "5. 启动服务"
|
||||
echo
|
||||
echo -n " 是否继续? (y/N): "
|
||||
read -r confirm
|
||||
|
||||
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
|
||||
print_info "操作已取消"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 执行操作
|
||||
echo
|
||||
print_info "开始修改服务配置..."
|
||||
|
||||
# 停止服务
|
||||
if ! stop_service; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 禁用服务自启动
|
||||
if ! disable_service; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 修改服务文件
|
||||
if ! modify_service_user "$SERVICE_FILE" "$username"; then
|
||||
print_error "修改失败!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 重新加载systemd配置
|
||||
if ! reload_systemd; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 启用服务自启动
|
||||
if ! enable_service; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 启动服务
|
||||
if ! start_service; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 显示最终状态
|
||||
echo
|
||||
print_info "=== 操作完成 ==="
|
||||
show_service_status
|
||||
|
||||
echo
|
||||
print_info "服务用户已成功修改为: $username"
|
||||
}
|
||||
|
||||
# 错误处理
|
||||
set -euo pipefail
|
||||
|
||||
# 执行主函数
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user