cron 스크립트를 실행할 때 CentOS 7이 너무 빨리 부팅되고 네트워크가 준비되지 않았습니다 방금 CentOS 6.5에서

방금 CentOS 6.5에서 7.0으로 업그레이드했으며 새로운 기능 systemd으로 인해 문제가 발생할 수 있으므로 너무 행복하지 않습니다 . 단순히 너무 빨리 부팅하여 프로세스를 비동기식으로 시작하고 서비스 종속성을 강화하는 것 같습니다.

예를 들어 crond재부팅 후 트리거되는 몇 가지 스크립트 설정 이 있습니다.

@reboot    /root/scripts/check_gmail.sh
@reboot    /root/scripts/start_gps_listener.sh

이로 인해 모든 종류의 이상한 오류가 발생합니다 (그중 하나만 표시).

Warning: stream_socket_client(): unable to connect to tcp://192.168.20.4:4001
  (Network is unreachable) in /root/scripts/check_gmail.php on line 137
  ERROR: Network is unreachable (101)

위의 TCP 소켓에 쓰고 있습니다. crond네트워크가로 제대로 초기화되기 전에 시작된 것이 분명 합니다 network is unreachable.

Apache와 MySQL (MariaDB)도 마찬가지입니다. MySQL은 시작 속도가 매우 느립니다 (많은 데이터) crond. 스크립트가 호출 될 때 MySQL 데이터베이스가 실행되지 않기 때문에 Apache와 많은 시작 스크립트가 모두 실패합니다.

나는 의존성을 설정하려고 노력했지만 운이 없다. (와 같이)에 추가 network하고 mysql서비스했습니다 . 이상적으로 모든 서비스는 MySQL이 시작되어 실행될 때까지 기다립니다.[Unit]systemctl list-dependencies

vi /lib/systemd/system/httpd.service
  [Unit]
  Description=The Apache HTTP Server
  After=network.target remote-fs.target nss-lookup.target network.service mysql.service

vi /lib/systemd/system/crond.service
  [Unit]
  Description=Command Scheduler
  After=syslog.target auditd.service systemd-user-sessions.service time-sync.target network.service mysql.service

위와 같이 부팅하면 동일한 오류가 발생합니다. mailqcron 스크립트를 처리 할 때 네트워크 / DNS가 준비되지 않았기 때문에 이메일도 수신됩니다 . 시작한지 몇 분 후에 올바르게 전송됩니다.

누구나 서비스가 올바른 순서로 실행되도록하여 올바르게 처리 할 수 ​​있습니까? 부팅 속도가 너무 빠르며 이상적으로는 한 가지 서비스를 시작하는 중입니다 … 기다립니다 … 새로운 서비스를 시작합니다 … 기다리십시오.

그것이 systemd내 문제 인지 확실하지 않다는 것을 명심하십시오 -그것은 내가 그물에서 읽을 수있는 것에 대한 나의 이론 일뿐입니다.



답변

더 많이 읽은 후에 나에게 맞는 솔루션을 찾았습니다.

이 안내서 인 네트워크 작동 후 서비스 실행을 읽었 습니다 . 가이드의 작은 인용문 :

이렇게하면 구성된 모든 네트워크 장치가 작동하고 부팅을 계속하기 전에 IP 주소가 할당됩니다.

이것은 내가 원하는 것이므로이 서비스를 활성화하고 서비스 파일에서 다음에 대한 종속성 규칙을 설정했습니다 crond.

[root@srv]# systemctl enable NetworkManager-wait-online

[root@srv]# vi /lib/systemd/system/crond.service
  Requires=network.target
  After=syslog.target auditd.service systemd-user-sessions.service time-sync.target network.target mysqld.service

mysqld여전히 이전을 기반으로 init.d만드는 데 필요한 내가 systemd여기 제안 서비스를 systemctl의 시작부터 다릅니다 수 systemctl :

[root@srv]# vi /lib/systemd/system/mysqld.service
  [Unit]
  Description=MySQL Server
  After=network.target
  [Service]
  Type=forking
  ExecStart=/etc/rc.d/init.d/mysql start
  ExecStop=/etc/rc.d/init.d/mysql stop
  [Install]
  WantedBy=multi-user.target

[root@srv]# systemctl daemon-reload
[root@srv]# chkconfig mysql off
[root@srv]# systemctl enable mysqld

마지막으로 MySQL 이후에 Apache 서비스를 시작하도록 설정하십시오 .

[root@srv]# vi /lib/systemd/system/httpd.service
  Requires=mysqld.service
  After=network.target remote-fs.target nss-lookup.target mysqld.service

이것은 적어도 나를 위해 작동합니다.

나는이 명령을 사용하여 적어도 MySQL과 Apache 전에 네트워크가 시작되었음을 분명히 알 수있는 곳에서 나중에 그것을 확인했습니다. 나는 crond아무데도 볼 수 없지만 내 스크립트에서 작동하는 것을 볼 수 있습니다.

[root@srv]# systemd-analyze critical-chain
  multi-user.target @10.510s
    + httpd.service @10.344s +165ms
      + mysqld.service @9.277s +1.065s
        + network.target @9.273s
          + network.service @8.917s +355ms
            + iptables.service @444ms +157ms
              + basic.target @443ms
                [CUT]

내가 사용한 다른 유용한 명령은 다음과 같습니다.

# See exactly what takes how long (who to blame for the delay)
[root@srv]# systemd-analyze blame

# Check available names that can be used in the service files
[root@srv]# systemctl list-unit-files

이 중 하나를 수행하는 더 좋은 방법이 있다면 공유하십시오.


답변