前言:
客户提出新需求,要求每天晚上2:00-3:00,系统自动重启,主要是为了避免android系统长时间启动内存消耗过高导致系统卡顿。
当前计划两种方案,方案一在开发板外接可控制电源,使用串口可以与开发板通信,在凌晨2:00-3:00时,android应用发送命令控制电源重新上电操作,此方式实现比较复杂,但是可以对开发板完全断电操作,开发板所有外设都会重新启动;方案二在android系统中添加自动重启服务,此方式会调用系统reboot命令重启系统,设计简单,但是对一些开发板模块无法完成断电操作。
本文主要介绍方案二的实现。
1、服务源码
#!/system/bin/sh
USR_HDATE=02
#USR_MDATE=00
#USR_SDATE=00
USR_MDATE=$(( $RANDOM % 59 ))
USR_SDATE=$(( $RANDOM % 59 ))
echo USR_HDATE:$USR_HDATE
echo USR_MDATE:$USR_MDATE
echo USR_SDATE:$USR_SDATE
HDATE=`date +%H`
MDATE=`date +%M`
DATE=`date +%S`
echo SDATE:$SDATE
echo HDATE:$HDATE
echo MDATE:$MDATE
while [ $SDATE -ne $USR_SDATE ] || [ $MDATE -ne $USR_MDATE ] || [ $HDATE -ne $USR_HDATE ]
do
sleep 1
SDATE=`date +%S`
MDATE=`date +%M`
HDATE=`date +%H`
echo $HDATE-$MDATE-$SDATE $USR_HDATE:$USR_MDATE:$USR_SDATE
done
echo reboot time is $USR_HDATE:$USR_MDATE:$USR_SDATE > /sdcard/Android/Reboot.txt
sync
sleep 3
reboot
2、拷贝脚本到system
device/rockchip/common/device.mk
+#timing reboot for bin
PRODUCT_COPY_FILES += \
$(LOCAL_PATH)/timing_reboot.sh:system/bin/timing_reboot.sh
3、添加服务
device/rockchip/common/init.rockchip.rc
service timing_reboot /system/bin/timing_reboot.sh
class main
oneshot
4、SElinux权限处理
device/rockchip/common/sepolicy/file_contexts
/system/bin/timing_reboot.sh u:object_r:timing_reboot_exec:s0
device/rockchip/common/sepolicy/init.te
domain_trans(init, timing_reboot_exec, timing_reboot)
device/rockchip/common/sepolicy/timing_reboot.te
type timing_reboot, domain, mlstrustedsubject;
type timing_reboot_exec, exec_type, file_type;
init_daemon_domain(timing_reboot)
allow timing_reboot shell_exec:file { getattr open read};
allow timing_reboot toolbox_exec:file { getattr open read execute execute_no_trans };
allow timing_reboot storage_file:lnk_file { read };
allow timing_reboot tmpfs:dir { search };
allow timing_reboot mnt_user_file:dir { read search };
allow timing_reboot mnt_user_file:lnk_file { read };
allow timing_reboot sdcardfs:dir { search };
allow timing_reboot media_rw_data_file:dir { open read search };
allow timing_reboot system_file:file { execute_no_trans };
allow timing_reboot property_socket:sock_file { write };
allow timing_reboot powerctl_prop:property_service { set };
allow timing_reboot init:unix_stream_socket { connectto };