我正在尝试定制 apcupsd doshutdown 事件脚本来运行某个 java 程序,该程序最终会使所有虚拟机休眠,并最终使主系统本身休眠。
/etc/apcupsd/doshutdown 在这里:
[root@localhost apcupsd]# cat doshutdown
#!/bin/bash
#
# This shell script is placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when apcupsd
# thinks to shutdown the system
# Instead of shutdown we first hibernate all VMWare VMs and the the system
wall ${USER} "Hibernate of VMs and system begins now."
rm /var/log/apcupsd_doshutdown_error.log
/usr/bin/java -version 2>> /var/log/apcupsd_doshutdown_error.log
/usr/bin/java -Dlogback.configurationFile=/etc/apcupsd/BTool.xml -cp /etc/apcupsd/BTool-assembly-1.0.jar Shutdown hibernate NOFLAG 2>> /var/log/apcupsd_doshutdown_error.log
wall ${USER} "Hibernate done."
# Do not run default shutdown after this!!!
exit 99
当 UPS 被迫使用电池供电时,doshutdown(或 apcupsd)会在墙上打印以下内容
Power failure on UPS localhost.localdomain. Running on batteries.
root Hibernate of VMs and system begins now.
Battery time limit exceeded on UPS localhost.localdomain. Doing shutdown.
root Hibernate done.
apcupsd_doshutdown_error.log 是:
/etc/apcupsd/doshutdown: line 11: /usr/bin/java: Permission denied
/etc/apcupsd/doshutdown: line 12: /usr/bin/java: Permission denied
因此 doshutdown 脚本启动但无法运行 java 程序。
Java 权限包括:
lrwxrwxrwx. 1 root root 22 Sep 25 11:45 /usr/bin/java -> /etc/alternatives/java
lrwxrwxrwx. 1 root root 46 Sep 25 11:45 /etc/alternatives/java -> /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin/java
-rwxr-xr-x. 1 root root 9488 Jan 21 20:25 /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin/java
我可以以普通用户或 root 身份从终端运行 doshutdown,但以 root 身份通过 apcupsd 启动时会失败。
我可以在终端中运行 java,版本是
$ java -version
openjdk version "1.8.0_71"
OpenJDK Runtime Environment (build 1.8.0_71-b15)
OpenJDK 64-Bit Server VM (build 25.71-b15, mixed mode)
发行版:
CentOS release 6.7 (Final)
Linux localhost.localdomain 2.6.32-573.18.1.el6.x86_64 #1 SMP Tue Feb 9 22:46:17 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
权限错误的原因是什么以及如何修复?
编辑:添加了更多 Java 权限、Java 版本和发行版。将 Java -version 添加到 doshutdown 脚本并在日志中添加相关错误。