Skip to content

Commit

Permalink
Refactor command
Browse files Browse the repository at this point in the history
  • Loading branch information
anasfanani committed Jun 3, 2024
1 parent 15e6571 commit 7827217
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
7 changes: 1 addition & 6 deletions tailscale/scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
DIR=${0%/*}
source $DIR/../settings.ini

stop_service() {
if [ -f "${tailscaled_run_dir}/tailscaled.pid" ]; then
"${tailscaled_service}" stop >> "/dev/null" 2>&1
fi
}
start_service() {
if [ ! -f "${module_dir}/disable" ]; then
"${tailscaled_service}" start >> "/dev/null" 2>&1
Expand Down Expand Up @@ -34,4 +29,4 @@ module_version=$(busybox awk -F'=' '!/^ *#/ && /version=/ { print $2 }' "$module
log Info "Magisk Tailscaled version : ${module_version}."
start_service
# start_socks5tunnel # no longer run automatically at boot, because ndk build work, except you're using verry obsoleted android device
start_inotifyd
start_inotifyd
54 changes: 40 additions & 14 deletions tailscale/scripts/tailscaled.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,36 @@ scripts_name=$(basename $0)

start_tailscaled() {
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ ⏲ $current_time | ✔ tailscaled service is running!!! ] /g" "$module_prop"
PID=$(busybox pidof "${tailscaled_bin}")
PID=$(busybox pgrep "${tailscaled_bin}")
if [ -n "$PID" ]; then
log Info "✔ tailscaled service running with PID : ( $PID )."
PID_INFO=$(ps -p $PID -o pid,stime,cmd | grep $PID)
log Info "✔ tailscaled service already running with PID : ( $PID )."
read pid stime cmd <<< "$PID_INFO"
log Info "Start time: $stime"
return
fi
# The logs time cannot set to current timezone, default is UTC.
[ -f "${tailscaled_log}" ] && mv "${tailscaled_log}" "${tailscaled_log}.bak"; # for debugging
nohup ${tailscaled_bin} ${tailscaled_bin_param} > "${tailscaled_log}" 2>&1 &
PID=$!
echo -n $PID > "${tailscaled_pid}"
log Info "✔ tailscaled service running with PID : ( $PID )."

# Fetch process information
PID_INFO=$(ps -p $PID -o pid,stime,cmd | grep $PID)

# Check if we found the process
if [ -n "$PID_INFO" ]; then
# Read the fetched information into variables
read pid stime cmd <<< "$PID_INFO"
# Output detailed information
log Info "✔ tailscaled service running with PID : ( $PID )."
log Info "Start time: $stime"
log Info "Command: $cmd"
echo -n $PID > "${tailscaled_pid}"
else
log Error "No process with PID $PID found."
[ -f "${tailscaled_log}" ] && { log Error "Check ${tailscaled_log} for more information !"; } || { log Error "No output from command, please start manual with this command and see the response."; log Info "HOME=/data/adb/tailscale/tmp ${tailscaled_bin} ${tailscaled_bin_param}"; }
fi

}
stop_tailscaled(){
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ ⏲ $current_time | ✘ tailscaled shutting down, service is stopped !!! ] /g" "$module_prop"
Expand All @@ -24,7 +44,7 @@ stop_tailscaled(){
if busybox pkill -15 -e "${tailscaled_bin}" >/dev/null 2>&1; then
: # Do nothing if busybox pkill is successful
else
killall -15 "${tailscaled_bin}" >/dev/null 2>&1 || kill -15 "$(busybox pidof "${tailscaled_bin}")" >/dev/null 2>&1
killall -15 "${tailscaled_bin}" >/dev/null 2>&1 || kill -15 "$(busybox pgrep "${tailscaled_bin}")" >/dev/null 2>&1
fi
else
log Info "✘ tailscaled service already stop."
Expand All @@ -34,7 +54,7 @@ stop_tailscaled(){
fi
# Check if the binary has stopped
sleep 0.5
if ! busybox pidof "${tailscaled_bin}" >/dev/null 2>&1; then
if ! busybox pgrep "${tailscaled_bin}" >/dev/null 2>&1; then
# Delete the `tailscaled.pid` file if it exists
rm -f "${tailscaled_pid}"
log Info "✘ tailscaled shutting down, service is stopped."
Expand All @@ -61,12 +81,24 @@ force_stop() {
fi
fi
sleep 0.5
if ! busybox pidof "${tailscaled_bin}" >/dev/null 2>&1; then
if ! busybox pgrep "${tailscaled_bin}" >/dev/null 2>&1; then
log Info "✔ done, you can sleep peacefully."
[ -t 1 ] && echo -e "${white}--------------------------------------------${normal}"
rm -f "${tailscaled_pid}"
fi
}

status_tailscaled(){
PID=$(busybox pgrep "${tailscaled_bin}")
if [ -n "$PID" ]; then
log Info "✔ tailscaled service is running with PID : ( $PID )."
log Info "Start command: ${tailscaled_bin} ${tailscaled_bin_param}"
log Info "Logs dir: ${tailscaled_run_dir}"
return
else
log Info "✘ tailscaled service is stopped."
fi
}
log_view(){
case "$1" in
runs)
Expand Down Expand Up @@ -100,13 +132,7 @@ case "$1" in
start_tailscaled
;;
status)
# Check whether the service is running or not
PID=$(busybox pidof "${tailscaled_bin}")
if [ -n "$PID" ]; then
log Info "✔ tailscaled service is running with PID : ( $PID )."
else
log Info "✘ tailscaled service is stopped."
fi
status_tailscaled
;;
log)
log_view $2
Expand Down

0 comments on commit 7827217

Please sign in to comment.