Skip to content

Instantly share code, notes, and snippets.

@chasebolt
Forked from tentious/install_deletion_job.sh
Last active July 8, 2019 22:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save chasebolt/fd5210b4c2a44a2b0db383162a66632c to your computer and use it in GitHub Desktop.
Save chasebolt/fd5210b4c2a44a2b0db383162a66632c to your computer and use it in GitHub Desktop.
installs EON cron job that automatically purges oldest video when used space is greater than the MAX_USED_PERCENT limit
#!/bin/env bash
set -e
# install via curl
# curl -sL https://gist.github.com/chasebolt/fd5210b4c2a44a2b0db383162a66632c/raw/install_eon_purge_data.sh | bash
# 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=80
# 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
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 -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.'
@onekrishnakumar
Copy link

how do I incorporate this into my EON?
I have never before connect to my EON from my PC
Any help or links to a step by step guide would be much appreciated !

@onekrishnakumar
Copy link

I was finally able to figure out SSH and run the following
curl -sL https://gist.github.com/chasebolt/fd5210b4c2a44a2b0db383162a66632c/raw/install_eon_purge_data.sh | bash
I got the following message
'Install complete! Please reboot to start crond.'
I did reboot my EON
And nothing seems different
Planning on driving 1,000 miles on Saturday, hope this fix works!!

@monterman
Copy link

did it work okay for you?

@doliho
Copy link

doliho commented Dec 31, 2018

Yes, this worked well for me and thanks for the scripts

@onekrishnakumar
Copy link

This worked on the previous version I had openpilot v0.5.6 release
But when I was forced to upgrade to openpilot v0.5.7 release, the above fix that was installed was overwritten, so I installed this again, but it did not work.
So, for now, I ran the following once I was SSHed into my eon
rm -rf /storage/self/primary/realdata/*
This basically removed all the drive recordings
Then I installed the above mentioned
curl -sL https://gist.github.com/chasebolt/fd5210b4c2a44a2b0db383162a66632c/raw/install_eon_purge_data.sh | bash
I am at 3% memory now, but let me see if it it works
If not, then the above code might have to be tweaked for the openpilot v0.5.7 release

@boradwell
Copy link

This worked on the previous version I had openpilot v0.5.6 release
But when I was forced to upgrade to openpilot v0.5.7 release, the above fix that was installed was overwritten, so I installed this again, but it did not work.
So, for now, I ran the following once I was SSHed into my eon
rm -rf /storage/self/primary/realdata/*
This basically removed all the drive recordings
Then I installed the above mentioned
curl -sL https://gist.github.com/chasebolt/fd5210b4c2a44a2b0db383162a66632c/raw/install_eon_purge_data.sh | bash
I am at 3% memory now, but let me see if it it works
If not, then the above code might have to be tweaked for the openpilot v0.5.7 release

Did the script work?

@onekrishnakumar
Copy link

This worked on the previous version I had openpilot v0.5.6 release
But when I was forced to upgrade to openpilot v0.5.7 release, the above fix that was installed was overwritten, so I installed this again, but it did not work.
So, for now, I ran the following once I was SSHed into my eon
rm -rf /storage/self/primary/realdata/*
This basically removed all the drive recordings
Then I installed the above mentioned
curl -sL https://gist.github.com/chasebolt/fd5210b4c2a44a2b0db383162a66632c/raw/install_eon_purge_data.sh | bash
I am at 3% memory now, but let me see if it it works
If not, then the above code might have to be tweaked for the openpilot v0.5.7 release

Did the script work?

Nope, when the memory gets full and the eon does not work anymore, I SSH into the EON and run this
rm -rf /storage/self/primary/realdata/*
This removes all the files and I can start using the Eon again
I think they designed it for folks who drive max an hour a day and come back to their garage and the eon uploads
But I live in an apartment and the wifi from my apartment to the car parked in the carport is very weak and I also drive 2-3 hours a day
So, every 3 days I wipe all the files so I can use my EON
It is a pain to do it, but that is the only way I have now
If you came across another way, please share

@smatterchoo
Copy link

I wrote my own script (worked) and then tried set it up to run regularly via cron (didn't work.. filesystems are all readonly)

I found https://gist.github.com/SippieCup/b5a4d8f7b11f6d2af93a9ddfcaebbf6e which is a different approach (instead of a while loop, it just deletes all files older than 4 hours) but adds flipping the filesystem to read/write to make the cron entries, then flips back to readonly

@onekrishnakumar
Copy link

I wrote my own script (worked) and then tried set it up to run regularly via cron (didn't work.. filesystems are all readonly)

I found https://gist.github.com/SippieCup/b5a4d8f7b11f6d2af93a9ddfcaebbf6e which is a different approach (instead of a while loop, it just deletes all files older than 4 hours) but adds flipping the filesystem to read/write to make the cron entries, then flips back to readonly

I see, so if I Install this link https://gist.github.com/SippieCup/b5a4d8f7b11f6d2af93a9ddfcaebbf6e, will it work?
Like did you try it and did it work?

@castillo-luis
Copy link

This will install but never run as is...seems like bash is not the default shell when the job is kicked off by cron. Changing the shebang line in the purge script on line 10 from #!/bin/bash to #!/bin/sh will make this work correctly....

@onekrishnakumar
Copy link

This will install but never run as is...seems like bash is not the default shell when the job is kicked off by cron. Changing the shebang line in the purge script on line 10 from #!/bin/bash to #!/bin/sh will make this work correctly....

Hi Castillo-luis

Thank you for the reply

But, if we are to change /bash to /sh in line 10, should we also change /bash to /sh in line 1, 5, 41 and also when executing the curl command once ssh'ed into EON?
In other words, should this like be changed from
curl -sL https://gist.github.com/chasebolt/fd5210b4c2a44a2b0db383162a66632c/raw/install_eon_purge_data.sh | bash
to
curl -sL https://gist.github.com/chasebolt/fd5210b4c2a44a2b0db383162a66632c/raw/install_eon_purge_data.sh | sh
?

Thank you!
Krishna.

@castillo-luis
Copy link

castillo-luis commented Jan 26, 2019

Just changing that one line (line 10) made it work for me....

Just run the curl command then go to the directory where it made the script and change it. Or clone this script to your own repo change line 10 and issue the curl command against your own repo..

@castillo-luis
Copy link

I modified this script and added code to auto shutdown eon when done uploading files and not charging....great for Bosch which does not have constant power

https://gist.github.com/castillo-luis/adf461e15e00f2c6e1c88de9759f5ec9

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