[Linux] 起動時にコマンドやプログラムを自動実行させる方法

CentOSの起動時にコマンドやプログラムを自動実行させる方法を調べたのでメモ。

OS起動時にユーザが独自でプログラムを実行させたい場合には、 /etc/rc.d/rc.local ファイルに起動コマンドを書きます。

/etc/rc.d/rc.local ファイルは、起動プロセスの最後に実行されるシェルスクリプトです。

僕は、起動時に export で認証付きプロキシを通すのと、ntpdate で時刻同期させたかったので以下のようにしました。

■ /etc/rc.d/rc.local(編集前)

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
 
touch /var/lock/subsys/local

■ /etc/rc.d/rc.local(編集後)

#!/bin/sh
 
touch /var/lock/subsys/local
 
# プロキシ認証
export http_proxy=http://[ユーザ名]:[パスワード]@[IPアドレス]:[ポート番号]/
# 時刻同期
ntpdate [IPアドレス]

以上です。