Skip to content

Instantly share code, notes, and snippets.

@castillo-luis
Last active July 24, 2019 22:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save castillo-luis/adf461e15e00f2c6e1c88de9759f5ec9 to your computer and use it in GitHub Desktop.
Save castillo-luis/adf461e15e00f2c6e1c88de9759f5ec9 to your computer and use it in GitHub Desktop.
#!/bin/env bash
set -e
# create purge script
mkdir -p /data/local
cat <<'EOF' > /data/local/purge-data.sh
#!/bin/sh
set -e
# OP is disabled if free space is less than 15%
MAX_USED_PERCENT=70
# update $PATH to contain all the binaries we need access to when running from cron
PATH=/data/data/com.termux/files/usr/bin:/data/data/com.termux/files/usr/bin/applets:$PATH
echo -e "\t\tUsed\tMax"
# delete oldest entries until we are under the $MAX_USED_PERCENT limit
while true; do
# get the used space on the device
USED_PERCENT=$(df -h /data/media/0/realdata | tail -1 | awk '{print $5}' | sed 's/%$//')
# check if the available space is lower than the max allowed
if [[ USED_PERCENT -gt MAX_USED_PERCENT ]]; then
echo -e "Deleting\t${USED_PERCENT}%\t${MAX_USED_PERCENT}%"
# TODO: prioritize deleting driver monitoring videos first
find /data/media/0/realdata/* -type f -not -name 'rlog.bz2' -print0 2>/dev/null | \
xargs -r -0 ls -tr 2>/dev/null | \
head -1 | \
xargs rm -rf
else
echo -e "Complete\t${USED_PERCENT}%\t${MAX_USED_PERCENT}%"
break
fi
done
# delete empty directories
# find doesnt have -empty param so we have to use shell
find /data/media/0/realdata/* -type d -exec bash -c \
'shopt -s nullglob; shopt -s dotglob; \
a=("$1"/*); [[ ${a[@]} ]] || printf "%s\n" "$1"' sh {} \; 2>/dev/null | \
xargs -r rmdir
# shutdown if done uploading or wifi down
BATTERY_STATUS=`cat /sys/class/power_supply/battery/status`
NETWORK_STATUS=`cat /sys/class/net/wlan0/operstate`
FILE_COUNT=$(ls /data/media/0/realdata | wc -c)
if [ “$BATTERY_STATUS” = “Discharging” ] && [ “$FILE_COUNT” = “0” ]; then
reboot -p
elif [ “$BATTERY_STATUS” = “Discharging” ] && [ “$NETWORK_STATUS” != “up” ]; then
reboot -p
fi
EOF
chmod 0755 /data/local/purge-data.sh
# create cron job to run every 10 minutes.
mkdir -p /data/local/crontab
echo '*/10 * * * * /data/local/purge-data.sh' > /data/local/crontab/root
# install crond into userinit
cat <<EOF > /data/local/userinit.sh
#!/bin/env sh
/data/data/com.termux/files/usr/bin/applets/crond -b -c /data/local/crontab
EOF
chmod 0755 /data/local/userinit.sh
# cleanup previous versions
if [[ -f /system/etc/init.d/crond ]]; then
mount -o rw,remount /dev/block/bootdevice/by-name/system /system
fi
rm -rf \
/data/cleardata \
/data/crontab \
/system/etc/init.d/crond
if grep '\srw[\s,]' /proc/mounts | grep -q '\s/system\s'; then
mount -o ro,remount /dev/block/bootdevice/by-name/system /system
fi
echo 'Install complete! Please reboot to start crond.'
@stevenan93
Copy link

hi Luis! I notice this cron job doesn't work anymore on openpilot v0.6, any idea why? I've been using it for a long long time but updated today and now it doesnt seem to workafter updating. I also tried reinstalling this script in case the update overwrote it.

@castillo-luis
Copy link
Author

castillo-luis commented Jul 24, 2019 via email

@stevenan93
Copy link

I haven't looked into this further. You are correct with the new NEOS that V0.6 uses you can no longer run cron jobs. I have not tracked down why though. The script itself still functions but it does not seem to run as a cron job anymore. In Discord a guy from comma.ai just said that its not needed anymore since openpilot automatically cycles and overwrites old videos when it gets full and now has better power management.

On Wed, Jul 24, 2019 at 5:03 PM Steven An @.***> wrote: install via curl curl -sL https://gist.githubusercontent.com/castillo-luis/adf461e15e00f2c6e1c88de9759f5ec9/raw/453e1c401810ff61dcc55e91944546288008d4a3/install_eon_purge_data_shutdown.sh | bash — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://gist.github.com/adf461e15e00f2c6e1c88de9759f5ec9?email_source=notifications&email_token=AK6ZSODE3MSG2GTNZ3UEFO3QBDGTJA5CNFSM4IGNDE22YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFV4GU#gistcomment-2979946, or mute the thread https://github.com/notifications/unsubscribe-auth/AK6ZSOD4ZSPW2PWULYSD773QBDGTJANCNFSM4IGNDE2Q .

Hm I see....I agree that basically deleting old video files arent necessary anymore, but I really liked the shutdown script, despite power management being improved, I don't see how it could surpass turning the device off as soon as power to the car and wifi are disconnected. Can you think of any other way to facilitate an auto shutdown feature without using cron jobs? I've been looking myself but can't seem to figure anything out :/ I really liked your script haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment