Operating system: centos 5.2
Scene description:
We have a WEB project that the customer requires to run as a normal user (such as chb), and then there is a button on the page that can be shut down after clicking it.
Implementation method:
1. JAVA code:
Copy the code code as follows:
public static boolean shutDownForLinux() {
try {
Runtime.getRuntime().exec("sudo /sbin/poweroff");
} catch (IOException e) {
return false;
}
return true;
}
2. Modify /etc/sudoers and add the following information at the end:
chb ALL=NOPASSWD:/sbin/reboot,/sbin/poweroff
3. Set to automatically start tomcat at boot, modify /etc/rc.d/rc.local, and add the following at the end:
su chb -c "/opt/tomcat/bin/startup.sh"
Problem description:
For the java process that starts automatically every time the computer is turned on, the shutdown button on the page cannot implement the shutdown function. However, if you log in to the server through ssh with the chb account at this time, manually kill the tomcat process, and then restart tomcat, the shutdown button on the page It works. Do you know why this is? Is it related to sudo loading order? Or is it related to the chb user shell environment?
Problem analysis:
By analyzing the JAVA process started at boot and the JAVA process started manually, we found that tty is different. Then we googled the relationship between sudo and tty and found that sudo requires tty by default. When the system starts up, there is no tty, and we use the ssh client. Log in to the server, kill tomcat, and then restart tomcat. At this time, you can get the tty as pts, so you can use the sudo command.
Solution:
By modifying the /etc/sudoers file, change
Defaults requiretty
Just comment it out