diff --git a/script_tools/gsectl/agent/linux/gsectl b/script_tools/gsectl/agent/linux/gsectl index 98b9033a4..1a1ace785 100755 --- a/script_tools/gsectl/agent/linux/gsectl +++ b/script_tools/gsectl/agent/linux/gsectl @@ -502,31 +502,42 @@ get_os_type () { fi } -add_startup_to_boot (){ +check_rc_file () { + get_os_type + if [ -f $RC_LOCAL_FILE ]; then + return 0 + elif [ -f "/etc/rc.d/rc.local" ]; then + RC_LOCAL_FILE="/etc/rc.d/rc.local" + elif [ -f "/etc/init.d/rc.local" ]; then + RC_LOCAL_FILE="/etc/init.d/rc.local" + elif [ -f "/etc/init.d/boot.local" ]; then + RC_LOCAL_FILE="/etc/init.d/boot.local" + else + RC_LOCAL_FILE="/etc/rc.local" + fi +} + +add_startup_to_boot () { + local module=agent # 添加启动项到 rc.local - echo "Check startup items, and if not existing, add the startup item to rc.local" - get_os_type - if [ -f $RC_LOCAL_FILE ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |egrep ${module} |wc -l` -eq 0 ];then - echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE - fi - else - if [ -f /etc/rc.d/rc.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then - echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE - fi - elif [ -f /etc/init.d/rc.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then - echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE - fi - elif [ -f /etc/init.d/boot.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |egrep ${module} |wc -l` -eq 0 ];then - echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE - fi - fi + echo "Check startup items, and if not existing, add the [${module}] startup item to rc.local" + + check_rc_file + local rcfile=$RC_LOCAL_FILE + + if [ $OS_TYPE == "ubuntu" ]; then + sed -i "\|\#\!/bin/bash|d" $rcfile + sed -i "1i \#\!/bin/bash" $rcfile fi + + chmod +x $rcfile + + # 先删后加,避免重复 + sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $rcfile + + echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile } add_config_to_systemd () { @@ -541,11 +552,11 @@ After=network-online.target [Service] LimitNOFILE=512000 LimitCORE=infinity -WorkingDirectory=/usr/local/${INSTALL_ENV}/${node_type}/bin -PIDFile=/usr/local/${INSTALL_ENV}/${node_type}/bin/run/${module}.pid -ExecStart=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_agent -f /usr/local/${INSTALL_ENV}/${node_type}/etc/gse_agent.conf -ExecReload=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_agent --reload -ExecStop=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_agent --quit +WorkingDirectory=${WORK_HOME}/bin +PIDFile=${WORK_HOME}/bin/run/${module}.pid +ExecStart=${WORK_HOME}/bin/gse_agent -f /usr/local/${INSTALL_ENV}/${node_type}/etc/gse_agent.conf +ExecReload=${WORK_HOME}/bin/gse_agent --reload +ExecStop=${WORK_HOME}/bin/gse_agent --quit Type=forking KillMode=process User=root @@ -573,26 +584,8 @@ EOF fi # 删除rc.local里的启动项 - get_os_type - if [ -f $RC_LOCAL_FILE ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |wc -l` -ne 0 ];then - sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" $RC_LOCAL_FILE - fi - else - if [ -f /etc/rc.d/rc.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |wc -l` -ne 0 ];then - sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/rc.d/rc.local - fi - elif [ -f /etc/init.d/rc.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |wc -l` -ne 0 ];then - sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/rc.local - fi - elif [ -f /etc/init.d/boot.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |wc -l` -ne 0 ];then - sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/boot.local - fi - fi - fi + check_rc_file + sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $RC_LOCAL_FILE # 删除crontab里的watch条目 remove_crontab diff --git a/script_tools/gsectl/proxy/linux/gsectl b/script_tools/gsectl/proxy/linux/gsectl index ebe00ae21..32cf7bf58 100755 --- a/script_tools/gsectl/proxy/linux/gsectl +++ b/script_tools/gsectl/proxy/linux/gsectl @@ -828,31 +828,43 @@ get_os_type () { fi } -add_startup_to_boot (){ +check_rc_file () { + get_os_type + if [ -f $RC_LOCAL_FILE ]; then + return 0 + elif [ -f "/etc/rc.d/rc.local" ]; then + RC_LOCAL_FILE="/etc/rc.d/rc.local" + elif [ -f "/etc/init.d/rc.local" ]; then + RC_LOCAL_FILE="/etc/init.d/rc.local" + elif [ -f "/etc/init.d/boot.local" ]; then + RC_LOCAL_FILE="/etc/init.d/boot.local" + else + RC_LOCAL_FILE="/etc/rc.local" + fi +} + + +add_startup_to_boot () { + local module=$1 # 添加启动项到 rc.local - echo "Check startup items, and if not existing, add the startup item to rc.local" - get_os_type - if [ -f $RC_LOCAL_FILE ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |egrep ${module} |wc -l` -eq 0 ];then - echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE - fi - else - if [ -f /etc/rc.d/rc.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then - echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE - fi - elif [ -f /etc/init.d/rc.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |egrep ${module} |wc -l` -eq 0 ];then - echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE - fi - elif [ -f /etc/init.d/boot.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |egrep ${module} |wc -l` -eq 0 ];then - echo "[ -f /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl ] && /usr/local/${INSTALL_ENV}/${node_type}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >> $RC_LOCAL_FILE - fi - fi + echo "Check startup items, and if not existing, add the [${module}] startup item to rc.local" + + check_rc_file + local rcfile=$RC_LOCAL_FILE + + if [ $OS_TYPE == "ubuntu" ]; then + sed -i "\|\#\!/bin/bash|d" $rcfile + sed -i "1i \#\!/bin/bash" $rcfile fi + + chmod +x $rcfile + + # 先删后加,避免重复 + sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $rcfile + + echo "[ -f ${WORK_HOME}/bin/gsectl ] && ${WORK_HOME}/bin/gsectl start ${module} 1>>/var/log/${INSTALL_ENV}_${node_type}.log 2>&1" >>$rcfile } add_config_to_systemd () { @@ -875,11 +887,11 @@ After=network-online.target [Service] LimitNOFILE=512000 LimitCORE=infinity -WorkingDirectory=/usr/local/${INSTALL_ENV}/${node_type}/bin -PIDFile=/usr/local/${INSTALL_ENV}/${node_type}/bin/run/${module}.pid -ExecStart=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_${module} -f /usr/local/${INSTALL_ENV}/${node_type}/etc/gse_${config}.conf -ExecReload=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_${module} --reload -ExecStop=/usr/local/${INSTALL_ENV}/${node_type}/bin/gse_${module} --quit +WorkingDirectory=${WORK_HOME}/bin +PIDFile=${WORK_HOME}/bin/run/${module}.pid +ExecStart=${WORK_HOME}/bin/gse_${module} -f ${WORK_HOME}/etc/gse_${config}.conf +ExecReload=${WORK_HOME}/bin/gse_${module} --reload +ExecStop=${WORK_HOME}/bin/gse_${module} --quit Type=forking KillMode=process User=root @@ -909,26 +921,8 @@ EOF [ -f /tmp/${INSTALL_ENV}_${node_type}_${module}.service ] && rm /tmp/${INSTALL_ENV}_${node_type}_${module}.service # 删除rc.local里的启动项 - get_os_type - if [ -f $RC_LOCAL_FILE ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" $RC_LOCAL_FILE |wc -l` -ne 0 ];then - sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" $RC_LOCAL_FILE - fi - else - if [ -f /etc/rc.d/rc.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/rc.d/rc.local |wc -l` -ne 0 ];then - sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/rc.d/rc.local - fi - elif [ -f /etc/init.d/rc.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/rc.local |wc -l` -ne 0 ];then - sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/rc.local - fi - elif [ -f /etc/init.d/boot.local ];then - if [ `egrep "local/${INSTALL_ENV}/${node_type}" /etc/init.d/boot.local |wc -l` -ne 0 ];then - sed -i "/local\/${INSTALL_ENV}\/${node_type}/d" /etc/init.d/boot.local - fi - fi - fi + check_rc_file + sed -i "\|${WORK_HOME}/bin/gsectl start ${module}|d" $RC_LOCAL_FILE # 删除crontab里的定时任务 remove_crontab diff --git a/script_tools/setup_agent.sh b/script_tools/setup_agent.sh index 9fa4c382e..357f936aa 100644 --- a/script_tools/setup_agent.sh +++ b/script_tools/setup_agent.sh @@ -395,7 +395,8 @@ remove_crontab () { local tmpcron tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX) - crontab -l | grep -v "bin/gsectl" >"$tmpcron" + # 仅删除关联到安装目录的 crontab,避免多 Agent 互相影响 + crontab -l | grep -v "${AGENT_SETUP_PATH}" >"$tmpcron" crontab "$tmpcron" && rm -f "$tmpcron" # 下面这段代码是为了确保修改的crontab能立即生效 diff --git a/script_tools/setup_agent.zsh b/script_tools/setup_agent.zsh index 1d87e966c..25e65f8b5 100644 --- a/script_tools/setup_agent.zsh +++ b/script_tools/setup_agent.zsh @@ -373,7 +373,8 @@ remove_crontab () { local tmpcron tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX) - crontab -l | grep -v "bin/gsectl" >"$tmpcron" + # 仅删除关联到安装目录的 crontab,避免多 Agent 互相影响 + crontab -l | grep -v "${AGENT_SETUP_PATH}" >"$tmpcron" crontab "$tmpcron" && rm -f "$tmpcron" # 下面这段代码是为了确保修改的crontab能立即生效 diff --git a/script_tools/setup_proxy.sh b/script_tools/setup_proxy.sh index 2bc5d63cc..ac217b5f4 100755 --- a/script_tools/setup_proxy.sh +++ b/script_tools/setup_proxy.sh @@ -352,7 +352,8 @@ remove_crontab () { local tmpcron tmpcron=$(mktemp "$TMP_DIR"/cron.XXXXXXX) - crontab -l | grep -v "bin/gsectl" >"$tmpcron" + # 仅删除关联到安装目录的 crontab,避免多 Agent 互相影响 + crontab -l | grep -v "${AGENT_SETUP_PATH}" >"$tmpcron" crontab "$tmpcron" && rm -f "$tmpcron" # 下面这段代码是为了确保修改的crontab能立即生效