optimize bfm service script

This commit is contained in:
llyc
2023-02-12 10:23:56 +07:00
parent 3362794301
commit 63f174c4b9
3 changed files with 54 additions and 35 deletions

View File

@@ -26,7 +26,7 @@ x="xray"
v="v2fly"
bin_list=("${c}" "${x}" "${s}" "${v}")
# select client
bin_name=${c}
bin_name=$c
# true: enable / false: disable Ipv6
ipv6="false"
@@ -50,7 +50,7 @@ ap_list=("softap+" "wlan+" "swlan+" "ap+" "rndis+")
ignore_out_list=()
# set interval update, info: https://crontab.guru/
crontab_sec="true"
crontab_sec="false"
# "0 00 * * *" / every 12 p.m
update_interval="0 00 * * *"

View File

@@ -141,7 +141,7 @@ run_box() {
# && (sed -i 's/"auto_detect_interface":.*/"auto_detect_interface": true/' ${data_dir}/sing-box/config.json) || (sed -i 's/"auto_detect_interface":.*/"auto_detect_interface": false/' ${data_dir}/sing-box/config.json)
sed -i 's/network_mode=.*/network_mode="tproxy"/' ${settings}
if (${bin_path} check -D ${data_dir}/${bin_name} > "${run_path}/${bin_name}-report.log" 2>&1) ; then
nohup setuidgid 0:3005 ${bin_path} run -D ${data_dir}/${bin_name} > /dev/null 2> "${run_path}/${bin_name}.log" &
nohup setuidgid 0:3005 ${bin_path} run -D ${data_dir}/${bin_name} > "${run_path}/${bin_name}.log" 2>&1 &
echo -n $! > ${pid_file}
else
log error "configuration failed, please check the ${run_path}/${bin_name}-report.log file."
@@ -181,7 +181,7 @@ run_box() {
export XRAY_LOCATION_ASSET=${data_dir}/${bin_name}
export XRAY_LOCATION_CONFDIR=${data_dir}/${bin_name}
if (${bin_path} -test > "${run_path}/${bin_name}-report.log" 2>&1) ; then
nohup setuidgid 0:3005 ${bin_path} > /dev/null 2> "${run_path}/${bin_name}.log" &
nohup setuidgid 0:3005 ${bin_path} > "${run_path}/${bin_name}.log" 2>&1 &
echo -n $! > ${pid_file}
else
log error "configuration failed, please check the ${run_path}/${bin_name}-report.log file."
@@ -197,7 +197,7 @@ run_box() {
export V2RAY_LOCATION_ASSET=${data_dir}/${bin_name}
export V2RAY_LOCATION_CONFDIR=${data_dir}/${bin_name}
if (${bin_path} test > "${run_path}/${bin_name}-report.log" 2>&1) ; then
nohup setuidgid 0:3005 ${bin_path} run > /dev/null 2> "${run_path}/${bin_name}.log" &
nohup setuidgid 0:3005 ${bin_path} run > "${run_path}/${bin_name}.log" 2>&1 &
echo -n $! > ${pid_file}
else
log error "configuration failed, please check the ${run_path}/${bin_name}-report.log file."
@@ -225,9 +225,8 @@ bin_usage() {
if bin_pid=$(pidof ${bin_name}) ; then
log info "${bin_name} has started with the $(stat -c %U:%G /proc/${bin_pid}) user group."
log info "${bin_name} status: ${state},PID: (${bin_pid})"
# log info "${bin_name} service is running. PID: (${bin_pid})"
log info "${bin_name} memory usage: ${bin_rss},swap: ${bin_swap}"
log info "${bin_name} status: ${state}, PID: (${bin_pid})"
log info "${bin_name} memory usage: ${bin_rss}, swap: ${bin_swap}"
log info "${bin_name} cpu usage: $((/system/bin/ps -p $(pidof ${bin_name}) -o pcpu | grep -v %CPU | awk '{print $1}') 2> /dev/null)%"
log info "${bin_name} running time: $(/system/bin/ps -p $(pidof ${bin_name}) -o comm,etime | grep ${bin_name} | awk '{print $2}')"
echo -n ${bin_pid} > ${pid_file}
@@ -264,8 +263,11 @@ network_port () {
start_box() {
echo -n "" > ${logs_file}
echo $(date) >> ${logs_file}
echo "--------------------------------------------" >> ${logs_file}
[ -t 1 ] && echo "\033[1;31m$(date)\033[0m" || echo "$(date)" | tee -a ${logs_file} >> /dev/null 2>&1
line="--------------------------------------------"
[ -t 1 ] && echo "\033[1;32m$line\033[0m" || echo "$line" | tee -a ${logs_file} >> /dev/null 2>&1
if bin_pid=$(pidof ${bin_name}) ; then
log info "${bin_name} service is still running, refresh iptables"
${scripts_dir}/box.iptables renew
@@ -277,9 +279,7 @@ start_box() {
create_tun
run_box
sleep 1
if [ "${crontab_sec}" != "false" ] || log info "crontab: disable." ; then
crontab_alive
fi
[ "${crontab_sec}" != "false" ] && crontab_alive || log info "crontab: disable."
cgroup_limit
detected_port || sleep 1
network_port

View File

@@ -74,8 +74,11 @@ update_file() {
echo ${request}
${request} 2>&1
sleep 0.5
[ -f "${file}" ] \
&& return 0 || [ -f "${file_bak}" ] && mv ${file_bak} ${file}
if [ -f "${file}" ] ; then
return 0
else
[ -f "${file_bak}" ] && mv ${file_bak} ${file}
fi
}
update_subgeo() {
@@ -108,7 +111,7 @@ update_subgeo() {
esac
if [ "${auto_updategeox}" = "true" ] ; then
if log debug "download ${geoip_file}" && update_file ${geoip_file} ${geoip_url} && log debug "download ${geosite_file}" && update_file ${geosite_file} ${geosite_url} ; then
if log debug "download ${geoip_url}" && update_file ${geoip_file} ${geoip_url} && log debug "download ${geosite_url}" && update_file ${geosite_file} ${geosite_url} ; then
log debug "Update geo $(date +"%Y-%m-%d %I.%M %p")"
flag=false
fi
@@ -226,36 +229,52 @@ update_kernel() {
case "${bin_name}" in
clash)
[ -f /system/bin/gunzip ] \
&& extra="/system/bin/gunzip" || extra="${busybox_path} gunzip"
if (${extra} --help > /dev/null 2>&1) ; then
${extra} "${data_dir}/${file_kernel}.gz"
&& local extra="/system/bin/gunzip" || local extra="${busybox_path} gunzip"
if (${extra} "${data_dir}/${file_kernel}.gz" >&2) ; then
mv -f "${data_dir}/${file_kernel}" "${bin_kernel}/${bin_name}" \
&& flag="true" || log error "failed to move the kernel"
[ -f "${pid_file}" ] && [ "${flag}" = "true" ] \
&& restart_box || log debug "${bin_name} does not restart"
else
log warn "failed extra file ${data_dir}/${file_kernel}.gz"
fi
;;
sing-box)
tar -xf "${data_dir}/${file_kernel}.tar.gz" -C ${data_dir}/bin
mv "${data_dir}/bin/sing-box-${sing_box_version}-${platform}-${arch}/sing-box" "${bin_kernel}/${bin_name}"
rm -r "${data_dir}/bin/sing-box-${sing_box_version}-${platform}-${arch}" \
&& flag="true" || log error "failed to move the kernel"
[ -f "${pid_file}" ] && [ "${flag}" = "true" ] \
&& restart_box || log debug "${bin_name} does not restart"
[ -f /system/bin/tar ] \
&& local extra="/system/bin/tar" || local extra="${busybox_path} tar"
if (${extra} -xf "${data_dir}/${file_kernel}.tar.gz" -C ${data_dir}/bin >&2) ; then
mv "${data_dir}/bin/sing-box-${sing_box_version}-${platform}-${arch}/sing-box" "${bin_kernel}/${bin_name}"
rm -r "${data_dir}/bin/sing-box-${sing_box_version}-${platform}-${arch}" \
&& flag="true" || log error "failed to move the kernel"
[ -f "${pid_file}" ] && [ "${flag}" = "true" ] \
&& restart_box || log debug "${bin_name} does not restart"
else
log warn "failed extra file ${data_dir}/${file_kernel}.gz"
fi
;;
v2fly)
(unzip -o "${data_dir}/${file_kernel}.zip" "v2ray" -d ${bin_kernel} >&2) \
&& mv "${bin_kernel}/v2ray" "${bin_kernel}/v2fly" \
&& flag="true" || log error "failed to move the kernel"
[ -f "${pid_file}" ] && [ "${flag}" = "true" ] \
&& restart_box || log debug "${bin_name} does not restart"
[ -f /system/bin/unzip ] \
&& local extra="/system/bin/unzip" || local extra="${busybox_path} unzip"
if (${extra} -o "${data_dir}/${file_kernel}.zip" "v2ray" -d ${bin_kernel} >&2) ; then
mv "${bin_kernel}/v2ray" "${bin_kernel}/v2fly" \
&& flag="true" || log error "failed to move the kernel"
[ -f "${pid_file}" ] && [ "${flag}" = "true" ] \
&& restart_box || log debug "${bin_name} does not restart"
else
log warn "failed extra file ${data_dir}/${file_kernel}.gz"
fi
;;
xray)
(unzip -o "${data_dir}/${file_kernel}.zip" "xray" -d ${bin_kernel} >&2) \
&& mv "${bin_kernel}/xray" "${bin_kernel}/xray" \
&& flag="true" || log error "failed to move the kernel"
[ -f "${pid_file}" ] && [ "${flag}" = "true" ] \
&& restart_box || log debug "${bin_name} does not restart"
[ -f /system/bin/unzip ] \
&& local extra="/system/bin/unzip" || local extra="${busybox_path} unzip"
if (${extra} -o "${data_dir}/${file_kernel}.zip" "xray" -d ${bin_kernel} >&2) ; then
mv "${bin_kernel}/xray" "${bin_kernel}/xray" \
&& flag="true" || log error "failed to move the kernel"
[ -f "${pid_file}" ] && [ "${flag}" = "true" ] \
&& restart_box || log debug "${bin_name} does not restart"
else
log warn "failed extra file ${data_dir}/${file_kernel}.gz"
fi
;;
*)
log error "kernel error." && exit 1