Page 1 of 1

Keep running (don't exit after last line of code executes)

Posted: Mon May 02, 2011 5:27 am
by wvxvw
Hi. I'm trying to make a script that launches the server. I need it to launch within an Ant task (Ant is sort of non-problem for me, but Lisp is :)) to run it along with my unit tests. What I fail to find, is how would I tell a Lisp program to not exit (when I say script I mean specifically SBCL invoked with --script argument). Ideally, I'd be happy to find some way to:
1. launch the script (or fasl) in a background process (think server.fasl -start & #server keeps running, but the script exits)
2. same script (whit another parameter), or another script that tells the server to stop.
I tried to find a simple setup for Hunchentoot or Webdav (this would probably fit well), but didn't find anything simple / good enough to even explain the process.

Think something like this:

Code: Select all

<target ...>
    <exec name="shell-or-lisp-script" spawn="if-you-need?">
        <arg value="start-server"/>
    </exec>
    <junit .../><!-- a bunch of unit tests that talk to the server -->
    <exec name="shell-or-lisp-script" spawn="if-you-need?">
        <arg value="stop-server"/>
    </exec>
</target>
Thanks!

Re: Keep running (don't exit after last line of code executes)

Posted: Mon May 02, 2011 5:39 am
by ramarren
I have no experience with doing this, but you can use something like detachtty or screen to daemonize the SBCL process.

Re: Keep running (don't exit after last line of code executes)

Posted: Mon May 02, 2011 6:37 am
by wvxvw
Thank you, this looks interesting (esp. detachtty)!
For now this is what I did (just in case anyone needs something quick to get things working in the time being).
start-server.lisp

Code: Select all

;; This is some old-ish asdf function, I think it's called differently now
(asdf:oos 'asdf:load-op :video-rounds)
(rounds-web:start-server)
;; To keep us running. This looks like an ugly hack, 
;; but that's all I could find for now. 
(defvar alive t)
(loop (sleep 1000) (if (not alive) (quit)))
and then I have:
start

Code: Select all

#!/bin/bash
nohup sbcl --script start-server.lisp > /dev/null 2> /dev/null &
and
stop

Code: Select all

#!/bin/bash
PID=`ps aux | awk '/sbcl --script start-server.lisp$/ { print $2 }'`;
kill "$PID"
and then somewhere in my Ant script:

Code: Select all

	<target name="start-hunchentoot"
		description="This is a test target. Don't use it yet. 
It's for launching web server for tests.">
		<exec executable="${SERVER_DIR}/start" dir="${SERVER_DIR}"/>
		<!--exec executable="${SERVER_DIR}/stop" dir="${SERVER_DIR}"/-->
	</target>
This isn't a production or long time support code. It's only for running unit tests, so I'm fine with it as it is, but, of course I'll try to find a cleaner way to do it.

Re: Keep running (don't exit after last line of code executes)

Posted: Mon May 02, 2011 4:17 pm
by JamesF
I have an /etc/init.d/script that wraps around this and takes care of startup/shutdown at system boot/shutdown, but the code I use to kick off a hunchentoot server looks like this:

Code: Select all

#!/bin/bash

# Starts up SBCL in a detached `screen` session, allowing you to
# reattach at will, so long as you log in as the same user as the one
# who owns this process.

# Might just need tweaking for other lisps. You never know...

# Notes:
# `screen -d -m` starts screen in "detached" mode

SCREEN=/usr/bin/screen

PACKAGE=$1
COMMAND=$2

# Limit the memory usage
# This helps prevent sbcl from taking over the entire machine.
MEMLIMIT=256

if [[ -z "$PACKAGE" || -z "$COMMAND" ]]
then
	echo "Need the package and at least one command as arguments!"
	exit 1
else
	$SCREEN -d -m sbcl --dynamic-space-size $MEMLIMIT --eval "(asdf:load-system $PACKAGE)" --eval "(in-package $PACKAGE)" --eval "($COMMAND)"
fi
This is used to load a package that handles running multiple webapps within a single hunchentoot instance, similar in spirit to Cyrus Harmon's hunchentoot-vhost package. It loads those systems, sets up the URL handlers, then starts Hunchentoot. I've been meaning to publish that for a while now, but it really needs porting to hunchentoot-1.1.1 first.

Re: Keep running (don't exit after last line of code executes)

Posted: Mon May 02, 2011 10:16 pm
by wvxvw
Thanks. Will have to read on /usr/bin/screen more. Didn't have to use it before / didn't even know such things exist.