Create service tomcat - Start and Shutdown - linux

I installed the apache tomcat but to start the service, i have to go to the path $HOME/apache-tomcat-8.0.28/bin and run the script ./startup.sh to start and ./shutdown.sh to shutdown. How can I make like
service tomcat start
service tomcat stop
or another way for not need to go to the path of the tomcat on Ubuntu?

/etc/init.d/tomcat stop
/etc/init.d/tomcat start

Related

Bash script to run automatically after server restart

I have a script, it restarts tomcat in 2 minutes,, I would like that
this script was started constantly after server restart, I will be grateful if someone shows how to do it?
#! /bin/bash
sleep 120
systemctl restart tomcat
Read archwiki about systemd/Timer or maybe original freedesktop documentation about systemd.timer.
Create a file my_super_tomcat_restarter.timer in /etc/systemd/system/:
[Unit]
Description=Superbly restart my tomcat service every 120 seconds!
[Timer]
OnBootSec=120sec
OnUnitActiveSec=120sec
[Install]
WantedBy=timers.target
Create a file my_super_tomcat_restarter.service in /etc/systemd/system/ with the content:
[Unit]
Description=Superbly restart tomcat service!
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart tomcat
Execute from your terminal as root:
systemctl enable my_super_tomcat_restarter.timer
The timer should fire in 120 seconds and execute my_super_tomcat_restarter.service which in turn will restart your tomcat service.
either add it in init.d or systemd based on your Linux distribution of yours. in both cases you have to be root to add your script.

Tomcat Start on Reboot: init.d script unsuccessful

I need to have tomcat start after a reboot of the linux OS. I cannot get init.d to function properly through reboot.
OS and Versions:
JRE: 1.8.0
JAVA: 1.8.0
Tomcat: 8.5.34
Linux: Amazon Linux 2
****ALL STEPS COMPLETED AS ROOT
TOMCAT Deployment Configuration:
1) Install tomcat 8.5.34 using a tar.gz gzip file
2) configure /{$TOMCAT}/conf/server.xml to use 443 connectors
3) Deploy MicroStrategy application through deploying a .war file on restart
4) configure SSL keys using Java Key Store
5) configure microstrategy webapp for SAML authentication using PING
init.d Script Deployment Configuration
Note: I have tried various scripts through /etc/init.d/tomcat and the chkconfig utility.
1) Create tomcat using vi
2) Insert script (I have tried numerous scripts, but this one seems to
be the clostest to exactly what I need and the most explicit)
3) chmod 755 /etc/init.d/tomcat
4) chkconfig --add tomcat
5) chkconfig --level 2345 tomcat on (This command is not successful)
6) chkconfig --list tomcat (returns tomcat 0:off 1:off 2:off 3:on 4:on 5:on 6:off)
Testing of this script is successful:
./etc/init.d/tomcat start
./etc/init.d/tomcat stop
./etc/init.d/tomcat restart
Confirmed that chkconfig created the links:
/etc/rc1.d K20tomcat
/etc/rc2.d K20tomcat
/etc/rc3.d S82tomcat
/etc/rc4.d S82tomcat
/etc/rc5.d S82tomcat
/etc/rc6.d K20tomcat
Script File for Tomcat
#!/bin/sh
#
# chkconfig: 345 82 20
#
# description: Tomcat Service
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
JRE_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
CATALINA_HOME=/opt/apache-tomcat-8.5.34
export JAVA_HOME JRE_HOME CATALINA_HOME
case $1 in
start)
cd $CATALINA_HOME/bin
./startup.sh
;;
stop)
cd $CATALINA_HOME/bin
./shutdown.sh
;;
restart)
cd $CATALINA_HOME/bin
./shutdown.sh
./startup.sh
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
Expectations
I expect the base URL at {$TOMCAT}/webapps/ROOT to be accessible from the Public URL pointing to this device following a reboot. The tomcat services remain in a stopped state after reboot.
Any suggestions?
Amazon Linux 2 uses systemd service manager which should be backwards compatible with systemv init scripts provided that systemd-sysv-generator is executed to generate service units out of /etc/init.d scripts (not recommended in your case I think).
Since you are writing the script yourself it is recommended that you write a proper service unit.
It's probable that such *.service file is already present on the tar.gz used to install tomcat.
Enable tomcat using systemd rather than systemv
Description
The script used is relatively simple because its only function is to start the server at reboot. I have established all of the required environment variables using setenv.sh in the TOMCAT bin.
Variables
TEST: Any alphanumeric value
TOMCAT_INSTALL_PATH: the location where you installed TOMCAT
Steps
Create a file tomcat#.service in /etc/systemd/system
A template can be found in /etc/systemd/system/multi-user.target.wants/tomcat.service
tomcat#.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment="CATALINA_HOME={TOMCAT_INSTALL_PATH}"
ExecStart=/opt/apache-tomcat-8.5.34/bin/startup.sh
ExecStop=$CATALINA_HOME/bin/shutdown.sh
SuccessExitStatus=143
User=root
[Install]
WantedBy=multi-user.target
create empty tomcat.pid file (mine was in {TOMCAT_INSTALL_PATH}/conf
write CATALINA_PID="{TOMCAT_INSTALL_PATH}/conf/tomcat.pid" line to setenv.sh in the {TOMCAT_INSTALL_PATH}/bin
systemctl daemon-reload
systemctl enable tomcat#test.service
systemctl start tomcat#test.service
Troubleshoot
systemctl status tomcat#test.service -l
-This command will provide log output to the console which displays the output which is also written to the systemctl log. I received errors on initial setup because tomcat could not interpret which was the main tomcat process and would close when reading the end of other processes. This was overcome by creating a pid file in the /conf folder and setting the CATALINA_PID variable in setenv.sh.

systemd: how to start a tomcat service after a successful tomcat service start

I have two tomcat servers. I want with systemd to have tomcatB started only if tomcatA was successfully started.
e.g. tomcatB service should start right after "org.apache.catalina.startup.Catalina.start Server startup" is logged in catalina.out log file. Is that possible?
Try using both After= and Requires= in the unit file for tomcatB to specify that it depends on A and should start after it.

How to trigger a custom script to run whenever a specific systemd service restarts

I wish to know how can I schedule a custom script to run whenever I restart a service.
My use case is that I have to run several commands whenever I restart my Tomcat Service. I want to know if there is a way I can write a script and schedule it to run whenever I restart the Tomcat service.
I have setup the tomcat script as a systemd service. I am using Cent OS 7 x64.
I have been able to achieve this by creating another service and incorporating the Tomcat service's start stop in the new service. The new service acts as a wrapper service which first starts tomcat and then executes the commands that we need to run as soon as tomcat starts.
Then while stopping, it stops tomcat and runs clean up commands.
EDIT: I found another way of doing this on unix & linux stackexchange.
Simply create an new systemd .service file in /etc which includes and overrides part of the one in /lib. For example, create /etc/systemd/system/tomcat.service to contain
.include /lib/systemd/system/tomcat.service
[Service]
ExecStartPre=/home/meuh/myscripttorun some pre args here
ExecStartPost=/home/meuh/myscripttorun some post args here
Any ExecStartPre lines will be executed before the ExecStart line, and similarly any ExecStartPost will run after tomcat has started.

Tomcat starts after issuing 'stop' command

I am seeing very confusing behavior in my tomcat.
I execute:
/usr/libexec/tomcat/server stop
But instead of stopping tomcat restarts. I issue the command a 2nd time, and then it actually stops. I have tried searching but have not come up with a good way to search 'restarts after stop'. Almost all results talk about scripts to restart tomcat, and stop/start functionality.
You can stop tomcat with this command: your/path/tomcat/bin/./catalina.sh stop and your/path/tomcat/bin/./catalina.sh start to start the server.
If you use linux and your catalina.sh does not have executable permission you need execute this: chmod +x catalina.sh before

Resources