简体   繁体   English

在启动时运行Linux Shell脚本

[英]Run Linux Shell Script On Boot

I have a Shell script that I want to run on boot. 我有一个Shell脚本,我想在启动时运行。 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. 该脚本包含一会儿true循环,并假设它至少在关闭设备之前一直运行。 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 : 建议我使用以下特殊模式(与LSB兼容)将文件放到/etc/init.d文件夹并将脚本放在那里:

!#/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 通过chmod + x使脚本可执行,然后通过键入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. 这应该是“硬方法”,而“简易方法”是将我的脚本放在/etc/rc.local文件夹中,该文件夹将在主引导过程之后引导我的脚本。

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 我有在etc文件夹rc.d导致子文件夹: 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. 由于我看到了不同类型的代码,其中包括带有描述和运行级别的###,因此我使用了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. 开始konsole会话,然后查找名为.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). 使用pico创建它(使用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 在DEBIAN脚本中应该位于顶部

#!/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 那么在外壳中必须启用rc系统链接

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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