写个centos自动关机脚本
时间: 2023-02-09 09:12:59 浏览: 79
Sure, here's a simple shell script in CentOS that will automatically shut down the system after a specified amount of time:
```
#!/bin/bash
# Set the amount of time to wait before shutting down
time_to_wait=60
# Display a message to the user
echo "System will shut down in $time_to_wait seconds."
# Wait for the specified amount of time
sleep $time_to_wait
# Shut down the system
shutdown -h now
```
Save this script as a file with a `.sh` extension, make it executable with `chmod +x filename.sh`, then run it with `./filename.sh`. The system will shut down after 60 seconds. You can adjust the `time_to_wait` variable to set a different amount of time.
相关问题
写一个centos 开局的脚本
好的,下面是一个简单的 CentOS 开局脚本:
```bash
#!/bin/bash
# 更新系统软件包
sudo yum update -y
# 安装常用软件
sudo yum install wget curl vim git -y
# 配置 SSH 访问
sudo systemctl enable sshd
sudo systemctl start sshd
# 配置防火墙
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
# 设置时区为北京时间
sudo timedatectl set-timezone Asia/Shanghai
# 输出系统信息
echo "CentOS 系统初始化完成!"
echo "当前日期时间:$(date)"
echo "当前登录用户:$(whoami)"
echo "系统版本号:$(cat /etc/redhat-release)"
```
这个脚本会更新系统软件包、安装常用软件、配置 SSH 访问、配置防火墙、设置时区为北京时间,并输出系统信息。
使用命令 `chmod +x filename.sh` 赋予脚本执行权限,然后执行 `./filename.sh` 即可运行脚本。
写一个centos7 自动安装docker的脚本
### 回答1:
#!/bin/bash# 在Centos7上安装Docker
# 更新yum缓存
yum update -y# 安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2# 设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo# 安装docker
yum install -y docker-ce# 启动docker服务
systemctl start docker# 设置docker开机启动
systemctl enable docker
### 回答2:
Sure! 下面是一个用Shell脚本编写的CentOS 7自动安装Docker的脚本。
```bash
#!/bin/bash
# 检查是否已经安装了Docker
if ! [ -x "$(command -v docker)" ]; then
echo "开始安装Docker..."
else
echo "Docker已经安装。"
exit 0
fi
# 安装必要的依赖包
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# 设置Docker的yum源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装Docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
# 启动并设置Docker为开机自启
sudo systemctl start docker
sudo systemctl enable docker
# 添加当前用户到docker用户组,以便无需sudo即可执行docker命令
sudo usermod -aG docker $USER
# 完成安装
echo "Docker安装完成!请重新登录以使用户组更改生效。"
```
将以上脚本保存为一个文件(比如`install_docker.sh`),然后通过以下命令运行脚本:
```bash
$ chmod +x install_docker.sh
$ ./install_docker.sh
```
脚本会检查是否已经安装了Docker,如果已经安装则退出脚本,否则将自动安装Docker并完成相关设置。最后,脚本会提示您重新登录以使用户组更改生效。
注意:请在具备管理员权限的帐户下运行该脚本。
### 回答3:
以下是一个CentOS 7自动安装Docker的脚本示例:
```bash
#!/bin/bash
# 检查是否以root用户身份执行
if [[ $EUID -ne 0 ]]; then
echo "请以root用户身份执行此脚本。"
exit 1
fi
# 安装需要的依赖软件包
yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加Docker源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 更新yum缓存
yum makecache fast
# 安装Docker CE
yum install -y docker-ce
# 启动Docker服务
systemctl start docker
# 设置Docker开机自启动
systemctl enable docker
# 验证Docker安装是否成功
docker run hello-world
# 输出Docker版本信息
docker version
echo "Docker安装完成。"