Shammer's Philosophy

My private adversaria

自動起動スクリプトの作成

Lisp UDP Server 改 - Shammerismで作成した簡易UDPサーバを自動起動させたいと思う。その手順を整理する。大枠としては以下の作業になる。

  1. lisp ファイルをコンパイルしてバイナリにする
  2. /etc/init.d/skeleton をコピーして編集

lisp ファイルをコンパイルしてバイナリにする

これは楽勝。先日書いたときは、ccl64 -l $FILE というように実行すればそのまま動作するようになっていたが、本体部分を一つの関数にして save-application してやればできるはず。新しいファイルはこんな感じになる。

(defun start ()
  (let ((server (make-socket :type :datagram
			     :local-host "0.0.0.0"
			     :local-port 7001
			     :format :binary)))
    (with-open-file (f "/var/log/clozure-udp-server.log" :direction :output :if-exists :append :if-does-not-exist :create)
		    (do () ()
			(multiple-value-bind (buffer length client-ip client-port)
					     (receive-from server 512)
					     (when (equalp buffer #(81 85 73 84 10))
					       (return))
					     (format f "[~A:~A] Received byte length is ~A, message is ~A~%"
						     (ipaddr-to-dotted client-ip)
						     client-port
						     length
						     (decode-string-from-octets buffer :external-format :UTF-8))
					     (force-output f))))
    (format t "FINISH!~%")))
(save-application "ccludp" :toplevel-function #'start :prepend-kernel t)

この例では、ccludp という名前で保存される。これを/usr/sbinに保存。他の場所に保存する場合は、skeleton の中身のDAEMON=/usr/sbin/$NAMEの行も編集する。ここを編集するのではなく、/usr/sbinに実行ファイルを置く方がいいと思うが。

/etc/init.d/skeleton をコピーして編集

コピーしたファイルの名前は ccludp にした。編集箇所は少しでよかった。以下は skeleton と新しいファイルの diff の結果。

# diff skeleton ccludp
3c3
< # Provides:          skeleton
---
> # Provides:          ccludp
8,10c8,9
< # Short-Description: Example initscript
< # Description:       This file should be used to construct scripts to be
< #                    placed in /etc/init.d.
---
> # Short-Description: Clozure Common Lisp UDP Echo Server
> # Description:       Clozure Common Lisp UDP Echo Server
13,16c12
< # Author: Foo Bar <foobar@baz.org>
< #
< # Please remove the "Author" lines above and replace them
< # with your own name if you copy and modify this script.
---
> # Author: My name <MyName@example.com>
22,23c18,19
< DESC="Description of the service"
< NAME=daemonexecutablename
---
> DESC="Lisp UDP Server"
> NAME=ccludp

コメント行を除けばさらに少ない。

22,23c18,19
< DESC="Description of the service"
< NAME=daemonexecutablename
---
> DESC="Lisp UDP Server"
> NAME=ccludp