简体   繁体   中英

Run Linux Shell Script On Boot

I have a Shell script that I want to run on boot. Every time that I start the device It'll run the script in the background. The script contains a while true loop and suppose to run constantly, at least until the device will be turned off. This is the script :

#!/bin/bash

cd /home/.../
while true 
do 
  sh ./update_logs.sh
sleep 1
done

After of plenty of searches I've came up with too much information which made a salad in my head. I've been advised to get to this folder /etc/init.d and put down my script there by using special pattern (LSB-compliant) which looks like this :

!#/bin/sh

start () {
    echo "application started";
    ./helloworld  # you should use an absolute path here instead of ./
}

stop () {

}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    *)
        echo "Usage start|stop";
esac

exit $?

Make the script executable by chmod +x, then make A symbolic link for the file by typing ln -s /etc/rc.d/init.d/run_update.sh /etc/init.d/rc5.d/S90run_update

This supposed to be the "hard way" while the "easy way" is putting my script in a folder /etc/rc.local where it shall boot my script after the main boot process.

Well, I don't have this kind of folder. What I to have in etc folder is rc.d which leads to sub folders : init.d rc0.d rc1.d rc2.d ... rc6.d

If the solution is the hard way by writing the code above, what is the minimum that I need to include in it? since I see different type of codes which include ### with descriptions and run levels I have a Linux Red Hat 4.6.3-2.

OK I think I understand. start a konsole session, then look for a hidden file called .bash_profile. If you do not find it in your home directory then it does not exit. Create it with pico (use pico .bash_profile). If the file exist, edit it with a link to your script. The next time you log into your system that file will run.

HOpe this helps.

in DEBIAN script should have at top

#!/bin/sh    
### BEGIN INIT INFO
# Provides:          SCRIPT_NAME_HERE_NO_PATH
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

....

then in shell must enable rc system links

update-rc.d SCRIPT_NAME_HERE_NO_PATH defaults
update-rc.d SCRIPT_NAME_HERE_NO_PATH enable
1. Add below lines in intit.rc: 
  chmod 0755 /system/bin/shellscript_name  //giving permissions to your shell_script
  start name_your_service      //starting your shellscrip

  service name_your_service /system/bin/shellscript_name     
      class main
      user root
      group shell system 
      seclabel u:r:shell:s0
      disabled


2. Goto the vendor directory and create your shell script under system/bin/shellscript_name.

3. Add your shell script under Android MakeFile:
include $(CLEAR_VARS)
LOCAL_MODULE        := module_name
LOCAL_MODULE_OWNER  := owner_name
LOCAL_MODULE_TAGS   := optional
LOCAL_MODULE_CLASS  := EXECUTABLES
LOCAL_SRC_FILES     := path to your .sh
LOCAL_MODULE_PATH   := $(PRODUCT_OUT)/system/bin/
include $(BUILD_PREBUILT)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM