LoginSignup
53
48

More than 5 years have passed since last update.

systemdでOS起動時に任意のプログラムを実行する

Last updated at Posted at 2016-01-30

やりたかったこと:CentOS7起動時にChefを一回だけ実行したい

CentOS7からサービス管理がinitiscriptsからsystemdになっています。
initscriptsでは/etc/rc.d/rc.localにコマンドを書いておけばOSの起動時に実行してくれますが、systemdでは違うようなので(今更ですが)調べました。

CentOS7にも一応/etc/rc.d/rc.localもあるのですが、中身を見てみると以下のようになっています。

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

ということで、自分でsystemdのサービス作るかudevのルールを書くのが超おすすめみたいです。

書いた。
/etc/systemd/system/chef-client.serviceを配置してsystemctl daemon-reloadすればサービスに登録されます。

/etc/systemd/system/chef-client.service
[Unit]
Description=run Chef Client during boot
After=network.target

[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/bin/chef-client -z

[Install]
WantedBy=multi-user.target

ポイントとしてはUnitセクションのAfterで先に起動しておいてほしいサービスを定義してます。
また、Typeで起動プロセスの種類を書いておきます。Type=simpleでは、コマンドを実行したタイミングで起動完了となります。oneshotだと、指定したコマンドが完了したタイミングで起動完了・サービス終了と判断します。
コマンドが完了したあともサービスとしては起動状態として認識させたい場合にRemainAfterExit=yesを指定します。

.serviceファイルの詳細はman pageでどうぞ。
http://www.freedesktop.org/software/systemd/man/systemd.service.html

起動設定、動作確認

[root@centos7-01 chef]# systemctl daemon-reload
[root@centos7-01 chef]# systemctl enable chef-client
[root@centos7-01 chef]# systemctl stop chef-client
[root@centos7-01 chef]# systemctl start chef-client
[root@centos7-01 chef]# systemctl status chef-client
● chef-client.service - Chef Client
   Loaded: loaded (/etc/systemd/system/chef-client.service; disabled; vendor preset: disabled)
   Active: active (exited) since Fri 2016-01-29 19:05:52 EST; 7s ago
  Process: 6397 ExecStart=/bin/chef-client -z (code=exited, status=0/SUCCESS)
 Main PID: 6397 (code=exited, status=0/SUCCESS)

Jan 29 19:05:51 centos7-01 chef-client[6397]: [2016-01-29T19:05:51-05:00] INFO: Starting Chef Run for centos7-01
Jan 29 19:05:51 centos7-01 chef-client[6397]: [2016-01-29T19:05:51-05:00] INFO: Running start handlers
Jan 29 19:05:51 centos7-01 chef-client[6397]: [2016-01-29T19:05:51-05:00] INFO: Start handlers complete.
Jan 29 19:05:51 centos7-01 chef-client[6397]: [2016-01-29T19:05:51-05:00] INFO: HTTP Request Returned 404 Not Found: Object not found:
Jan 29 19:05:51 centos7-01 chef-client[6397]: [2016-01-29T19:05:51-05:00] INFO: Loading cookbooks []
Jan 29 19:05:51 centos7-01 chef-client[6397]: [2016-01-29T19:05:51-05:00] WARN: Node centos7-01 has an empty run list.
Jan 29 19:05:51 centos7-01 chef-client[6397]: [2016-01-29T19:05:51-05:00] INFO: Chef Run complete in 0.053521516 seconds
Jan 29 19:05:51 centos7-01 chef-client[6397]: [2016-01-29T19:05:51-05:00] INFO: Running report handlers
Jan 29 19:05:51 centos7-01 chef-client[6397]: [2016-01-29T19:05:51-05:00] INFO: Report handlers complete
Jan 29 19:05:52 centos7-01 systemd[1]: Started Chef Client.

うまく動いているようです!

53
48
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53
48