简化常用指令操作,写个脚本
比如设置环境变量和scp往机器中拷贝文件,整个脚本简化下操作,省得重复敲不少命令
#!/bin/bash source /opt/myir-imx-fb-qt5/4.1.15-2.0.1/environment-setup-cortexa7hf-neon-poky-linux-gnueabi scp myapp root@192.168.79.1:/app/my_app/bin
或者是启动应用,手动执行可能还得进入某个目录或设置环境变量,写一个脚本搞定:
run_app.sh
#!/bin/bash export LD_LIBRARY_PATH=/app/city_app/lib/ echo $LD_LIBRARY_PATH cd /app/city_app/bin/ ./b503_app
查找应用并设置应用的环境变量并运行
run_app.sh
应用升级脚本
start_remoteupdate.sh
#!/bin/bash singelName="remoteupdate" fileName="/usr/bin/remoteupdate" configFile="/app/update.conf" function CheckProcess() { PROCESS_NUM=`ps | grep "$1" | grep -v "grep" | wc -l` return $PROCESS_NUM } function startProcess() { echo "check process run status" CheckProcess $singelName if [ $? -eq 0 ];then echo "progress is not run" else echo "process is running" #killall -9 $singelName #sleep 1 exit 0 fi echo "start remoteupdate ..." $singelName & echo "start end" } #copy config file if [ -f $configFile ];then echo "config file exist" else echo "copy update.conf file" cp /app/city_app/opt/update.conf /app/ sync if [ -f $configFile ];then echo "copy ok" fi fi #start process remoteupdate if [ ! -f $fileName ];then echo "copy remoteupdate process to /usr/bin" cp /app/city_app/opt/$singelName /usr/bin/ sync if [ -f $fileName ];then echo "copy ok" startProcess exit 0 else echo "copy fail" exit 1 fi else echo "remoteupdate file exsit" startProcess exit 0 fi
应用监控和升级服务脚本
app_monitor.sh
#!/bin/bash #######city app monitor deamon ######## function checkAppRun() { flag=0 val=0 cnt=0 max=$2 #echo "checkApprun...$1" while [ $val -lt $max ] do cnt=$(ps | grep $1 | wc -l) if [ $cnt -le 1 ]; then let flag+=1 sleep 1 let val+=1 else break fi done if [ $flag -ge $max ]; then return 0 #$1 app is not running else return 1 #$1 app has been run fi } function CheckProcess() { PROCESS_NUM=`ps | grep "$1" | grep -v "grep" | wc -l` return $PROCESS_NUM } function monitor_app() { while true do CheckProcess b503_app if [ $? -ne 1 ] ; then if [ $? -ne 0 ]; then killall -9 b503_app fi source /etc/init.d/app_update_b503_app.sh fi # CheckProcess remoteupdate # if [ $? -ne 1 ] ; # then # if [ $? -ne 0 ]; # then # killall -9 remoteupdate # fi # source /etc/init.d/app_start_remoteupdate.sh # fi done } function monitor_b503_ft() { while true do CheckProcess b503_ft if [ $? -eq 0 ] ; then break else sleep 1 fi done } function monitor_ft_app() { while true do checkAppRun b503_ft 2 if [ $? -eq 0 ] ; then break fi done } ################main##################### ####synchronize date and rtc time####### #date #hwclock -w --local ######################################## source /etc/init.d/app_update_b503_ft.sh #monitor_ft_app & checkAppRun b503_ft 2 if [ $? -eq 0 ] ; then source /etc/init.d/app_update_b503_app.sh monitor_app & else monitor_b503_ft source /etc/init.d/app_update_b503_app.sh monitor_app & fi
应用ipk包安装脚本
#!/bin/bash #b503 application update mechnism #search directory in order, /update,/media/usb/ #set -e buzzGPIO="/sys/class/gpio/gpio15" curdir=$(pwd) #b503_city_app_name=city_app #enable buzzse for notifying success function beep_notify() { if [ ! -d "$buzzGPIO" ]; then echo 15 > /sys/class/gpio/export fi if [ -d "$buzzGPIO" ]; then echo "out" > "/sys/class/gpio/gpio15/direction" echo "1" > "/sys/class/gpio/gpio15/value" sleep 1 echo "0" > "/sys/class/gpio/gpio15/value" fi } #install all ipks in specified directory function install_ipks() { path=$1 files=$(ls $path) for filename in $files do if [ "${filename##*.}"x = "ipk"x ]; then echo $filename cd $path opkg install --force-reinstall --force-downgrade --force-overwrite $filename # sleep 1 fi done } #check and create directory by specified path function check_and_mkdir() { result_path=$1 result_name=$2 cd $result_path # echo "$result_path" if [ ! -d $result_name ]; then #echo "mkdir $result_name" mkdir -p $result_name #else # echo "$result_name is exit!!" fi } #check if updating from update directory function isUpdateIpkFromUpdir() { #echo "1--->search update ipk directory." cd /update #cnt=$(find -name "*.ipk" | wc -l) if [ -d ipk ]; then cnt=$(ls ipk | wc -l) if [ $cnt -ge 1 ] ; then echo "1.1--->check update flag !" cnt=$(find -name "update.txt" | wc -l ) if [ $cnt -ge 1 ]; then var=$(cat update.txt) if [[ $var -eq 1 ]]; then echo "1.2--->update-flag is 1!" return 0 else echo "1.3--->update flag is 0" return 1 fi else echo "1.8--->no update.txt.." return 1 fi else echo "1.9--->there is no ipk files in update directory!" return 1 fi fi return 1 } #check if updating bin from update directory function isUpdateBinFromUpdir() { #echo "1--->search update bin directory." cd /update if [ -d bin ]; then cnt=$(ls bin | wc -l) if [ $cnt -ge 1 ] ; then cnt=$(find -name "update.txt" | wc -l ) if [ $cnt -ge 1 ]; then var=$(cat update.txt) if [[ $var -eq 2 ]]; then return 0 else return 1 fi else return 1 fi fi fi return 1 } #check if updating lib from update directory function isUpdateLibFromUpdir() { #echo "1--->search update lib directory." cd /update i if [ -d lib ]; then cnt=$(ls lib | wc -l) if [ $cnt -ge 1 ] ; then cnt=$(find -name "update.txt" | wc -l ) if [ $cnt -ge 1 ]; then var=$(cat update.txt) if [[ $var -eq 3 ]]; then return 0 else return 1 fi else return 1 fi fi fi return 1 } #check if updating audio from update directory function isUpdateAudioFromUpdir() { #echo "1--->search update audio directory." cd /update if [ -d audio ]; then cnt=$(ls audio | wc -l) if [ $cnt -ge 1 ] ; then cnt=$(find -name "update.txt" | wc -l ) if [ $cnt -ge 1 ]; then var=$(cat update.txt) if [[ $var -eq 4 ]]; then return 0 else return 1 fi else return 1 fi fi fi return 1 } #check if updating form udisk function isUpdateFromUdisk() { #echo "2--->search u disk directory." cd /media cnt=$(find -name "usb" | wc -l ) if [ $cnt -ge 1 ]; then cd /media/usb cnt=$(find -name "*.ipk" | wc -l) if [ $cnt -ge 1 ] ; then echo "2.1--->udisk,find ipk files" return 0 else echo "2.2--->udisk,no b503-app.ipk......" return 1 fi else #echo "2.9:no udisk is inserted......." return 1 fi }
服务管理,启动或停止服务
service.sh
#!/usr/bin/env sh export ALIPAY_ROOT=$(cd `dirname $0`; cd ../../; pwd) # define the global value pid= start_service() { cd ${ALIPAY_ROOT}/iotsdk/bin nohup ./alipay_iotd >/dev/null 2>&1 & } PS_LINE=`ps |grep alipay_iotd|grep -v grep` if [ ! -z "${PS_LINE}" ]; then #pids=(${PS_LINE// / }) pid=`echo ${PS_LINE} | cut -d ' ' -f 1` #pid=${pids[0]} echo "snapshot pid is ${pid}" fi case $1 in "keepalive") if [ -z "${pid}" ]; then start_service fi ;; "startup") if [ -z "${pid}" ]; then start_service else echo "Service was already started!" fi ;; "shutdown") if [ -n "${pid}" ]; then kill -9 "${pid}" echo "Service was terminated!" fi ;; "restart") if [ -n "${pid}" ]; then echo "Stop service..." kill -9 "${pid}" echo "Service was terminated!" fi sleep 3 start_service ;; "status") echo "Service is running on proc: ${pid}" ;; *) echo "Unsupported command!" ;; esac
统一改写目录下的文件属性
#!/bin/bash #查找当前目录下(递归级数1)的所有目录文件 SRC_DIR=$(find ./ -maxdepth 1 -type d) #变量SRC_DIR可以用${}引用,可以$直接引用,但不可以用$()引用 echo ${SRC_DIR} #将当前目录下所有一级目录文件的Other写属性去掉 chmod o-w ${SRC_DIR}
自动下载并构建freetype脚本
#!/bin/bash set -x set -o errexit -o nounset # 22.0.16 is the libtool version of 2.9.0 if pkg-config --atleast-version 22.0.16 freetype2; then exit; fi pushd $HOME wget http://download.savannah.gnu.org/releases/freetype/freetype-2.9.tar.bz2 tar xf freetype-2.9.tar.bz2 pushd freetype-2.9 ./autogen.sh ./configure --prefix=$HOME/.local make -j4 install popd popd
首次安装脚本install.sh
#!/usr/bin/env sh #SOURCE_DIR=`pwd` SOURCE_DIR=/app/city_app/alipay TARGET_DIR=/app/alipay if [ ! -d $TARGET_DIR ]; then mkdir $TARGET_DIR fi if [ ! -d $TARGET_DIR/iotsdk ]; then mkdir $TARGET_DIR/iotsdk fi if [ ! -d $TARGET_DIR/iotsdk/bin ]; then mkdir $TARGET_DIR/iotsdk/bin fi if [ ! -d $TARGET_DIR/iotsdk/conf ]; then mkdir $TARGET_DIR/iotsdk/conf fi if [ ! -d $TARGET_DIR/runtime ]; then mkdir $TARGET_DIR/runtime fi if [ -e $SOURCE_DIR/iotsdk/bin/alipay_iotd ]; then cp $SOURCE_DIR/iotsdk/bin/alipay_iotd $TARGET_DIR/iotsdk/bin fi if [ -e $SOURCE_DIR/iotsdk/bin/alipay_iotmd ]; then cp $SOURCE_DIR/iotsdk/bin/alipay_iotmd $TARGET_DIR/iotsdk/bin fi if [ -e $SOURCE_DIR/iotsdk/bin/monitor.sh ]; then cp $SOURCE_DIR/iotsdk/bin/monitor.sh $TARGET_DIR/iotsdk/bin fi if [ -e $SOURCE_DIR/iotsdk/bin/service.sh ]; then cp $SOURCE_DIR/iotsdk/bin/service.sh $TARGET_DIR/iotsdk/bin fi
autogen.sh
#!/bin/sh # Run this to generate all the initial makefiles, etc. test -n "$srcdir" || srcdir=`dirname "$0"` test -n "$srcdir" || srcdir=. olddir=`pwd` cd $srcdir #echo -n "checking for ragel... " #which ragel || { # echo "You need to install ragel... See http://www.complang.org/ragel/" # exit 1 #} echo -n "checking for pkg-config... " which pkg-config || { echo "*** No pkg-config found, please install it ***" exit 1 } echo -n "checking for libtoolize... " which glibtoolize || which libtoolize || { echo "*** No libtoolize (libtool) found, please install it ***" exit 1 } echo -n "checking for gtkdocize... " if which gtkdocize ; then gtkdocize --copy || exit 1 else echo "*** No gtkdocize (gtk-doc) found, skipping documentation ***" echo "EXTRA_DIST = " > gtk-doc.make fi echo -n "checking for autoreconf... " which autoreconf || { echo "*** No autoreconf (autoconf) found, please install it ***" exit 1 } echo "running autoreconf --force --install --verbose" autoreconf --force --install --verbose || exit $? cd $olddir test -n "$NOCONFIGURE" || { echo "running configure $@" "$srcdir/configure" "$@" }
引用
linux系统中开机自启的三种方式_灬紫荆灬-CSDN博客_linux开机自启动
linux /etc/init.d和/etc/rc/init.d联系,运行级别,/etc/rc.d/init.d执行流程_mengzuchao的专栏-CSDN博客