在 CentOS 上,您可以使用 systemd 服務來實現 Apache 的自動重啟。以下是創建和啟用 Apache 自動重啟的步驟:
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl status httpd
現在,Apache 服務將在系統啟動時自動運行,并在出現故障時自動重啟。
如果您希望在特定時間間隔后自動重啟 Apache,可以創建一個 cron 任務來實現。以下是如何創建一個每天凌晨 1 點重啟 Apache 的 cron 任務的示例:
打開終端。
輸入以下命令以編輯 root 用戶的 crontab 文件:
sudo crontab -e
0 1 * * * /usr/sbin/systemctl restart httpd
這將在每天凌晨 1 點執行 systemctl restart httpd
命令,從而重啟 Apache 服務。
現在,Apache 服務將按照您設置的時間間隔自動重啟。