centos服务器重启脚本命令
时间: 2024-09-14 11:14:22 浏览: 70
CentOS使用expect批量远程执行脚本和命令
在 CentOS 系统中,创建一个用于重启服务器的简单脚本通常会包含 `shutdown` 或 `reboot` 命令。这里是一个基本的重启脚本示例,可以保存到 `/etc/init.d/restart_server` 文件中(假设你需要设置定时重启,可以在 crontab 中运行它):
```bash
#!/bin/bash
# chkconfig: 2345 90 10
# description: Restart Server Script
# Source function library in /etc/rc.d/init.d/functions
. /etc/rc.d/init.d/functions
# Check if root user is running the script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root."
exit 1
fi
# Function to perform the actual reboot
restart_server() {
local message="Restarting server at $(date +'%Y-%m-%d %H:%M:%S')"
log_action_msg $message
shutdown -r now
}
# Run the restart action if called directly or via service
case "$1" in
start|stop|restart)
restart_server
;;
*)
echo "Usage: /sbin/service restart_server {start|stop|restart}"
exit 1
;;
esac
exit 0
```
要启用此服务并使其开机自启,可以运行以下命令:
```bash
chkconfig restart_server on
systemctl enable restart_server.service
```
然后你可以通过 `/sbin/service restart_server restart` 进行手动重启。
阅读全文