4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

備忘録 PostgreSQL/Tomcat/Apacheを順番に自動起動

Last updated at Posted at 2017-04-18

CentOS 7を起動してから、各ミドルウェアの起動をわざわざ手で実行したくなかったので、自動起動するための設定を試みる。

起動したい順番は、PostgreSQL ⇒ Tomcat ⇒ Apache

PostgreSQLとApacheは変更したいところがあるため、/usr/lib/systemd/systemにあるファイルを、/etc/systemd/systemにコピーして、サービスファイルを作成する。
Tomcatについては自分で作成。

サービスファイルの作成

  • PostgreSQLのサービスファイル
postgresql.service
[Unit]
Description=PostgreSQL 9.6.2
After=syslog.target network.target
[Service]
User=postgres
Group=postgres
Type=notify
Environment=PGDATA=/psql/data/
OOMScoreAdjust=1000
ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir /psql/data/
ExecStart=/usr/pgsql-9.6/bin/postmaster -D /psql/data/
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=
TimeoutSec=300
[Install]
WantedBy=multi-user.target
  • Tomcatのサービスファイル
tomcat.service
[Unit]
Description=apache-tomcat-8.5.12
After=network.target remote-fs.target nss-lookup.target **postgresql.service** ※PostgreSQLの後に起動させるため
[Service]
User=tomcat
Group=tomcat
Type=oneshot
ExecStart=/usr/local/apache-tomcat-8.5.12/bin/startup.sh
ExecStop=/usr/local/apache-tomcat-8.5.12/bin/shutdown.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
  • Apacheのサービスファイル
httpd.service
[Unit]
Description=Apache 2.4.6
After=remote-fs.target nss-lookup.target tomcat.service
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecStop=/bin/kill -WINCH ${MAINPID}
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
KillSignal=SIGCONT
PrivateTemp=true
[Install]
WantedBy=multi-user.target

再起動後にログを確認

journalctl -lコマンドで実行結果を確認

Apr 18 16:20:54 test01 kernel: Netfilter messages via NETLINK v0.30.
Apr 18 16:20:54 test01 kernel: ip_set: protocol 6
Apr 18 16:20:54 test01 httpd[818]: (99)Cannot assign requested address: AH00072: make_sock: could not bind to address 192.168.100.100:10080
Apr 18 16:20:54 test01 httpd[818]: no listening sockets available, shutting down
Apr 18 16:20:54 test01 httpd[818]: AH00015: Unable to open logs
Apr 18 16:20:54 test01 systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
Apr 18 16:20:54 test01 kill[894]: kill: cannot find process ""
Apr 18 16:20:54 test01 systemd[1]: httpd.service: control process exited, code=exited status=1
Apr 18 16:20:54 test01 systemd[1]: Failed to start Apache 2.4.6.
Apr 18 16:20:54 test01 systemd[1]: Unit httpd.service entered failed state.
Apr 18 16:20:54 test01 systemd[1]: httpd.service failed.
Apr 18 16:20:54 test01 network[658]: Bringing up loopback interface:  [  OK  ] 
~(省略)~
Apr 18 16:20:56 sistest01.sis.tvc.com avahi-daemon[578]: Registering new address record for fe80::444:57ff:fefd:26e on eth0.*.
Apr 18 16:20:59 sistest01.sis.tvc.com avahi-daemon[578]: Joining mDNS multicast group on interface eth0.IPv4 with address 10.133.207.23.
Apr 18 16:20:59 sistest01.sis.tvc.com avahi-daemon[578]: New relevant interface eth0.IPv4 for mDNS.
Apr 18 16:20:59 sistest01.sis.tvc.com avahi-daemon[578]: Registering new address record for 10.133.207.23 on eth0.IPv4.
Apr 18 16:21:01 sistest01.sis.tvc.com network[658]: Bringing up interface eth0:  [  OK  ]
Apr 18 16:21:01 sistest01.sis.tvc.com network[658]: Bringing up interface eth0.old:  RTNETLINK answers: File exists```
no listening sockets available

ということで、どうもIPアドレスが設定されるまえに、Apacheが起動されたようだ・・・・

紆余曲折の結果。。。

Apacheの起動を見直した。

httpd.service
[Unit]
Description=Apache 2.4.6
After=remote-fs.target nss-lookup.target tomcat.service
Requires=network.target
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecStop=/bin/kill -WINCH ${MAINPID}
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
KillSignal=SIGCONT
PrivateTemp=true
[Install]
WantedBy=multi-user.target

RedHatのシステムユニットファイルの作成および変更と、

  • Requires
    他のユニットに依存関係を設定します。
    Requires に一覧表示されるユニットは、該当ユニットと共にアクティブ化されます。
    必要なユニットのいずれかが開始しない場合、ユニットはアクティブ化されません。

とかかれていた。ということは、network系の必要なユニットが開始されるまではApacheを起動しなければいいのかと思い、上記の設定に変更。
(Afterからnetwork.taregetを省き、Requires=network.targetを追加)

再起動後の確認は??

journalctl -lコマンドで実行結果を確認

Apr 18 18:06:30 test01 systemd[1]: Starting Network.
Apr 18 18:06:30 test01 systemd[1]: Starting OpenSSH server daemon...
Apr 18 18:06:30 test01 systemd[1]: Starting Dynamic System Tuning Daemon...
Apr 18 18:06:30 test01 systemd[1]: Starting PostgreSQL 9.6.2...
Apr 18 18:06:30 test01 systemd[1]: Starting Postfix Mail Transport Agent...
Apr 18 18:06:30 test01 systemd[1]: Starting System Logging Service...
Apr 18 18:06:30 test01 systemd[1]: Reached target Network is Online.
Apr 18 18:06:30 test01 systemd[1]: Starting Network is Online.
Apr 18 18:06:30 test01 systemd[1]: Starting Crash recovery kernel arming...
Apr 18 18:06:30 test01 systemd[1]: Starting LSB: XenServer Virtual Machine daemon providing host integration services...
Apr 18 18:06:30 test01 systemd[1]: Started System Logging Service.
Apr 18 18:06:30 test01 systemd[1]: PID file /var/run/sshd.pid not readable (yet?) after start.
Apr 18 18:06:30 test01 xe-linux-distribution[1291]: Mounting xenfs on /proc/xen: [  OK  ]
Apr 18 18:06:30 test01 sshd[1308]: Server listening on 10.133.207.23 port 22.
Apr 18 18:06:30 test01 systemd[1]: Started OpenSSH server daemon.
Apr 18 18:06:31 test01 xe-linux-distribution[1291]: Detecting Linux distribution version: [  OK  ]
Apr 18 18:06:31 test01 xe-linux-distribution[1291]: Starting xe daemon:  [  OK  ]
Apr 18 18:06:31 test01 systemd[1]: Started LSB: XenServer Virtual Machine daemon providing host integration services.
Apr 18 18:06:31 test01 systemd[1]: Starting SYSV: Advanced Policy Firewall...
Apr 18 18:06:31 test01 postmaster[1301]: < 2017-04-18 18:06:31.077 JST > LOG:  redirecting log output to logging collector process
Apr 18 18:06:31 test01 postmaster[1301]: < 2017-04-18 18:06:31.077 JST > HINT:  Future log output will appear in directory "/prdlog/psql".
Apr 18 18:06:31 test01 systemd[1]: Started PostgreSQL 9.6.2.
Apr 18 18:06:31 test01 systemd[1]: Starting apache-tomcat-8.5.12...
Apr 18 18:06:31 test01 startup.sh[1367]: Tomcat started.
Apr 18 18:06:31 test01 systemd[1]: Started apache-tomcat-8.5.12.
Apr 18 18:06:31 test01 systemd[1]: Starting Apache 2.4.6...
Apr 18 18:06:31 test01 systemd[1]: Started Apache 2.4.6.
Apr 18 18:06:31 test01 systemd[1]: Started Dynamic System Tuning Daemon.

うむ。ちゃんと、PostgreSQL、Tomcatの次にApacheが起動してきたぞ!
システムユニットのUnitオプションはもう少し調べたほうがよさそうだ。

4
6
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
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?