Script in rc.d can not startup on booting

i install openhab in openwrt in a x86_64 machine, the start service command is /opt/openhab/start.sh
i follow the installation guide from openhab.org and write a init.d script depend on the openhab2 startup script. the script can startup openhab when i run it manually. but when i use it in rc.d it can not startup openhab. anybody know what is the problem?
my openwrt

BusyBox v1.35.0 (2023-04-27 20:28:15 UTC) built-in shell (ash)

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt 22.03.5, r20134-5f15225c1e
 -----------------------------------------------------

my openhab just run start.sh manually

root@OpenWrt:~# /opt/openhab/start.sh
Launching the openHAB runtime...

                           _   _     _     ____  
   ___   ___   ___   ___  | | | |   / \   | __ ) 
  / _ \ / _ \ / _ \ / _ \ | |_| |  / _ \  |  _ \ 
 | (_) | (_) |  __/| | | ||  _  | / ___ \ | |_) )
  \___/|  __/ \___/|_| |_||_| |_|/_/   \_\|____/ 
       |_|       4.0.2 - Release Build

Use '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
To exit, use '<ctrl-d>' or 'logout'.

openhab>               
```````````````````````````````````````````````````````````````````````````````````````````````````
my init.d scripting
``````````````````````````````````````````````````````````````````````````````````
!/bin/sh /etc/rc.common                                                                                        
START=99                                                                                                        
STOP=1                                                                                                          
openhab_pidfile="/var/run/openhab.pid"                                                                          
                                                                                                                
# Set OH-User                                                                                                   
OHUSER=openhab                                                                                                  
# Set OH-Path                                                                                                   
OHPATH=/opt/openhab                                                                                             
                                                                                                                
# uncomment and set if necessary to obtain correct Codepage and language                                        
# export LC_ALL=de_DE.UTF-8                                                                                     
                                                                                                                
  start() {                                                                                                     
        echo "Starting openHAB"                                                                                 
        sudo -u ${OHUSER} screen -S openHAB -dm sh ${OHPATH}/start.sh                                           
        /usr/bin/pgrep openhab > $openhab_pidfile                                                               
        echo "openHAB: Started Service with PID $(cat $openhab_pidfile)."                                       
        }                                                                                                       
                                                                                                                
  stop() {                                                                                                      
        echo "Stopping openHAB"                                                                                 
#        /opt/openhab/runtime/bin/stop
}
````````````````````````````````````````````````````````````````````````````````````````````````````
my startup script running manually
````````````````````````````````````````````````````````````````````````````````````````````````````
root@OpenWrt:/etc/init.d# ./openhab start
Starting openHAB
openHAB: Started Service with PID 22533.

ps -axf
22542 ?        Ss     0:00 SCREEN -S openHAB -dm sh /opt/openhab/start.sh
22544 pts/1    Ss+    0:00  \_ /bin/sh /opt/openhab/runtime/bin/karaf
22667 pts/1    Sl+    2:49      \_ /opt/jdk17/bin/java -XX:-UsePerfData -Dopenhab.home=/opt/openhab -Dopenhab.conf=/opt/openhab/conf -Dopenhab.runtime

ls /var/run/openhab.pid -al
-rw-r--r--    1 root     root             6 Sep  7 23:33 /var/run/openhab.pid

systemlog
Thu Sep  7 23:33:49 2023 authpriv.notice sudo:     root : TTY=pts/0 ; PWD=/etc/init.d ; USER=openhab ; COMMAND=/usr/sbin/screen -S openHAB -dm sh /opt/openhab/start.sh

BUT when i run it in rc.d when system booting up
there is no related item in "ps -axf"
but it has openhab.pid file
-rw-r--r-- 1 root root 5 Sep 7 23:42 /var/run/openhab.pid
in systemlog i have got these
Thu Sep 7 23:42:21 2023 daemon.notice procd: /etc/rc.d/S99openhab: Starting openHAB
Thu Sep 7 23:42:21 2023 authpriv.notice sudo: root : PWD=/ ; USER=openhab ; COMMAND=/usr/sbin/screen -S openHAB -dm sh /opt/openhab/start.sh
Thu Sep 7 23:42:21 2023 daemon.notice procd: /etc/rc.d/S99openhab: openHAB: Started Service with PID 2824.
it looks like openhab had been started up and been stoped just after that.
is there anybody has experience on this or has any idea on make it run in rc.d when booting.
thanks alot
any suggestion is welcomed to.

Echo may not work, when the script is run headless...

Use "logger" instead, to pass text into log items.

i have make a change as your suggestion, except the systemlog changed to

Fri Sep  8 07:49:24 2023 user.notice root: Starting openHAB
Fri Sep  8 07:49:24 2023 authpriv.notice sudo:     root : PWD=/ ; USER=openhab ; COMMAND=/usr/sbin/screen -S openHAB -dm sh /opt/openhab/start.sh
Fri Sep  8 07:49:24 2023 user.notice root: openHAB: Started Service with PID 2935.

nothing turned to better. the openhab thread did not listed in ps -axf.

Do you need to use "screen" there?

1 Like