Detach a bash script launched by tomcat - bash

I have implemented a dashboard servlet for my linux server. I wanted to add an option for "restart main services" to this dashboard. Basically the dashboard just run bash script with the ProcessBuilder object from java.
My main services are just tomcat and postgresql. When I want to restart postgresql, I have to restart tomcat too because if I don't, the existing connection to the database are aborted and even if I restart postgresql, tomcat cannot access my database anymore.
So there is the simple script I use to stop services :
main_services_restart.sh :
#!/bin/bash
sudo service tomcat stop
sudo service postgresql stop
sudo service postgresql start
sudo service tomcat start
If I run it manually, all is going well. But because I run it from my tomcat dashboard, when it calls the script, the script will stop the tomcat service so the script itself is stopped too and so tomcat isn't restarted.
I tried to create differents scripts to detach the main script like this :
sudo -b nohup /home/me/scripts/main_services_restart.sh
or even :
sudo /home/me/scripts/main_services_restart.sh & disown
But this isn't detaching the script from tomcat. How can I completely detach the script from the ProcessBuilder of tomcat ?

Related

How to Set the Correct Permissions to Launch Neo4J on AWS EC2 via Its Bash Script?

I'm trying to launch Neo4J graph database on AWS using their AIM image (enteprise 3.3.9)
However, the server fails to launch the instance automatically how it's supposed to.
When I try to relaunch it using
systemctl restart neo4j
It also fails.
When I do
systemctl cat neo4j
I find the /etc/neo4j/pre-neo4j.sh file, which is apparently launched on the instance's startup, which, in turn launches Neo4J (when it's supposed to work):
[Unit]
Description=Neo4j Graph Database
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/etc/neo4j/pre-neo4j.sh
Restart=on-failure
User=neo4j
Group=neo4j
Environment="NEO4J_CONF=/etc/neo4j" "NEO4J_HOME=/var/lib/neo4j"
LimitNOFILE=60000
TimeoutSec=120
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
So then I launch it manually via the bash script using the sudo prefix and then it starts up fine.
sudo /etc/neo4j/pre-neo4j.sh
The documentation on deploying Neo4J on an AWS server doesn't mention anything about permissions if you use their image. So what can be the problem?
I don't want to have manually launch the DB using the sudo — is it possible to resolve this problem by modifying the bash script itself?
..
The file /etc/neo4j/pre-neo4j.sh sets some environmental parameters and then launches neo4j via:
/usr/share/neo4j/bin/neo4j console
Based on the comments.
The solution was to use
journalctl -u neo4j
to inspect the logs associated with the failed start of neo4j. This enabled to identify the root cause, and subsequently, to fix the issue.

my spring boot webapp.war stop running on ssh logout. how to create app service so it runs without user logged in

my spring boot webapp.war stop running on ssh logout. So i tried to create my own service using $ sudo ln -s /path/to/webapp.war /etc/init.d/webapp and then $ sudo service webapp start. but it says "Failed to start webapp.service: Unit webapp.service not found." I am using ubuntu 16.04. i am logged in as non root user
If the war application doesn't have an embedded servlet container, the app would have to be deployed to one first (Tomcat, Jetty, Undertow) or to a app server (Weblogic, ...).
To run the servlet container as a Linux service, one of the options is Tanuki Java Service Wrapper

How to install Spring boot app on Ubuntu server?

I have Ubuntu server on Digital Ocean and I wrote Spring web app and now I want to put it in production.
I upload it via FTP to the server and I open my console via Putty and I use this command:
java -jar name.jar
Spring is started after that and when I open my web app everything is working fine, but when I close my Putty session my Spring web app does not work anymore. It seems like when I close my Putty session that also Spring web app is closed.
How to solve this?
While what KLHauser suggested will work, but if the vm is restarted in the cloud (which happens) your application will not automatically restart. Also stopping your application with kill -9 is error prone and dangerous, because you accidentally may kill the wrong process.
See running as Linux service section of Spring Boot documentation on how to do that.
If you’ve configured Spring Boot’s Maven or Gradle plugin to generate
a fully executable jar, and you’re not using a custom
embeddedLaunchScript, then your application can be used as an init.d
service. Simply symlink the jar to init.d to support the standard
start, stop, restart and status commands.
The script supports the following features:
Starts the services as the user that owns the jar file
Tracks
application’s PID using /var/run//.pid
Writes
console logs to /var/log/.log
Assuming that you have a Spring Boot application installed in
/var/myapp, to install a Spring Boot application as an init.d service
simply create a symlink:
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp Once installed,
you can start and stop the service in the usual way. For example, on a
Debian based system:
$ service myapp start
Just use java -jar name.jar & and the application is started in new process thread.
by adding also > log.txt directly at the end you would also have a log.

restarting tomcat instance with bamboo using a function

I'm trying to run a function that we have to recycle our tomcat instance when doing a deploy to tomcat via bamboo. I thought that we would be able to use bamboo's ssh task to call our function tomcat recycle. I am able to run the function ssh connected to the box via putty but in bamboo we get the message:
ksh: tomcat: not found
Does anybody have any experience restarting tomcat automatically via bamboo? Is there a better way?
To restart tomcat via Bamboo
Create a ssh task -
You should be having tomcat service file in /etc/init.d (if you are using redhat)
/etc/init.d/tomcat start
/etc/init.d/tomcat stop
/etc/init.d/tomcat restart
should work

can't shutdown tomcat service on Mac

I installed the Apache Tomcat/7.0.65 on my Mac, then, run the startup.sh. It works great fine and the service is available immediately. But when I run the shutdown.sh to stop the service. It seems that the shell scripts can not aware of the tomcat running. Would someone please help me with this problem?
I had a similar problem. In my case, although running <Tomcat Root>/bin>./shutdown.sh was technically working (the tomcat process was being killed), the tomcat service was restarting automatically (after a few seconds).
If you run <Tomcat Root>/bin/catalina.sh stop or <Tomcat Root>/bin/shutdown.sh and you see that after a few seconds tomcat restarts => that basically means that you are not able to shutdown tomcat for good. Thus, if you want to make sure that tomcat does not restart automatically, run brew services stop tomcat.
OBS: If you want to find what is your <Tomcat Root> run brew ls tomcat

Resources