Skip to content

Commit b43b059

Browse files
author
twnesss
committed
feat(cron): improve crond handling and cleanup
- Replaced existing crond startup logic with cleaner, safer implementation. - Added `pkill` fallback mechanism for stopping `crond`. - Removed redundant `touch` and `chmod` on crontab file; now using descriptive header and appending additional config from `crontab.cfg`. - Adjusted logging messages for clarity ("crond enable"/"crond disable"). - Updated service status emojis in `description=` fields for better UX.
1 parent 1619f19 commit b43b059

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

.github/workflows/debug.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
run: |
6565
if [ ! -z "${{ secrets.BOT_TOKEN }}" ]; then
6666
export VERSION=$(cat module.prop | grep 'version=' | awk -F '=' '{print $2}')
67-
export COMMIT=$(git log --oneline -n 15 --no-decorate | sed 's/^[0-9a-f]* //' | sed 's/^/— /')
67+
export COMMIT=$(git log --oneline -n 5 --no-decorate | sed 's/^[0-9a-f]* //' | sed 's/^/— /')
6868
FILE=$(find -name "*.zip")
6969
pip3 install telethon==1.31.1
7070
python3 $GITHUB_WORKSPACE/.github/taamarinbot.py $FILE

box/crontab.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# cron tasks for root

box/scripts/box.service

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@ box_bin_alive() {
3838
}
3939

4040
box_run_crontab() {
41-
# start crond with the "-c" option and keep it in the background
42-
nohup busybox crond -c "${box_run}" > /dev/null 2>&1 &
43-
# delete the previous crontab and create a new crontab
41+
# Stop crond if it is running (optional)
42+
pkill -f "busybox crond" > /dev/null 2>&1
43+
# Prepare crontab
4444
busybox crontab -c "${box_run}" -r
45-
touch "${box_run}/root"
46-
chmod 0755 "${box_run}/root"
45+
echo "# cron tasks for root" > "${box_run}/root"
4746

4847
if [ "${run_crontab}" = "true" ]; then
49-
log Debug "crontab job enabled"
48+
log Debug "crond enable"
5049
if [ "$update_subscription" = "true" ] || [ "$update_geo" = "true" ]; then
5150
log Debug "Interval crontab: ${interva_update}."
5251
echo "${interva_update} ${scripts_dir}/box.tool geosub" >> "${box_run}/root"
@@ -55,8 +54,12 @@ box_run_crontab() {
5554
log Info "${bin_name} subscription update: ${update_subscription}."
5655
fi
5756
fi
57+
cat "${box_dir}/crontab.cfg" >> "${box_run}/root"
58+
chmod 0644 "${box_run}/root"
59+
# start crond with the "-c" option and keep it in the background
60+
nohup busybox crond -c "${box_run}" > /dev/null 2>&1 &
5861
else
59-
log Info "crontab Job disabled"
62+
log Info "crond disable"
6063
fi
6164
}
6265

@@ -594,15 +597,15 @@ box_bin_status() {
594597

595598
# Save the process ID to the pid file
596599
if [ -n "$PID" ]; then
597-
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ $current_time | 😎 $bin_name service is running!!! ] /g" "$PROPFILE"
600+
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ $current_time | 🍀 $bin_name service is running!!! ] /g" "$PROPFILE"
598601
echo -n "$PID" > "${box_pid}"
599602
fi
600603
}
601604

602605
start_box() {
603606
# Clear the log file and add the timestamp and delimiter
604607
# cd /data/adb/box/bin; chmod 755 *
605-
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ $current_time | 😵‍💫 Module is working! but no service is running ] /g" "$PROPFILE"
608+
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ $current_time | 🙂‍↔️ Module is working! but no service is running ] /g" "$PROPFILE"
606609

607610
echo -n "" > "${box_log}"
608611
box_version=$(busybox awk '!/^ *#/ && /version=/ { print $0 }' "/data/adb/modules/box_for_root/module.prop" 2>/dev/null)
@@ -757,16 +760,18 @@ stop_box() {
757760
force_stop
758761
fi
759762

760-
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ $current_time | 🥶 $bin_name shutting down, service is stopped !!! ] /g" "$PROPFILE"
763+
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ $current_time | 😱 $bin_name shutting down, service is stopped !!! ] /g" "$PROPFILE"
761764
}
762765

763766
stop_cron() {
764-
# Find cronjob PID using `pgrep`
767+
# Stop crond with pkill
768+
if ! busybox pkill -f "busybox crond" > /dev/null 2>&1; then
769+
# If pkill fails, find and kill the process
765770
cronkill=$(busybox pgrep -f "crond -c ${box_run}")
766771
for cron in ${cronkill[@]}; do
767-
# kill cronjob
768-
kill -15 "${cron}"
772+
kill -15 "${cron}" 2>/dev/null
769773
done
774+
fi
770775
}
771776

772777
force_stop() {

0 commit comments

Comments
 (0)