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

Search This thread

mcbyte_it

Senior Member
Nov 24, 2008
828
314
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)

[COLOR="DarkRed"]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[/COLOR]
[COLOR="Green"]	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;[/color]
[COLOR="DarkRed"]fi[/COLOR]

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)
#

[COLOR="DarkRed"]# 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[/COLOR]
	[COLOR="Green"]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;[/COLOR]
[COLOR="DarkRed"]fi;[/COLOR]
 
Last edited:

HTCDreamOn

Senior Member
Jun 17, 2012
1,069
1,353
This looks like a great idea :good: is it possible to easily change the intended interval at which yo want the scripts to run though?
 

mcbyte_it

Senior Member
Nov 24, 2008
828
314
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....
 
  • Like
Reactions: UnitedOceanic

UnitedOceanic

Senior Member
Nov 14, 2010
342
118
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. :)
 

remzej

Senior Member
Feb 22, 2012
140
381
Zamboanga City
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)

[COLOR="DarkRed"]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[/COLOR]
[COLOR="Green"]	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;[/color]
[COLOR="DarkRed"]fi[/COLOR]

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)
#

[COLOR="DarkRed"]# 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[/COLOR]
	[COLOR="Green"]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;[/COLOR]
[COLOR="DarkRed"]fi;[/COLOR]

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.
 

Danster840

Senior Member
Feb 14, 2011
230
105
South Florida
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)

[COLOR="DarkRed"]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[/COLOR]
[COLOR="Green"]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;[/color]
[COLOR="DarkRed"]fi[/COLOR]

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)
#

[COLOR="DarkRed"]# 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[/COLOR]
[COLOR="Green"]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;[/COLOR]
[COLOR="DarkRed"]fi;[/COLOR]
Hi guys! I recently installed lollipop touch wiz and installed the two scripts on the Op, managed to get the sql script to work fine but when the zip align tries to run it errors out in the log, any advice to what I've done wrong or if you guys know of a change in lollipop that doesn't play nice with zip aligning?
Starting Automatic ZipAlign 06-04-2014 15:23:01
ZipAligning *.apk
ZipAligning *.apk Failed
Automatic ZipAlign finished at 06-04-2014 15:23:01
All I've done as far as modify the script is I got rid of the color tags.

Also the sql log shows the correct date as well, where this script does not.

Thank you in advance guys!
 

Top Liked Posts

  • There are no posts matching your filters.
  • 23
    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)
    
    [COLOR="DarkRed"]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[/COLOR]
    [COLOR="Green"]	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;[/color]
    [COLOR="DarkRed"]fi[/COLOR]

    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)
    #
    
    [COLOR="DarkRed"]# 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[/COLOR]
    	[COLOR="Green"]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;[/COLOR]
    [COLOR="DarkRed"]fi;[/COLOR]
    2
    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)
    
    [COLOR="DarkRed"]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[/COLOR]
    [COLOR="Green"]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;[/color]
    [COLOR="DarkRed"]fi[/COLOR]

    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)
    #
    
    [COLOR="DarkRed"]# 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[/COLOR]
    [COLOR="Green"]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;[/COLOR]
    [COLOR="DarkRed"]fi;[/COLOR]
    Hi guys! I recently installed lollipop touch wiz and installed the two scripts on the Op, managed to get the sql script to work fine but when the zip align tries to run it errors out in the log, any advice to what I've done wrong or if you guys know of a change in lollipop that doesn't play nice with zip aligning?
    Starting Automatic ZipAlign 06-04-2014 15:23:01
    ZipAligning *.apk
    ZipAligning *.apk Failed
    Automatic ZipAlign finished at 06-04-2014 15:23:01
    All I've done as far as modify the script is I got rid of the color tags.

    Also the sql log shows the correct date as well, where this script does not.

    Thank you in advance guys!
    1
    This looks like a great idea :good: 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".
    1
    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....
    1
    Any chance to make this flasheable thx!