0

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.

Marked One
  • 59
  • 4
  • 13
  • Look under `/etc/rc.d`. There should be an `rc.local`. If there isn't, you can create one. In other words, you want a script file, `/etc/rc.d/rc.local`, not a folder, `/etc/rc.local`. – lurker Aug 31 '15 at 11:45
  • I was thinking that I might need to create rc.local. It something that the OS is familiar with? I just create one by using "vi"? – Marked One Aug 31 '15 at 11:46
  • Yes the OS knows about it. – lurker Aug 31 '15 at 11:52
  • 1
    Not knowing what your script actually does: Can't you just schedule it to -- for instance -- run every minute? (you can do this with cron) – Franz-Robert van Vugt Aug 31 '15 at 12:02
  • Which distribution are you running? You said Red Hat 4.6.3-2, but that is not likely to be correct. What does `/etc/redhat-release` say? – larsks Aug 31 '15 at 12:06
  • -bash: /etc/redhat-release: Permission denied – Marked One Aug 31 '15 at 12:07
  • About the "Crontab"... the idea seems to be good in generally but unfortunately the script is fetching info from an XML file which updates every second. It's like a real time system, something that changes faster than a minute. It's probably the worst idea ever to use a while true loop, but for now it works. I only need that the system will boot it automatically. – Marked One Aug 31 '15 at 12:11
  • See: [**Running Additional Programs at Boot Time**](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Installation_Guide/s1-boot-init-shutdown-run-boot.html) – David C. Rankin Aug 31 '15 at 13:39

3 Answers3

1

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
bortunac
  • 4,642
  • 1
  • 32
  • 21
0

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.

RonY_Patel
  • 11
  • 2
  • I'll try your solution too. Fortunately the previous solution with the rc.local file was the best one. The problem from the first was that I didn't have this file in my rc.d directory. Eventually I understood that I could create this file on my own which eventually solved this case. – Marked One Sep 03 '15 at 12:11
0
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)