0

based on this post here I tried to create an executable in folder /etc/init.d/ with the name raumserver with the following content:

#! /bin/sh
# /etc/init.d/raumserver

### BEGIN INIT INFO
# Provides:          raumserver
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Carry out specific functions when asked to by the system
case "$1" in
   start)
    echo "Starting raumserver.js"
    # run application you want to start
    node /home/openhabian/node_modules/node-raumserver/raumserver.js > /home/openhabian/node_modules/node-raumserver/logs/RPiraumserver.js
    #/home/pi/downloads/node-v0.10.24-linux-arm-pi/bin/node /home/pi/test.js >> /home/pi/test.log
   ;;
   stop)
    echo "Stopping raumserver.js"
    # kill application you want to stop
    killall -9 node
    # Not a great approach for running
    # multiple node instances
    ;;
  *)
    echo "Usage: /etc/init.d/raumserver {start|stop}"
    exit 1
    ;;
esac

exit 0

You see that my nodeJS application resides in /home/openhabian/node_modules/node-raumserver/ and is named raumserver.js

When I run this executable via /etc/init.d/raumserver start I get the errormessage that WARNING: No configurations found in configuration directory:/etc/init.d/config which leads to an error because within this configuration it's stated to use another port instead of 8080 which is already in use. Thus the executable itself doesn't run.

This is based on the fact that this executable is looking for the folder config in the current path the executable resides in /etc/init.d/config which doesn't exist there. As the path to the nodeJS application is /home/openhabian/node_modules/node-raumserver/ I would expect it to look for the folder there which would be available there.

Why does this happen and how can I change that?

My plan is to get this executable running on startup via daemon. My plan there is to go for chmod +x /etc/init.d/raumserver and then sudo update-rc.d raumserver defaults which I read online and should work.

Any idea of how to tell the application to look in referenced nodeJS-application folder? I shortly thought about copying the config folder to /etc/init.d/ but I guess this would be very unclever for future updates etc etc.

Thanks in advance

2 Answers2

0

It seems that some of your code is using . as the path base rather than using __dirname which is the dirname of the module.

If you want to address files relative to the module, you need to use:

absolute_path = path.join(__dirname, relative_path)

Rather than letting . resolve to process.cwd()

When setting up services it helps to print process.cwd() also process.env specifically process.env.PATH. If those don't output the right values, you can fix them in the service setup or in your program,

Dan D.
  • 73,243
  • 15
  • 104
  • 123
0

As the nodeJS app is not mine and I'm not capable of changing it I guess this will be no option. :-(

But another question: Would it be possible to have the current executable run (on startup) another executable which lies in the relativ path which will then run smoothly?

What I tried is to change the content of the executable raumserver to the following:

### BEGIN INIT INFO
# Provides:          raumserver
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Carry out specific functions when asked to by the system
case "$1" in
   start)
    echo "Starting raumserver.js"
    # run application you want to start
    #node /home/openhabian/node_modules/node-raumserver/raumserver.js > /home/openhabian/node_modules/node-raumserver/logs/RPiraumserver.js
    /home/openhabian/node_modules/node-raumserver/raumstartup >> /home/openhabian/node_modules/node-raumserver/logs/raumstartup.log
   ;;
   stop)
    echo "Stopping raumserver.js"
    # kill application you want to stop
    killall -9 node
    # Not a great approach for running
    # multiple node instances
    ;;
  *)
    echo "Usage: /etc/init.d/raumserver {start|stop}"
    exit 1
    ;;
esac

exit 0

and the raumstartup looks like this:

### BEGIN INIT INFO
# Provides:          raumserver
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Carry out specific functions when asked to by the system
case "$1" in
   start)
    echo "Starting raumserver.js"
    # run application you want to start
    node /home/openhabian/node_modules/node-raumserver/raumserver.js > /home/openhabian/node_modules/node-raumserver/logs/RPiraumserver.js
    #/home/openhabian/node_modules/node-raumserver/raumstartup >> /home/openhabian/node_modules/node-raumserver/logs/raumstartup.log
   ;;
   stop)
    echo "Stopping raumserver.js"
    # kill application you want to stop
    killall -9 node
    # Not a great approach for running
    # multiple node instances
    ;;
  *)
    echo "Usage: /etc/init.d/raumserver {start|stop}"
    exit 1
    ;;
esac

exit 0

But somehow this approach doesn't work. :-( I know it's not best practice but as of now this still seems like a legit way... Any thoughts on my approach or - even better - a reason why it doesn't work?

I can manually start the first file via sudo /etc/init.d/raumserver start but it doesn't start the raumstartup :-(