TWRP 2.8.3.0 Available for Supported Devices

The TeamWin Recovery Project (or TWRP, for short) has quickly become one of the most popular … more

Easily Generate and Export Material Design Palletes

For many people, it can be pretty difficult to create a palette of complementary colors. … more

Android One Expanding to Bangladesh, Nepal, and Sri Lanka

At Google I/O 2013, Google proudly announced that the Android operating system was … more

Android 5.0.2 Fixes 2012 Nexus 7! Sony’s Making a Google Glass Competitor? – XDA TV

Android 5.0.2 Lollipop is available for the … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

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

OP mcbyte_it

6th January 2013, 01:04 AM   |  #1  
mcbyte_it's Avatar
OP Senior Member
Thanks Meter: 281
 
782 posts
Join Date:Joined: Nov 2008
More
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;
Last edited by mcbyte_it; 6th January 2013 at 01:09 AM.
The Following 16 Users Say Thank You to mcbyte_it For This Useful Post: [ View ]
6th January 2013, 01:27 PM   |  #2  
HTCDreamOn's Avatar
Recognized Contributor
Thanks Meter: 961
 
860 posts
Join Date:Joined: Jun 2012
Donate to Me
More
Red face
This looks like a great idea is it possible to easily change the intended interval at which yo want the scripts to run though?
6th January 2013, 01:40 PM   |  #3  
mcbyte_it's Avatar
OP Senior Member
Thanks Meter: 281
 
782 posts
Join Date:Joined: Nov 2008
More
Quote:
Originally Posted by HTCDreamOn

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".
The Following User Says Thank You to mcbyte_it For This Useful Post: [ View ]
6th January 2013, 01:53 PM   |  #4  
HTCDreamOn's Avatar
Recognized Contributor
Thanks Meter: 961
 
860 posts
Join Date:Joined: Jun 2012
Donate to Me
More
I'll take a look :)
Quote:
Originally Posted by mcbyte_it

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!
23rd February 2013, 02:48 PM   |  #5  
UnitedOceanic's Avatar
Senior Member
Thanks Meter: 109
 
332 posts
Join Date:Joined: Nov 2010
More
how can i modify this script to zimpalign /system/app too?
23rd February 2013, 04:33 PM   |  #6  
mcbyte_it's Avatar
OP Senior Member
Thanks Meter: 281
 
782 posts
Join Date:Joined: Nov 2008
More
Quote:
Originally Posted by UnitedOceanic

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....
The Following User Says Thank You to mcbyte_it For This Useful Post: [ View ]
23rd February 2013, 05:19 PM   |  #7  
UnitedOceanic's Avatar
Senior Member
Thanks Meter: 109
 
332 posts
Join Date:Joined: Nov 2010
More
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.
1st April 2014, 10:08 AM   |  #8  
Member
Flag Zamboanga City
Thanks Meter: 338
 
97 posts
Join Date:Joined: Feb 2012
Donate to Me
More
Quote:
Originally Posted by mcbyte_it

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.

Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes