/etc/init.dにスクリプトを生成するにはどうすればよいですか?

/etc/init.dにスクリプトを生成するにはどうすればよいですか?

私のnodejsアプリケーションをLinuxサービスにしようとしています。 stackexchangeで以下のリンクを見つけて、/etc/init.dフォルダの下にスクリプトを作成しました。

起動時に/etc/init.dのスクリプトを起動するにはどうすればよいですか?

#!/bin/bash
# chkconfig: 2345 20 80
# description: Description comes here....

# Source function library.
. /etc/init.d/functions

start() {
    # code to start app comes here 
    # example: daemon program_name &
    /usr/bin/node /home/myapp/index.js
}

stop() {
    # code to stop app comes here 
    # example: killproc program_name
}

case "$1" in 
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       # code to check status of app comes here 
       # example: status program_name
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

スクリプトを実行しようとすると、このエラーが発生します。

$ service myapp start
/etc/init.d/myapp: line 17: syntax error near unexpected token `}'
/etc/init.d/myapp: line 17: `}'

nodejs アプリケーションを手動で正常に実行できます。私のサービスに問題があります。 systemctlが利用できないので、お勧めしません。

私はこのnodejsアプリケーションをhttpd、ftpdのように制御できるLinuxサービスにしたいと思います。

ベストアンサー1

stop() 関数に内容がありません。何かを追加すると、実行している場合でも/bin/true(または多分?)killproc /usr/bin/nodeこのエラーを正常に克服できます。

例:

$ a() {
> echo foo
> }
$ b() {
> # comment
> }
-bash: syntax error near unexpected token `}'
$ b() {
> /bin/true
> }
$ 

おすすめ記事