Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 22 Mar 2016 11:43:39 -0700
From:      Jim Long <list@museum.rain.com>
To:        freebsd-questions@freebsd.org
Cc:        Matthew Seaman <matthew@FreeBSD.org>
Subject:   Re: Seeking a solid startup script for node.js/forever
Message-ID:  <20160322184339.GA64556@ns.umpquanet.com>
In-Reply-To: <mailman.93.1458388802.58722.freebsd-questions@freebsd.org>
References:  <mailman.93.1458388802.58722.freebsd-questions@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Previously, Matthew Seaman wrote:
> 
> Message: 10
> Date: Fri, 18 Mar 2016 23:52:25 +0000
> From: Matthew Seaman <matthew@FreeBSD.org>
> To: freebsd-questions@freebsd.org
> Subject: Re: Seeking a solid startup script for node.js/forever
> Message-ID: <56EC94B9.3010003@FreeBSD.org>
> Content-Type: text/plain; charset="windows-1252"
> 
> On 18/03/2016 21:54, Jim Long wrote:
> > Sigh.  I should test before I post.
> > 
> > The fix below is not sufficient.  It "mostly works" from inside the jail:
> > 
> 
> daemon(8) sounds like what you need.
> 
> 	Cheers,
> 
> 	Matthew

Matthew:

Thanks for your reply.  I'm afraid however, I may need more of a clue than that.

In my /usr/local/etc/rc.d/iws service script, I changed the start() function, but left
the others (status, stop, restart) as they were:

--snip--
start()
{
  NODE_ENV=production
#  su -m www -c "exec ${forever} start -a -l ${HOME}/forever.log -o ${HOME}/output.log -e ${HOME}/error.log -p /var/run/forever ${script}"
  daemon -p /var/run/iws.pid -u www \
            ${forever} start -a -l ${HOME}/forever.log -o ${HOME}/output.log -e ${HOME}/error.log -p /var/run/forever ${script}
}

status()
{
  su -m www -c "exec ${forever} list"
}

stop()
{
  su -m www -c "exec ${forever} stop ${script}"
}

restart()
{
  su -m www -c "exec ${forever} restart ${script}"
}
--snip--

That makes no change in the symptoms: the script works fine from inside the jail, but from
the host level, the script does not fork the forever/node process into the background, and
therefore, 'service jail start' on the host does not complete and return to a shell prompt.


Script started on Tue Mar 22 10:21:23 2016
# I'm running just one jail on this host:
my_host : 10:21:23 /root# jls
   JID  IP Address      Hostname       Path
     3  10.16.231.41    my_jail        /jail/my_jail
my_host : 10:21:29 /root# jexec my_jail bash -l

# Now I'm inside the jail.  iws is disabled by default; I'll enable it:
my_jail : 10:21:37 /# grep ^iws /etc/rc.conf
iws_enable="NO"
my_jail : 10:21:47 /# echo 'iws_enable="YES"' >> /etc/rc.conf

# here is the service script in full, with an eof marker at the end:
my_jail : 10:22:02 /# cat /usr/local/etc/rc.d/iws ; echo '# --eof--'
#!/bin/sh

# PROVIDE: forever
# REQUIRE: NETWORKING SERVERS DAEMON
# BEFORE:  LOGIN
# KEYWORD: shutdown

# Taken from http://habrahabr.ru/post/137857/

. /etc/rc.subr

name="forever"
forever="/usr/local/bin/node /usr/local/bin/forever"
workdir="/usr/local/IWS4"
#script="web.js"
#script="app.js"
script="${workdir}/app.js"

rcvar=iws_enable
extra_commands="status"

start_cmd="start"
status_cmd="status"
stop_cmd="stop"
restart_cmd="restart"

load_rc_config $name
eval "${rcvar}=\${${rcvar}:-'NO'}"

HOME=/usr/local/IWS4
start()
{
  NODE_ENV=production
#  su -m www -c "exec ${forever} start -a -l ${HOME}/forever.log -o ${HOME}/output.log -e ${HOME}/error.log -p /var/run/forever ${script}"
  daemon -p /var/run/iws.pid -u www \
            ${forever} start -a -l ${HOME}/forever.log -o ${HOME}/output.log -e ${HOME}/error.log -p /var/run/forever ${script}
}

status()
{
  su -m www -c "exec ${forever} list"
}

stop()
{
  su -m www -c "exec ${forever} stop ${script}"
}

restart()
{
  su -m www -c "exec ${forever} restart ${script}"
}

run_rc_command "$1"
# --eof--

# Inside the jail, the service starts okay:
my_jail : 10:22:29 /# service iws start
my_jail : 10:23:34 /# warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: /usr/local/IWS4/app.js

# and it stops okay:
service iws stop
info:    Forever stopped process:
data:        uid  command             script                 forever pid   logfile                     uptime      
[0] tdVb /usr/local/bin/node /usr/local/IWS4/app.js 13497   13498 /usr/local/IWS4/forever.log 0:0:0:8.400 

# a second stop throws the correct error:
my_jail : 10:23:43 /# service iws stop
error:   Forever cannot find process with index: /usr/local/IWS4/app.js

my_jail : 10:23:47 /# service iws start
my_jail : 10:23:48 /# warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: /usr/local/IWS4/app.js

# a second start also throws the correct error:
service iws start
daemon: process already running, pid: 13515

# status works:
my_jail : 10:23:50 /# service iws status
info:    Forever processes running
data:        uid  command             script                 forever pid   logfile                     uptime      
data:    [0] H2tR /usr/local/bin/node /usr/local/IWS4/app.js 13516   13517 /usr/local/IWS4/forever.log 0:0:0:4.967 

# restart works:
my_jail : 10:23:54 /# service iws restart
info:    Forever restarted process(es):
data:        uid  command             script                 forever pid   logfile                     uptime       
data:    [0] H2tR /usr/local/bin/node /usr/local/IWS4/app.js 13516   13517 /usr/local/IWS4/forever.log 0:0:0:12.382 
my_jail : 10:24:01 /# service iws status
info:    Forever processes running
data:        uid  command             script                 forever pid   logfile                     uptime    
data:    [0] H2tR /usr/local/bin/node /usr/local/IWS4/app.js 13516   13533 /usr/local/IWS4/forever.log 0:0:0:2.6 

# when I return to the host, 'service jail stop' works:
my_jail : 10:24:03 /# logout
my_host : 10:25:54 /root# service jail stop
Stopping jails: my_jail.

# but from the host, 'service jail start' does not terminate:
my_host : 10:26:02 /root# time service jail start
Starting jails:
load: 0.01  cmd: node 13914 [uwait] 123.25r 0.25u 0.02s 0% 33268k
^C

real    2m7.917s
user    0m0.007s
sys     0m0.015s
my_host : 10:28:17 /root# logout

Script done on Tue Mar 22 10:28:26 2016


Have I mis-applied the daemon utility?  Can you be more explicit about
how I could apply it to my use case?

I tried using the -f flag to daemon(8) with no improvement.  Also, forcing
daemon into the background using '&' made no difference:

  daemon -p /var/run/iws.pid -u www \
            ${forever} start -a -l ${HOME}/forever.log -o ${HOME}/output.log -e ${HOME}/error.log -p /var/run/forever ${script} &

Thank you very much!  Please CC: me on any replies sent to the list.

Jim



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20160322184339.GA64556>