start express on reboot

106 views
Skip to first unread message

Uomo di Carbone

unread,
Aug 28, 2015, 10:41:48 PM8/28/15
to nodejs
I am currently running express on an Rpi at: http://111001.cc
Is there a way to make it part of rc ?  sudo update-rc.d vsftpd defaults
I would like to start the service and have is run automatically on reboot like ftp apache nginx ssh ...
rather than issue a
$DEBUG=myapp npm start

Thanks you

David Saliba

unread,
Aug 30, 2015, 2:29:35 PM8/30/15
to nodejs
Well there are two options 

1) easy way Cron 

 create an entry in your crobtab ( usually crontab -e will open the editor for you)

<crontab -e> ------------------ editor
@reboot  $DEBUG=myapp npm start 


better practice would be create a script say under /usr/src/scripts ( chmod it to 755 or similar ) and call the script @reboot. such as 


<crontab -e> ------------------ editor
@reboot /usr/src/script/myscript.sh >> /var/log/myscript.log


Like that you actually have your console outputs saved in a log file as well.




2) The more complex one is create a daemon script ( varies slightly across distributions.

#!/bin/sh
#
# /etc/init.d/mysystem
# Subsystem file for "MySystem" server
#
# chkconfig: 2345 95 05	(1)
# description: MySystem server daemon
#
# processname: MySystem
# config: /etc/MySystem/mySystem.conf
# config: /etc/sysconfig/mySystem
# pidfile: /var/run/MySystem.pid

# source function library
. /etc/rc.d/init.d/functions

# pull in sysconfig settings
[ -f /etc/sysconfig/mySystem ] && . /etc/sysconfig/mySystem	(2)

RETVAL=0
prog="MySystem"
.
.	(3)
.

start() {	(4)
	echo -n $"Starting $prog:"
	.
	.	(5)
	.
	RETVAL=$?
	[ "$RETVAL" = 0 ] && touch /var/lock/subsys/$prog
	echo
}

stop() {	(6)
	echo -n $"Stopping $prog:"
	.
	.	(7)
	.
	killproc $prog -TERM
	RETVAL=$?
	[ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/$prog
	echo
}

reload() {	(8)
	echo -n $"Reloading $prog:"
	killproc $prog -HUP
	RETVAL=$?
	echo
}

case "$1" in	(9)
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	reload)
		reload
		;;
	condrestart)
		if [ -f /var/lock/subsys/$prog ] ; then
			stop
			# avoid race
			sleep 3
			start
		fi
		;;
	status)
		status $prog
		RETVAL=$?
		;;
	*)	(10)
		echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
		RETVAL=1
esac
exit $RETVAL




I use both methods depending on the mode of operation and the level of control I want to give users on the box.

1st is adequate second is proper.



david.

Stephen Vickers

unread,
Aug 31, 2015, 12:32:03 PM8/31/15
to nodejs
This module should do it for you:

https://www.npmjs.com/package/os-service

David Saliba

unread,
Aug 31, 2015, 4:32:54 PM8/31/15
to nod...@googlegroups.com
@Stephen  Thanks  I never came across that.
Very interesting thanks


On Mon, Aug 31, 2015 at 6:29 PM, Stephen Vickers <vortex...@gmail.com> wrote:
This module should do it for you:

https://www.npmjs.com/package/os-service

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/GzZFvCh1yXk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/8f87e8c9-81ed-4150-9c48-3acc6170cd7f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthias Bleyl

unread,
Sep 22, 2015, 2:33:51 AM9/22/15
to nodejs

https://www.npmjs.com/package/os-service

I tried to use os-service for adding and running a Windows-Service (Windows 10).

Basically, adding and running services works fine for me (this means: I was able to add a service and to run this service from the service manager).
However, I ended up with two problems:

- as soon as I use things like "console.log(..)" or "process.stdout.write('\x1Bc')" in my code, I could not start my service anymore - but this is not a big problem for me
- as soon as I include the express framework, I could not start my service anymore  !!!!

In both cases (may be even in more cases) starting the service causes an error 7000 resp. 7009 in Window's Service Control Manager ("timeout error", but already after only a few milliseconds!). Of course, I double-checked the manual start of my application, it works always fine.

Any ideas?
 

Stephen Vickers

unread,
Sep 22, 2015, 12:06:53 PM9/22/15
to nodejs
Can you provide an example failing program?

Matthias Bleyl

unread,
Oct 11, 2015, 12:37:23 AM10/11/15
to nodejs
Here is my code:

process.chdir(__dirname);

var service = require ("os-service");
var express = require('express');
var fs = require ("fs");

var serviceName = "myService";
switch (process.argv[2])
{
 
case ("--add"):
    service
.add (serviceName, {programArgs: ["--run"]}, function(error){
       
if (error)console.warn(error); else console.log(serviceName+" added");
   
});
   
break;
 
case ("--remove"):
    service
.remove (serviceName, function(error){
       
if (error)console.warn(error); else console.log(serviceName+" removed");
   
});
   
break;
 
case ("--run"):
var logStream = fs.createWriteStream (serviceName + ".log");
 service
.run (logStream, function () {
        service
.stop (0);
   
});
 
// appliation code
 setInterval
(function () {
 console
.log (new Date ().toString ());
 
}, 1000);
 
break;
}


---------------------------------------------------------------

All works fine from console (in Windows): --add, --remove, --run  .. no problem at all.
However, starting the added service from the Windows service manager fails, unfortunately!
To get it running as a service, I have to remove the line "var express = require('express');"
Something does not work together between express and os-service?
Express was installed using "npm install express".

Matthias 
 

Stephen Vickers

unread,
Oct 11, 2015, 4:19:04 PM10/11/15
to nodejs
Try moving the following line:

var express = require('express');

Down to after the `service.run()` call:

var express = require('express');

setInterval (function () {
   console
.log (new Date ().toString ());
}, 1000);

Steve

Matthias Bleyl

unread,
Oct 12, 2015, 12:04:11 PM10/12/15
to nodejs

Try moving the following line:

var express = require('express');

 
This works for me! 

I suggest to add a note in the example on your web page.
It's now:

    var logStream = fs.createWriteStream (process.argv[1] + ".log");

    service
.run (logStream, function () {
        service
.stop (0);
   
});

   
// Run service program code...

but should be:

    var logStream = fs.createWriteStream (process.argv[1] + ".log");

    service
.run (logStream, function () {
        service
.stop (0);
   
});

   
// Run service program Code INCLUDING ALL NEEDED "REQUIRE" CALLS ...


Note, that it is also not allowed to call "consolo.log(...)" before "service.run()".

This example code can not be started from Windows Service Manager because "console.log()" is called before "service.run()":

    case ("--run"):
         
var logStream = fs.createWriteStream (serviceName + ".log");
   
         console
.log("stream created, going to start the service");
         service
.run (logStream, function ()
         
{
             service
.stop (0);
         
});
 

 
Lets say, its a little bit tricky, but it works. Nice lib by the way!
 
Reply all
Reply to author
Forward
0 new messages