よく使うLinuxコマンド

update-rc.d - サーバー運営上必要なコマンド

ランレベルに合わせて自動起動及び停止させる設定を行う
(squeeze以降の使用は推奨されていない。) →現在はinsservコマンドを推奨

■よく使う形(例)

※apacheをランレベルに合わせて自動起動及び停止させる設定を行う。

# update-rc.d apache defaults

Adding system startup for /etc/init.d/apache ...
/etc/rc0.d/K20apache -> ../init.d/apache
/etc/rc1.d/K20apache -> ../init.d/apache
/etc/rc6.d/K20apache -> ../init.d/apache
/etc/rc2.d/S20apache -> ../init.d/apache
/etc/rc3.d/S20apache -> ../init.d/apache
/etc/rc4.d/S20apache -> ../init.d/apache
/etc/rc5.d/S20apache -> ../init.d/apache

※apacheの自動起動及び停止を少し細かく設定をする。

# update-rc.d apache defaults 19 21

Adding system startup for /etc/init.d/apache ...
/etc/rc0.d/K21apache -> ../init.d/apache
/etc/rc1.d/K21apache -> ../init.d/apache
/etc/rc6.d/K21apache -> ../init.d/apache
/etc/rc2.d/S19apache -> ../init.d/apache
/etc/rc3.d/S19apache -> ../init.d/apache
/etc/rc4.d/S19apache -> ../init.d/apache
/etc/rc5.d/S19apache -> ../init.d/apache

これはコマンドと言うよりツールと言ったほうが良さそうですが、rc.dディレクトリに起動スクリプトのシンボリックリンクを張ってくれます。ランレベルに合わせて、まとめてシンボリックリンクを張ってくれるので、とても便利。

■主なオプション

オプション 機能説明
-f 強制的にシンボリックリンクを削除する。
-n 動作内容を表示(確認)する。実際には何も実行されない。
アプリケーションをパッケージでインストールした場合には特に必要はありませんが、ソースからコンパイルしたサーバーサービスの場合、システムの起動時に自動起動するように設定しなくてはなりません。

サーバーのサービスの起動には、/etc/init.d/下に配置した起動スプリクトを実行すればよいわけですが、それを自動化するには、ランレベルにに合った起動スプリクトをそれぞれのディレクトリにシンボリックリンクを張ればOKです。
update-rc.dというコマンドは、それを簡単に設定してくれます。

debianの場合のランレベルは下記の通りです。

init0 = 停止
init1 = シングルユーザーモード
init2 = マルチユーザーモード
init3 = init2と同じ
init4 = init2と同じ
init5 = init2と同じ
init6 = 再起動

Debianでは、起動時のデフォルトランレベルは、rc2.dになります。
rc.d内のスクリプトは、先頭が"S"で始まる物が起動、"K"で始まる物が停止で、"S"や"K"の後に続く数字順に実行されます。

例えば、rc2.d/下はこんな感じです。

lrwxrwxrwx 1 root root 18 Mar 25 2007 S10sysklogd -> ../init.d/sysklogd
lrwxrwxrwx 1 root root 15 Mar 25 2007 S11klogd -> ../init.d/klogd
lrwxrwxrwx 1 root root 16 Mar 25 2007 S20apache -> ../init.d/apache
lrwxrwxrwx 1 root root 15 Mar 25 2007 S20inetd -> ../init.d/inetd
lrwxrwxrwx 1 root root 20 Mar 25 2007 S20postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 13 Mar 24 2007 S20ssh -> ../init.d/ssh
lrwxrwxrwx 1 root root 17 Nov 27 2007 S50proftpd -> ../init.d/proftpd
lrwxrwxrwx 1 root root 13 Mar 25 2007 S89atd -> ../init.d/atd
lrwxrwxrwx 1 root root 14 Mar 25 2007 S89cron -> ../init.d/cron

それを簡単に登録・削除できるツールがupdate-rc.dコマンドです。

# update-rc.d apache defaults

Adding system startup for /etc/init.d/apache ...

/etc/rc0.d/K20apache -> ../init.d/apache
/etc/rc1.d/K20apache -> ../init.d/apache
/etc/rc6.d/K20apache -> ../init.d/apache
/etc/rc2.d/S20apache -> ../init.d/apache
/etc/rc3.d/S20apache -> ../init.d/apache
/etc/rc4.d/S20apache -> ../init.d/apache
/etc/rc5.d/S20apache -> ../init.d/apache
/etc/rc6.d/K20apache -> ../init.d/apache

defaultsを設定した場合は、20番でシンボリックリンクが作成されます。

# update-rc.d -f inetd remove

Removing any system startup links for /etc/init.d/inetd ...

/etc/rc0.d/K20inetd
/etc/rc1.d/K20inetd
/etc/rc2.d/S20inetd
/etc/rc3.d/S20inetd
/etc/rc4.d/S20inetd
/etc/rc5.d/S20inetd
/etc/rc6.d/K20inetd

不必要な起動スプリクトの削除します。
パッケージでインストールした場合は、自動的に設定されているので、サービスを使用しない場合は、パッケージのアンインストールか起動スプリクトの削除が必要です。

# update-rc.d inetd start 19 2 3 4 5 . stop 21 0 1 6 .

Removing any system startup links for /etc/init.d/inetd ...

/etc/rc0.d/K21inetd
/etc/rc1.d/K21inetd
/etc/rc2.d/S19inetd
/etc/rc3.d/S19inetd
/etc/rc4.d/S19inetd
/etc/rc5.d/S19inetd
/etc/rc6.d/K21inetd

かなり細かく起動(停止)順序を設定します。
上記の例は各ランレベルに合わせて個別に設定していますが、通常の使用では「update-rc.d inetd defaults 19 21」の方で十分です。^^

もし、現在のサービスが何が動いているか不安であれば、[lsof -i]コマンド[netstat -an]コマンドで、現在LISTENしているサービス(ポート)を調べましょう。