From owner-freebsd-questions@freebsd.org Tue Mar 22 18:43:46 2016 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4FF7AAD9079 for ; Tue, 22 Mar 2016 18:43:46 +0000 (UTC) (envelope-from list@museum.rain.com) Received: from ns.umpquanet.com (ns.umpquanet.com [98.158.10.80]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 3C13E12D6; Tue, 22 Mar 2016 18:43:45 +0000 (UTC) (envelope-from list@museum.rain.com) Received: from ns.umpquanet.com (localhost [127.0.0.1]) by ns.umpquanet.com (8.14.9/8.14.9) with ESMTP id u2MIhdcP070149; Tue, 22 Mar 2016 11:43:39 -0700 (PDT) (envelope-from list@museum.rain.com) Received: (from james@localhost) by ns.umpquanet.com (8.14.9/8.14.9/Submit) id u2MIhdcl070148; Tue, 22 Mar 2016 11:43:39 -0700 (PDT) (envelope-from list@museum.rain.com) Date: Tue, 22 Mar 2016 11:43:39 -0700 From: Jim Long To: freebsd-questions@freebsd.org Cc: Matthew Seaman Subject: Re: Seeking a solid startup script for node.js/forever Message-ID: <20160322184339.GA64556@ns.umpquanet.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Mar 2016 18:43:46 -0000 Previously, Matthew Seaman wrote: > > Message: 10 > Date: Fri, 18 Mar 2016 23:52:25 +0000 > From: Matthew Seaman > 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