CentOS 7이 너무 빨리 시작되고 cron 스크립트를 실행할 때 네트워크가 준비되지 않았습니다.

CentOS 7이 너무 빨리 시작되고 cron 스크립트를 실행할 때 네트워크가 준비되지 않았습니다.

systemd방금 CentOS 6.5에서 7.0으로 업그레이드했는데 새 버전이 나에게 문제를 일으킬 수 있기 때문에 별로 만족스럽지 않습니다 . 이는 너무 빨리 시작되고, 프로세스를 비동기적으로 시작하고, 서비스 종속성을 엉망으로 만드는 것처럼 보입니다.

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. 온라인에서 읽은 이론일 뿐입니다.

답변1

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

나는 이 안내서를 읽었고,네트워크 연결 후 서비스 실행. 가이드의 작은 인용문:

이렇게 하면 시작을 진행하기 전에 구성된 모든 네트워크 장치가 시작되고 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하는데 ,systemdsystemctl 활성화는 systemctl start와 다릅니다.:

[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

마지막으로 Apache 서비스가 시작되도록 설정하십시오.뒤쪽에MySQL:

[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

누구든지 이 작업을 수행하는 더 좋은 방법을 볼 수 있다면 공유해 주세요.

관련 정보