Sultan Ahmed
Sultan Ahmed

Reputation: 2220

Can't start tomcat as service in redhat

I'm new to Linux but having spent a whole day I Installed Java and Tomcat. My goal is to host an App with this Linux box. I know it all works fine from my windows based machine, but it is my laptop so I'm planning to use the Linux Box as my dedicated server.

I am following this tutorial . From this tutorial I have executed the following command :

cd /etc/init.d 
vi tomcat 
#!/bin/bash  
# description: Tomcat Start Stop Restart  
# processname: tomcat  
# chkconfig: 234 20 80  
JAVA_HOME=/usr/java/jdk1.7.0_05  
export JAVA_HOME  
PATH=$JAVA_HOME/bin:$PATH  
export PATH  
CATALINA_HOME=/usr/share/apache-tomcat-7.0.29  

case $1 in  
start)  
sh $CATALINA_HOME/bin/startup.sh  
;;   
stop)     
sh $CATALINA_HOME/bin/shutdown.sh  
;;   
restart)  
sh $CATALINA_HOME/bin/shutdown.sh  
sh $CATALINA_HOME/bin/startup.sh  
;;   
esac      
exit 0  
chmod 755 tomcat
chkconfig --add tomcat 
chkconfig --level 234 tomcat on 
chkconfig --list tomcat 
service tomcat start  

After this command , tomcat is started at port 8082 . But when I restart pc , the tomcat is not started with boot of PC .

How can I do this ?

Upvotes: 2

Views: 5596

Answers (2)

gkatzioura
gkatzioura

Reputation: 2820

Since you use Red Hat you can use systemd for services.

Create a file /etc/systemd/system/tomcat.service:

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/java/jdk1.7.0_05
Environment=CATALINA_PID=/usr/share/apache-tomcat-7.0.29/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/share/apache-tomcat-7.0.29
Environment=CATALINA_BASE=/usr/share/apache-tomcat-7.0.29
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Duser.timezone=UTC -Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/usr/share/apache-tomcat-7.0.29/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target

I specified the script to start after syslog and network are enabled. As we can see systemd handles the tomcat as a daemon and kills the PID. With User and Group we specify the user and the group that the process should be run as.
Systemd will handle the upstart process and kill it using the PID.

To enable it to run then issue:

systemctl enable tomcat
systemctl start tomcat

Upvotes: 1

Michael
Michael

Reputation: 405

Try to use this command instead:

sudo systemctl enable tomcat

Upvotes: 0

Related Questions