Last active
January 22, 2019 22:26
-
-
Save SippieCup/b5a4d8f7b11f6d2af93a9ddfcaebbf6e to your computer and use it in GitHub Desktop.
Installer for script & cron job that automatically deletes older drives when free space goes under 10GB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Create deletion script | |
if [ ! -d /data/cleardata ] | |
then | |
echo "Creating /data/cleardata folder" | |
mkdir /data/cleardata | |
else | |
echo "/data/cleardata folder already exists..." | |
fi | |
if [ ! -f /data/cleardata/cleardata.sh ] | |
then | |
echo "Creating /data/cleardata/cleardata.sh script" | |
else | |
echo "Updating /data/cleardata/cleardata.sh script" | |
fi | |
echo "#!/bin/sh | |
# get the available space left on the device | |
size=\$(df -k /storage/emulated/0/Android/data | tail -1 | awk '{print \$4}') | |
# check if the available space is smaller than 5GB (5000000kB) | |
if ((\$size<5000000)); then | |
# find all files currently saved by openpilot in the past 7 hours and delete them. | |
find /data/media/0/realdata/* -mmin +240 -exec rm -rf {} \; | |
fi" > /data/cleardata/cleardata.sh | |
chmod +x /data/cleardata/cleardata.sh | |
# Create job file to run every 10 minutes. | |
if [ ! -d /data/crontab ] | |
then | |
echo "Creating /data/crontab" | |
mkdir /data/crontab | |
else | |
echo "/data/crontab folder already exists..." | |
fi | |
if [ ! -f /data/crontab/root ] | |
then | |
echo "Creating /data/crontab/root cron job" | |
else | |
echo "Updating existing /data/crontab/root cron job" | |
fi | |
echo " | |
*/10 * * * * /data/cleardata/cleardata.sh" > /data/crontab/root | |
#start job | |
cron_args="crond -b -c /data/crontab" | |
if [ `ps -o args | grep "crond -b -c /data/crontab" | head -1` == "crond -b -c /data/crontab" ] | |
then | |
echo "cronjob already running" | |
else | |
echo "Starting cron job" | |
crond -b -c /data/crontab | |
fi | |
if [ ! -f /system/etc/init.d/crond ] | |
then | |
echo "Persisting cron job after reboot" | |
# Remount /system to be read/write | |
mount -o rw,remount /dev/block/bootdevice/by-name/system /system | |
# Use Busybox crond | |
echo " | |
crond -b -c /data/crontab" >> /system/etc/init.d/crond | |
chmod +x /system/etc/init.d/crond | |
# Remount /system to be read only | |
mount -o ro,remount /dev/block/bootdevice/by-name/system /system | |
elif [ `grep -r "crond -b -c /data/crontab" /system/etc/init.d/crond` == "crond -b -c /data/crontab" ] | |
then | |
echo "Busybox crond already configured" | |
else | |
### For persistance after reboot | |
echo "Persisting cron job after reboot" | |
# Remount /system to be read/write | |
mount -o rw,remount /dev/block/bootdevice/by-name/system /system | |
# Use Busybox crond | |
echo " | |
crond -b -c /data/crontab" >> /system/etc/init.d/crond | |
chmod +x /system/etc/init.d/crond | |
# Remount /system to be read only | |
mount -o ro,remount /dev/block/bootdevice/by-name/system /system | |
fi | |
echo "Done!" |
note that the comment and code don't agree here
# find all files currently saved by openpilot in the past 7 hours and delete them.
find /data/media/0/realdata/* -mmin +240 -exec rm -rf {} \;
the find command actually lists all files older than 240 minutes, or 4 hours
so it's actually deleting files older than 4 hours, not files newer than 7 hours
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for better file handling from @tentious
Thanks!