I wrote a service script, and want to start tomcat6 when reboot. but it not work:
- the script file is /etc/init.d/tomcat6 (see the following)
- i've already chkconfig --add tomcat6, and no error occur, chkconfig --list can list tomcat6 service
- service tomcat6 start/stop can work properly
- but after reboot or systemctl start/stop tomcat6.service does not start tomcat6: the script was executed, and a file was created by the script
- SELinux is in Enforcing mode, but setenforce 0 does not help.
- Fedora 15, kernel 2.6.38.8-32.fc15.x86_64
There're no related log in
/var/log/message,
/var/log/audit/audit.log logged the following:
Code:
type=USER_CMD msg=audit(1308242653.204:369): user pid=3002 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:init_t:s0 msg='cwd="/" cmd="/usr/tomcat6/bin/startup.sh" terminal=? res=failed'
type=SERVICE_START msg=audit(1308242653.228:370): user pid=1 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:init_t:s0 msg=': comm="tomcat6" exe="/bin/systemd" hostname=? addr=? terminal=? res=success'
There's
failed at the end of the first line, but I can't get more detail information from it.
/etc/init.d/tomcat6
Code:
#!/bin/bash
#
# Startup script for Tomcat
#
# chkconfig: 35 80 20
# description: Tomcat service script
### BEGIN INIT INFO
# Provides: tomcat6
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 2 4 6
# Short-Description: Start and stop tomcat6
# Description: Tomcat 6, servlet 2.5, JSP 2.1
### END INIT INFO
ret=0
NAME="$(basename $0)"
LOCK_FILE="/var/lock/subsys/$NAME"
TOMCAT_HOME=/usr/tomcat6
start ()
{
if [ -f "$LOCK_FILE" ]
then
echo "$LOCK_FILE exists, this assuming Tomcat is running ! If it's not, delete $LOCK_FILE and start again."
exit 2
fi
echo "Starting $NAME ..."
sudo -u tomcat "$TOMCAT_HOME/bin/startup.sh"
touch "$LOCK_FILE"
}
stop ()
{
echo "Stopping $NAME ..."
sudo -u tomcat "$TOMCAT_HOME/bin/shutdown.sh"
rm -f "$LOCK_FILE"
}
restart ()
{
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "$1 is not supported."
echo "Usage: $0 {start|stop|restart}"
exit 3
esac
exit $ret
am i missed something or something wrong in the script?
---------- Post added at 12:03 PM ---------- Previous post was at 12:50 AM ----------
I don't know how to debug/verbose the output `systemctl start XX`, so I redirect some output in the script to a log file. finally, it's `sudo` failed with
Code:
sudo: sorry, you must have a tty to run sudo
it seems the internal mechanism between `service` and `systemctl` are different.
I take a look at the official tomcat6 init.d script, the offcial one use `/bin/su` or `/sbin/runuser`, not `sudo`. after I changed from `sudo` to `su`, the script works now.
Code:
#!/bin/bash
#
# Startup script for Tomcat
#
# chkconfig: 35 80 20
# description: Tomcat service script
### BEGIN INIT INFO
# Provides: tomcat6
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 2 4 6
# Short-Description: Start and stop tomcat6
# Description: Tomcat 6, servlet 2.5, JSP 2.1
### END INIT INFO
ret=0
NAME="$(basename $0)"
LOCK_FILE="/var/lock/subsys/$NAME"
#LOG_FILE=/var/log/tomcat6-service-log.txt
#chown ${TOMCAT_USER}:${TOMCAT_USER} "$LOG_FILE"
#whoami >> "$LOG_FILE"
TOMCAT_HOME=/usr/tomcat6
TOMCAT_USER="${TOMCAT_USER:-tomcat}"
TOMCAT_LOG="$TOMCAT_HOME/logs/catalina.out"
SU_COMMAND="/bin/su"
if [ -x "/sbin/runuser" ]; then
# For SELinux we need to use 'runuser' not 'su'
SU_COMMAND="/sbin/runuser" # /sbin/runuser -s /bin/sh
fi
start ()
{
if [ -f "$LOCK_FILE" ]
then
echo "$LOCK_FILE exists, this assuming Tomcat is running ! If it's not, delete $LOCK_FILE and start again."
exit 2
fi
echo "Starting 正在启动 $NAME ..."
# 如果用 systemctl start tomcat6.service 来启动,则 sudo 失败:sudo: sorry, you must have a tty to run sudo
#sudo -u tomcat "$TOMCAT_HOME/bin/startup.sh" >> "$LOG_FILE" 2>&1
#consoletype >> "$LOG_FILE" 2>&1
$SU_COMMAND $TOMCAT_USER -c "$TOMCAT_HOME/bin/startup.sh" >> "$TOMCAT_LOG" 2>&1
touch "$LOCK_FILE"
}
stop ()
{
echo "Stopping 正在停止 $NAME ..."
#sudo -u tomcat "$TOMCAT_HOME/bin/shutdown.sh" >> "$LOG_FILE" 2>&1
$SU_COMMAND $TOMCAT_USER -c "$TOMCAT_HOME/bin/shutdown.sh" >> "$TOMCAT_LOG" 2>&1
rm -f "$LOCK_FILE"
}
restart ()
{
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "$1 is not supported. 尚不不支持 $1 操作"
echo "Usage 用法: $0 {start|stop|restart}"
exit 3
esac
exit $ret