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

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

しばらく前にこれに関する内容を読んだようですが、どうなったのか覚えていません。デフォルトでは、/etc/init.d起動時に自動的に起動したいサービスがあります。スクリプトをディレクトリにシンボリックリンクすることに関連していたと思います/etc/rc.dが、今は覚えていません。このコマンドは何ですか?

私はFedora / CentOS派生製品を使用していると思います。

ベストアンサー1

前述のようにRed Hatベースのシステムを使用している場合は、次のことができます。

  1. スクリプトを生成してここに入れます/etc/init.d(例/etc/init.d/myscript:)。スクリプトの形式は次のとおりです。
#!/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 &
}

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

exit 0 

フォーマットは非常に標準的なので、で使用できます。その後、次/etc/init.dのスクリプトを使用できます。マニュアルページでは、スクリプトのタイトルについて説明します。/etc/init.d/myscript startchkconfig myscript startckconfig

 > This says that the script should be started in levels 2,  3,  4, and
 > 5, that its start priority should be 20, and that its stop priority
 > should be 80.

次に定義するヘルパー関数の開始、停止、およびステータスコードの使用例/etc/init.d/functions

  1. スクリプトの有効化

    $ chkconfig --add myscript 
    $ chkconfig --level 2345 myscript on 
    
  2. スクリプトが実際に有効になっていることを確認してください。 「on」を選択したレベルが表示されます。

    $ chkconfig --list | grep myscript
    

おすすめ記事