步骤如下
[root@localhost:~] cat /etc/rc.local.d/local.sh
#!/bin/sh
# local configuration options
# Note: modify at your own risk! If you do/use anything in this
# script that is not part of a stable API (relying on files to be in
# specific places, specific tools, specific output, etc) there is a
# possibility you will end up with a broken system after patching or
# upgrading. Changes are not supported unless under direction of
# VMware support.
# Note: This script will not be run when UEFI secure boot is enabled.
在exit0之前添加以下内容
/bin/kill $(cat /var/run/crond.pid)
下面的时间是UTC时间,日常使用的是北京时间,需要进行转换
/bin/echo '25 05 * * * /vmfs/volumes/datastore1/autoshutdown.sh' >> /var/spool/cron/crontabs/root
重启cron进程(将加载修改后的root文件)
/usr/lib/vmware/busybox/bin/busybox crond
exit 0
注意:
1、修改完/etc/rc.local.d/local.sh文件后,需要执行auto-backup命令把local.sh文件保存,否则重启后会丢失。
2、重启机器或者手工执行一次local.sh脚本才能生效 (sh /etc/rc.local.d/local.sh
)
关机脚本:
vi /vmfs/volumes/datastore1/autoshutdown.sh
#!/bin/bash
vim-cmd vmsvc/power.off 1 //后面数字指虚拟机id
/sbin/poweroff
相关命令:
//查询所有已配置的虚拟机,可获得VMID
vim-cmd vmsvc/getallvms
//通过VMID查询相应的虚拟机的当前开关机状态
vim-cmd vmsvc/power.getstate VMID
//发送关机信号(命令),但操作系统未必会真正关机
vim-cmd vmsvc/power.shutdown VMID
//直接关机(相当于关电源)
vim-cmd vmsvc/power.power VMID