Tomcat Start on Reboot: init.d script unsuccessful - linux

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.

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.

VPS in CentOS7, run Files JAVA on start my vps

my vps in centos 7 ,
I have applications developed in java, 3 files .jar .
I need to run this files when my vps start or log-in, like example "java -jar file-name"
how i can run that file like service
i have the second question is,
what is the file in centos that has the list of services that run when you start centos.
For edit that file and add my jar. files
The second questions:
CentOS uses systemd to start system-wide or user-defined services. You can use systemctl to find out. For example, checking out the SSH server daemon, we can do:
[user1#centos Good]$ systemctl | grep ssh
sshd.service loaded active running OpenSSH server daemon
You can write your own .service file and put it under one of the following directories to make your java program run like a service.
/usr/lib/systemd/system/
/lib/systemd/system/
To know more about systemd and .service file, you can check CentOS / RHEL 7 : Beginners guide to systemd
edit: 2019-11-13 18:53:47
//java_program.service
[Unit]
Description=java_program
[Service]
Type=simple
User=root
ExecStart=/usr/bin/java -jar /root/folder/name.jar
RestartSec=5
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=always
[Install]
WantedBy=multi-user.target
Place java_program.service at path /usr/lib/systemd/system/java_program.service
Run command:
sudo systemctl enable java_program
sudo systemctl start java_program

Restarting Services in Linux after a Server Reboot

So today one of our application servers were restarted due to some issue and after restart we found that our application services were not running.
I want to create one script which will check these below services after a server restart and start them automatically if found stopped:
1st Service with Path : /opt/bea/config/nm/nm-sdi-abc/beaNMctl.sh 
2nd service TOMCAT - Path : /opt/apache/tomcat/bin --- Service name startup.sh
Catch here is 1st service can be started with the normal id account that i use.
But 2nd service can be restarted after logging into a different service account on same server and network. Like below:
[x201691#abc bin]$ su - apache
Password:
-bash-2.05b$ cd /
-bash-2.05b$ cd /opt/apache/tomcat/bin/
-bash-2.05b$ ./startup.sh
Can someone help?
Also we are not root users.
You can write a shell script:
echo YOUR_PASSWORD | sudo -S su
cd /opt/apache/tomcat/bin/
./startup.sh
Save this as a file somewhere you have access and add the following cron entry:
#reboot MYPATH/myscript.sh >> MYPATH/script.log 2>&1
script.log will contain any output or errors from your script. You can add date command to the script to help with information on when it was run. More information on cron here.
Also, if you have concern with putting password in the script, you can go through the discussion here.
Preferred approach when installing Tomcat in Linux is to make Tomcat as a service.
This will ensure your service is started after reboot
1. Create the service file with the following command:
touch /etc/systemd/system/tomcat.service
2. Assign the relevant rights to the file you created:

 chmod 664 /etc/systemd/system/tomcat.service
3. Paste the following content in the file while adapting it to your configuration:
[Unit]
Description=Application description/name
After=syslog.target network.target
[Service]
Type=forking
User=tomcat
ExecStart=$CATALINA_HOME/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
Install]
WantedBy=multi-user.target
4. Reload the service daemon:
 systemctl daemon-reload
5. Start the service:
 systemctl start tomcat
6. To check status : 
 systemctl status tomcat

How to install Jenkins Ubuntu slave as a service?

I have a Ubuntu 16.04 LTS machine where I am successfully connected to the Jenkins server via JNLP connection. Steps I took for the connection are the following:
Create a directory in the slave called /home/MyUbuntu/Jenkins
Download both agent.jar and slave-agent.jnlp files into the
directory
Run this command from the terminal:
java -jar agent.jar -jnlpUrl http://my-jenkins-server:8080/computer/MyNode/slave-agent.jnlp -secret 6f8bb3250d6dbcda77979797997b0ea6bcaaa064785d558c0e4ea07d03 -workDir "/home/MyUbuntu/Jenkins"
The connection is successful.
Problem:
Once I close the terminal the connection gets disconnected.
Question:
How do I add this as a service in Ubuntu 16.04 LTS so whenever the machine is rebooted it starts as a startup???
If you use System D, add a file like this to /etc/systemd/system/.
[Unit]
Description=Jenkins slave connection
Wants=network.target
After=network.target
[Service]
ExecStart=java -jar agent.jar -jnlpUrl http://jenkinsurl:port/endpoint.jnlp -secret 4lph4num3r1cs3cr3t -workDir "/base/path/of/your/jenkinsjar"
Restart=always
WorkingDirectory=/base/path/of/your/jenkinsjar
User=my-user
Group=my-group
RestartSec=20s
[Install]
WantedBy=multi-user.target
Alias=jenkins.service
Permissions and ownership of the file may vary based on the service or OS. Long list files in /lib/systemd/system/ to get an idea of what perms you need or want (probably root:root 644).
Notice the command is the command that Jenkins provides for you when you create an agent jar. Just use that for ExecStart.
For user and group, I use the user that owns the directory where the Jenkins workspace is located. For example, if the Jenkins workspace is in /home/ubuntu, I specify ubuntu as user and group.
After that...
Prefix these with sudo if you're not running as root:
Probably a good idea to reload System D: systemctl daemon-reload.
Start: systemctl start jenkins.service. Notice this command pertains to the last line of the file Alias.
Enable it if you want the service to start with your computer: systemctl enable jenkins.service.
Here are the Git Gists I based my file on:
https://gist.github.com/unakatsuo/d4711f52a0ab0b9bc8010018149a7e84
https://gist.github.com/dragolabs/05dfe1c0899221ce51204dbfe7feecbb
I'm sure there's a lot more that can be done for the service config but in my case, I manage a lot of different servers and just need the thing to start automatically after boot!

How do I start a service on boot in debian?

So I created a service and made it executale in /etc/init.d
Then I tried to make it start after boot with the following command:
update-rc.d <myService> defaults
But the output were warnings of other services (which work just fine by the way) and I don't want to touch those.
My next attempt to solve this was to add the following lines in the rc.local file:
do_start() {
service <someOtherService> stop
service <myService> stop
sleep 5
service <someOtherService> start
service <myService> stop
........
As I said, I have some other service that works just fine, but my service which I am currently trying to add wont start after boot.
One more thing that I would like to add is when I manually enter:
service <myService> start/stop
then it works just fine
To add a service at boot, you can add an Upstart job.
Create your conf file in /etc/init (e.g. /etc/init/myjob.conf)
with a content like this (Example)
description "My job"
start on startup
task
exec /path/to/script.sh
More informations here (Debian doc)
Info: you need the upstart package.
The following works very well for me.
First determine if your system is running SysV init or systemd, for that use:
$ ps -p 1
If SysV init:
$ sudo update-rc.d <service_name> defaults 95 10
If systemd:
$ sudo /bin/systemctl daemon-reload
$ sudo /bin/systemctl enable <service_name>.service
I successfully used the last one on a ParrotSecurity OS that is based on Debian 5.

Resources