Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,806,684 Members 48,058 Now Online
XDA Developers Android and Mobile Development Forum

[SCRIPT] Run init.d scripts once every N days [ZipAlign/SQLite3/others]

Tip us?
 
mcbyte_it
Old
(Last edited by mcbyte_it; 6th January 2013 at 12:09 AM.)
#1  
mcbyte_it's Avatar
Senior Member - OP
Thanks Meter 276
Posts: 777
Join Date: Nov 2008
Default [SCRIPT] Run init.d scripts once every N days [ZipAlign/SQLite3/others]

There are many ROMs that include the ZipAlign or SQLite3 vacuum on boot, but these command are not really needed to run on EVERY boot, once every few days can be enough, so I came out with this script.

To run it, you need to have busybox with the stat applet (should be there always), which gives information about a file, including last modification date.

This is done is by checking the last modification date on a file, which is touched or changed only when the main script is executed.

Currently I modified the ZipAlign and SQLite vacuum in my own rom to use this technique, and they seem to work fine. The monitored file is the log of the operation done, which is stored in the /data/ directory

ZipAlign on Boot:
Code:
#!/system/bin/sh
# Automatic ZipAlign by Wes Garner
# ZipAlign files in /data that have not been previously ZipAligned (using md5sum)
# Thanks to oknowton for the changes

# Changelog:
# 1.0 (11/30/09) Original
# 1.1 (12/01/09) Switched to zipalign -c 4 to check the apk instead of MD5 (oknowton)
# 1.2 (06/01/13) Run the main script only once every N days (default 7 days, 1 week) (mcbyte_it)

LOG_FILE=/data/zipalign.log
#Interval between ZipAlign runs, in seconds, 604800=1 week
RUN_EVERY=604800

# Get the last modify date of the Log file, if the file does not exist, set value to 0
if [ -e $LOG_FILE ]; then
	LASTRUN=`stat -t $LOG_FILE | awk '{print $14}'`
else
	LASTRUN=0
fi;

# Get current date in epoch format
CURRDATE=`date +%s`

# Check the interval
INTERVAL=$(expr $CURRDATE - $LASTRUN)

# If interval is more than the set one, then run the main script
if [ $INTERVAL -gt $RUN_EVERY ];
then
	if [ -e $LOG_FILE ]; then
		rm $LOG_FILE;
	fi;

	echo "Starting Automatic ZipAlign $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;
	for apk in /data/app/*.apk ; do
		zipalign -c 4 $apk;
		ZIPCHECK=$?;
		if [ $ZIPCHECK -eq 1 ]; then
			echo ZipAligning $(basename $apk)  | tee -a $LOG_FILE;
			zipalign -f 4 $apk /cache/$(basename $apk);

			if [ -e /cache/$(basename $apk) ]; then
				cp -f -p /cache/$(basename $apk) $apk  | tee -a $LOG_FILE;
				rm /cache/$(basename $apk);
			else
				echo ZipAligning $(basename $apk) Failed  | tee -a $LOG_FILE;
			fi;
		else
			echo ZipAlign already completed on $apk  | tee -a $LOG_FILE;
		fi;
	done;
	echo "Automatic ZipAlign finished at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;
fi
SQLite3 vacuum:
Code:
#!/system/bin/sh
# ========================================
# init.d script for McByte jkSGS3
# ========================================

# SQLite database vaccum

# Frequent inserts, updates, and deletes can cause the database file to become fragmented - where data for a single table or index is scattered around the database file.
# Running VACUUM ensures that each table and index is largely stored contiguously within the database file.
# In some cases, VACUUM may also reduce the number of partially filled pages in the database, reducing the size of the database file further.
# sqlite3 binary in /system/xbin is required!

# Changelog
#  v1.0 - (??/??/????) - original version
#  v1.1 - (06/01/2013) - run only every X seconds, default = 1 week (mcbyte_it)
#

# Log file location
LOG_FILE=/data/sqlite.log

#Interval between SQLite3 runs, in seconds, 604800=1 week
RUN_EVERY=604800

# Get the last modify date of the Log file, if the file does not exist, set value to 0
if [ -e $LOG_FILE ]; then
	LASTRUN=`stat -t $LOG_FILE | awk '{print $14}'`
else
	LASTRUN=0
fi;

# Get current date in epoch format
CURRDATE=`date +%s`

# Check the interval
INTERVAL=$(expr $CURRDATE - $LASTRUN)

# If interval is more than the set one, then run the main script
if [ $INTERVAL -gt $RUN_EVERY ];
then
	if [ -e $LOG_FILE ]; then
		rm $LOG_FILE;
	fi;
		
	echo "SQLite database VACUUM and REINDEX started at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;

	for i in `busybox find /d* -iname "*.db"`; do
		/system/xbin/sqlite3 $i 'VACUUM;';
		resVac=$?
		if [ $resVac == 0 ]; then
			resVac="SUCCESS";
		else
			resVac="ERRCODE-$resVac";
		fi;
		
		/system/xbin/sqlite3 $i 'REINDEX;';
		resIndex=$?
		if [ $resIndex == 0 ]; then
			resIndex="SUCCESS";
		else
			resIndex="ERRCODE-$resIndex";
		fi;
		echo "Database $i:  VACUUM=$resVac  REINDEX=$resIndex" | tee -a $LOG_FILE;
	done
	  
	echo "SQLite database VACUUM and REINDEX finished at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;
fi;

Device 1: OnePlus One 64GB (KK 4.4.2 | CM11s)
Device 2: Nexus 10 16GB (KK 4.4.4)
Other projects: ROM Stats v2.0 beta | weekly init.d scripts | Daily Free App @ Amazon (Europe SUPPORTED)
The Following 16 Users Say Thank You to mcbyte_it For This Useful Post: [ Click to Expand ]
 
mcbyte_it
Old
#3  
mcbyte_it's Avatar
Senior Member - OP
Thanks Meter 276
Posts: 777
Join Date: Nov 2008
Quote:
Originally Posted by HTCDreamOn View Post
This looks like a great idea is it possible to easily change the intended interval at which yo want the scripts to run though?
Sure, check the examples I posted, they are well commented. Just change the value of the "RUN_EVERY".

Device 1: OnePlus One 64GB (KK 4.4.2 | CM11s)
Device 2: Nexus 10 16GB (KK 4.4.4)
Other projects: ROM Stats v2.0 beta | weekly init.d scripts | Daily Free App @ Amazon (Europe SUPPORTED)
The Following User Says Thank You to mcbyte_it For This Useful Post: [ Click to Expand ]
 
HTCDreamOn
Old
#4  
HTCDreamOn's Avatar
Recognized Contributor
Thanks Meter 685
Posts: 821
Join Date: Jun 2012
Default I'll take a look :)

Quote:
Originally Posted by mcbyte_it View Post
Sure, check the examples I posted, they are well commented. Just change the value of the "RUN_EVERY".
Awesome, I'll give them a go!
 
UnitedOceanic
Old
#5  
UnitedOceanic's Avatar
Senior Member
Thanks Meter 109
Posts: 331
Join Date: Nov 2010
how can i modify this script to zimpalign /system/app too?
Devices:
  • Google Nexus 4: 4.4.4 Mahdi & Mart1x ➡ daily driver
  • Motorola Milestone: 2.3.7 CM7 ➡ emulator machine & WiFi Drive
  • Sony Ericsson X10 Mini: 4.1.2 PureSlim ➡ Walkman, FM-Radio & WiFi Drive
Don't have Dropbox yet? Get both of us additional 500MB cloud storage free (on top of 2GB) by clicking this referral link!
 
mcbyte_it
Old
#6  
mcbyte_it's Avatar
Senior Member - OP
Thanks Meter 276
Posts: 777
Join Date: Nov 2008
Quote:
Originally Posted by UnitedOceanic View Post
how can i modify this script to zimpalign /system/app too?
There is no much sense of zipaligning system apps every x days, once they are zipaligned (when making the ROM zip), they remain zipaligned. some apks might need zip aligning if they get moved/installed as system apps after rom install.

Not to mention the stability, I don't know the exact boot mechanism of android, if you start zip aligning system apps during the init.d, it might cause instability to some system apps.
But if you mount the /system partition as read/write, do the zipaligning loop, then remount it as read-only, it should be possible.

still, I don't recommend doing so....

Device 1: OnePlus One 64GB (KK 4.4.2 | CM11s)
Device 2: Nexus 10 16GB (KK 4.4.4)
Other projects: ROM Stats v2.0 beta | weekly init.d scripts | Daily Free App @ Amazon (Europe SUPPORTED)
The Following User Says Thank You to mcbyte_it For This Useful Post: [ Click to Expand ]
 
UnitedOceanic
Old
#7  
UnitedOceanic's Avatar
Senior Member
Thanks Meter 109
Posts: 331
Join Date: Nov 2010
i pushed many apps from /data/app to /system/app.
I deleted the libs from the apks and copied them to /system/libs to saved space. however I forgot to zipalign the apks after removing the libs. I used the zipalign script from the rom toolbox app unfortunately it didn't work as expected. it just deleted the apps that should have been zipaligned.

I don't really want to zipalign /system at every boot. just once for the modified apks.
Devices:
  • Google Nexus 4: 4.4.4 Mahdi & Mart1x ➡ daily driver
  • Motorola Milestone: 2.3.7 CM7 ➡ emulator machine & WiFi Drive
  • Sony Ericsson X10 Mini: 4.1.2 PureSlim ➡ Walkman, FM-Radio & WiFi Drive
Don't have Dropbox yet? Get both of us additional 500MB cloud storage free (on top of 2GB) by clicking this referral link!
 
remzej
Old
#8  
Member
Thanks Meter 322
Posts: 95
Join Date: Feb 2012
Location: Zamboanga City

 
DONATE TO ME
Quote:
Originally Posted by mcbyte_it View Post
There are many ROMs that include the ZipAlign or SQLite3 vacuum on boot, but these command are not really needed to run on EVERY boot, once every few days can be enough, so I came out with this script.

To run it, you need to have busybox with the stat applet (should be there always), which gives information about a file, including last modification date.

This is done is by checking the last modification date on a file, which is touched or changed only when the main script is executed.

Currently I modified the ZipAlign and SQLite vacuum in my own rom to use this technique, and they seem to work fine. The monitored file is the log of the operation done, which is stored in the /data/ directory

ZipAlign on Boot:
Code:
#!/system/bin/sh
# Automatic ZipAlign by Wes Garner
# ZipAlign files in /data that have not been previously ZipAligned (using md5sum)
# Thanks to oknowton for the changes

# Changelog:
# 1.0 (11/30/09) Original
# 1.1 (12/01/09) Switched to zipalign -c 4 to check the apk instead of MD5 (oknowton)
# 1.2 (06/01/13) Run the main script only once every N days (default 7 days, 1 week) (mcbyte_it)

LOG_FILE=/data/zipalign.log
#Interval between ZipAlign runs, in seconds, 604800=1 week
RUN_EVERY=604800

# Get the last modify date of the Log file, if the file does not exist, set value to 0
if [ -e $LOG_FILE ]; then
	LASTRUN=`stat -t $LOG_FILE | awk '{print $14}'`
else
	LASTRUN=0
fi;

# Get current date in epoch format
CURRDATE=`date +%s`

# Check the interval
INTERVAL=$(expr $CURRDATE - $LASTRUN)

# If interval is more than the set one, then run the main script
if [ $INTERVAL -gt $RUN_EVERY ];
then
	if [ -e $LOG_FILE ]; then
		rm $LOG_FILE;
	fi;

	echo "Starting Automatic ZipAlign $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;
	for apk in /data/app/*.apk ; do
		zipalign -c 4 $apk;
		ZIPCHECK=$?;
		if [ $ZIPCHECK -eq 1 ]; then
			echo ZipAligning $(basename $apk)  | tee -a $LOG_FILE;
			zipalign -f 4 $apk /cache/$(basename $apk);

			if [ -e /cache/$(basename $apk) ]; then
				cp -f -p /cache/$(basename $apk) $apk  | tee -a $LOG_FILE;
				rm /cache/$(basename $apk);
			else
				echo ZipAligning $(basename $apk) Failed  | tee -a $LOG_FILE;
			fi;
		else
			echo ZipAlign already completed on $apk  | tee -a $LOG_FILE;
		fi;
	done;
	echo "Automatic ZipAlign finished at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;
fi
SQLite3 vacuum:
Code:
#!/system/bin/sh
# ========================================
# init.d script for McByte jkSGS3
# ========================================

# SQLite database vaccum

# Frequent inserts, updates, and deletes can cause the database file to become fragmented - where data for a single table or index is scattered around the database file.
# Running VACUUM ensures that each table and index is largely stored contiguously within the database file.
# In some cases, VACUUM may also reduce the number of partially filled pages in the database, reducing the size of the database file further.
# sqlite3 binary in /system/xbin is required!

# Changelog
#  v1.0 - (??/??/????) - original version
#  v1.1 - (06/01/2013) - run only every X seconds, default = 1 week (mcbyte_it)
#

# Log file location
LOG_FILE=/data/sqlite.log

#Interval between SQLite3 runs, in seconds, 604800=1 week
RUN_EVERY=604800

# Get the last modify date of the Log file, if the file does not exist, set value to 0
if [ -e $LOG_FILE ]; then
	LASTRUN=`stat -t $LOG_FILE | awk '{print $14}'`
else
	LASTRUN=0
fi;

# Get current date in epoch format
CURRDATE=`date +%s`

# Check the interval
INTERVAL=$(expr $CURRDATE - $LASTRUN)

# If interval is more than the set one, then run the main script
if [ $INTERVAL -gt $RUN_EVERY ];
then
	if [ -e $LOG_FILE ]; then
		rm $LOG_FILE;
	fi;
		
	echo "SQLite database VACUUM and REINDEX started at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;

	for i in `busybox find /d* -iname "*.db"`; do
		/system/xbin/sqlite3 $i 'VACUUM;';
		resVac=$?
		if [ $resVac == 0 ]; then
			resVac="SUCCESS";
		else
			resVac="ERRCODE-$resVac";
		fi;
		
		/system/xbin/sqlite3 $i 'REINDEX;';
		resIndex=$?
		if [ $resIndex == 0 ]; then
			resIndex="SUCCESS";
		else
			resIndex="ERRCODE-$resIndex";
		fi;
		echo "Database $i:  VACUUM=$resVac  REINDEX=$resIndex" | tee -a $LOG_FILE;
	done
	  
	echo "SQLite database VACUUM and REINDEX finished at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;
fi;
Thanks for this awesome script i was able to get an idea for this. I tried this two scripts but only zipalign works even though i have sqlite3 in stored in xbin.

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


And the Winners in the XDA Pebble Development Challenge Are…

Almost two months ago, we set out with Pebble to findthree amazing … more

MediaTek Releases Full Kernel Source for First Android One Devices

Those who might have thought that MediaTek wouldnever release working … more

Submitting a Patch to Gerrit – Featuring XplodWild – XDA Developer TV

Not every developer specializes in every area of … more

Learn How to Make a Live Wallpaper all by Yourself

Stillimages can bebeautiful, but some of you prefer to see animations on your screens. … more