Skip to content

Commit

Permalink
refactor: Modularize service management with functions
Browse files Browse the repository at this point in the history
- Added run_as_su, stop_service, and start_service for better readability and reuse.
- Improved process check with /proc/${PID}.
  • Loading branch information
dependabot-preview[bot] committed Dec 14, 2024
1 parent aab2112 commit d9cc218
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions action.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
#!/system/bin/sh

box_dir=/data/adb/box
# Definisi variabel
box_dir="/data/adb/box"
box_run="${box_dir}/run"
box_pid="${box_run}/box.pid"

if [ -f "${box_pid}" ]; then
echo "正在关闭服务…"
run_as_su() {
su -c "$1"
}

stop_service() {
echo "Service is shutting down"
su -c $box_dir/scripts/box.iptables disable && su -c $box_dir/scripts/box.service stop
run_as_su "${box_dir}/scripts/box.iptables disable"
run_as_su "${box_dir}/scripts/box.service stop"
}

start_service() {
echo "Service is starting, please wait for a moment"
run_as_su "${box_dir}/scripts/box.service start"
run_as_su "${box_dir}/scripts/box.iptables enable"
}

if [ -f "${box_pid}" ]; then
PID=$(cat "${box_pid}")
if [ -e "/proc/${PID}" ]; then
stop_service
else
start_service
fi
else
echo "正在启动服务,请耐心等待…"
echo "Service is starting,please wait for a moment"
su -c $box_dir/scripts/box.service start && su -c $box_dir/scripts/box.iptables enable
exit
fi
start_service
fi

0 comments on commit d9cc218

Please sign in to comment.