我似乎无法以普通用户身份运行 tomcat 服务,当我以 root 身份尝试时,我得到了
root@ip-:/home/ubuntu# service tomcat
Run as /etc/init.d/tomcat <start|stop|restart>
root@ip-:/home/ubuntu# service tomcat start
Failed to start tomcat.service: Unit tomcat.service not found.
root@ip-:/home/ubuntu#
我确实手动运行了它,它似乎有效
root@ip-:/home/ubuntu# /etc/init.d/tomcat start
Using CATALINA_BASE: /usr/local/tomcat/current_tomcat
Using CATALINA_HOME: /usr/local/tomcat/current_tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/current_tomcat/temp
Using JRE_HOME: /usr/local/java/current_java
Using CLASSPATH: /usr/local/tomcat/current_tomcat/bin/bootstrap.jar:/usr/local/tomcat/current_tomcat/bin/tomcat-juli.jar
Tomcat started.
如果我以普通用户身份运行它,我似乎无法获得权限。我有一些用户没有密码,只有 ssh 密钥。
ubuntu@ip-:~$ service tomcat start
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to start 'tomcat.service'.
Multiple identities can be used for authentication:
1. Ubuntu (ubuntu)
2. An,,, (an)
Choose identity to authenticate as (1-2): 1
Password:
polkit-agent-helper-1: pam_authenticate failed: Authentication failure
==== AUTHENTICATION FAILED ===
Failed to start tomcat.service: Access denied
See system logs and 'systemctl status tomcat.service' for details.
ubuntu@ip-:~$
这是服务脚本
ubuntu@ip-:~# cat /etc/init.d/tomcat
#!/bin/bash
export CATALINA_HOME=/usr/local/tomcat/current_tomcat`
start() {
sh $CATALINA_HOME/bin/startup.sh
}
stop() {
sh $CATALINA_HOME/bin/shutdown.sh
}
case $1 in
start|stop) $1;;
restart) stop; start;;
*) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac
ubuntu@ip-:~# ll /etc/init.d/tomcat
-rwxr-xr-x 1 root root 306 Feb 14 07:20 /etc/init.d/tomcat*
答案1
我在使用 Ubuntu 16.04 时遇到了这个问题。我已经解决了。
首先,在脚本顶部添加一行:
. /lib/lsb/init-functions
然后在 shell 中,创建从 /etc/rc* 到我的脚本的符号链接:
sudo update-rc.d tomcat defaults 95
这也将使您的脚本在重启后自动运行。