I can't define a valid upstart conf script to run a java service using upstart with the following requirements:
I have to specify classpath using folders because I have many jars in multiple folders
I have to listen to the shutdown signal fired by service myservicename stop
Based on that answer, I implemented a shutdown hook listener so I need upstart to send me the termination signal and wait for my application to terminate.
Here is my buggy upstart script:
description "masa"
author "Muhammad Gelbana <m.glba#gmail.com>"
start on runlevel [2345]
stop on shutdown
kill timeout 120
script
LOGS_DIR=/home/mgelbana/services/RealServices/logs
IPK_DB=/home/mgelbana/services/RealServices/config/db-ipk.properties
PRO_DB=/home/mgelbana/services/RealServices/config/db-reporting-engine.properties
MAIN_CLASS=com.sger.masaTA
mkdir -p $LOGS_DIR
CLASSPATH="/home/mgelbana/services/RealServices/masa-RealService-TA.jar"
for i in /home/mgelbana/services/commons/*.jar; do
CLASSPATH="$CLASSPATH:$i"
done
for i in /home/mgelbana/services/RealServices/lib/*.jar; do
CLASSPATH="$CLASSPATH:$i"
done
echo '\n\n\n====================================================='
echo 'Service startup:\t'`date`
echo 'Main class:\t\t'`echo $MAIN_CLASS`
echo 'Logs directory:\t\t'`echo $LOGS_DIR`
echo 'masa database configuration:\t'`echo $IPK_DB`
echo 'Pro configuration file:\t'`echo $PRO_DB`
echo 'Starting engine...'
java -Dta.id=2 -DIPK_DB=$IPK_DB -DPRO_DB=$PRO_DB -cp $CLASSPATH $MAIN_CLASS
end script
The following error is shown in the /var/log/upstart/myservicename.log log:
/proc/self/fd/9: 9: /proc/self/fd/9: Syntax error: word unexpected (expecting "do")
Thank you.
Related
I have created a fat-jar for my apache camel 3 route. I put that on my raspberry pi (bullseye) in /opt/myhome and could start it successfully with java 17 (also works with sudo):
/usr/bin/java -jar /opt/myhome/myhome-1.0-SNAPSHOT-jar-with-dependencies.jar
Then I have added /lib/systemd/system/myhome.service as:
[Unit]
Description=My home automation with apache camel 3.
After=network.target
[Service]
ExecStart=/usr/bin/java -jar /opt/myhome/myhome-1.0-SNAPSHOT-jar-with-dependencies.jar > /var/log/myhome.log 2>&1
[Install]
WantedBy=network.target
Then I did:
sudo systemctl daemon-reload
sudo systemctl enable myhome.service
sudo systemctl start myhome.service
After starting and waiting for some seconds I executed
systemctl status myhome.service
Which displays me
● myhome.service - My home automation with apache camel 3.
Loaded: loaded (/lib/systemd/system/myhome.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2022-10-07 18:28:52 CEST; 1h 18min ago
Process: 18159 ExecStart=/usr/bin/java -jar /opt/myhome/myhome-1.0-SNAPSHOT-jar-with-dependencies.jar > /var/>
Main PID: 18159 (code=exited, status=0/SUCCESS)
CPU: 10.174s
systemd[1]: Started My home automation with apache camel 3..
java[18159]: WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
java[18159]: Apache Camel Runner takes the following options
java[18159]: -h or -help = Displays the help screen
java[18159]: -r or -routers <routerBuilderClasses> = Sets the router builder classes which will be loaded while starting the camel context
java[18159]: -d or -duration <duration> = Sets the time duration (seconds) that the application will run for before terminating.
java[18159]: -dm or -durationMaxMessages <durationMaxMessages> = Sets the duration of maximum number of messages that the application will process before terminating.
java[18159]: -di or -durationIdle <durationIdle> = Sets the idle time duration (seconds) duration that the application can be idle before terminating.
java[18159]: -t or -trace = Enables tracing
java[18159]: -ts or -traceStandby = Enables tracing standby
java[18159]: -e or -exitcode <exitcode> = Sets the exit code if duration was hit
java[18159]: -pl or -propertiesLocation <propertiesLocation> = Sets location(s) to load properties, such as from classpath or file system.
systemd[1]: myhome.service: Succeeded.
systemd[1]: myhome.service: Consumed 10.174s CPU time.
So this means the process was started, but then not the same as when starting the jar manually happens, but a help message seems to appear.
So the question is, why did the jar behaves differently as when started manually?
I also tested to change the code in myhome.service to:
/usr/bin/java -jar /opt/myhome/myhome-1.0-SNAPSHOT-jar-with-dependencies.jar -e 10 -d 60 -r de.powerstat.camel.homeautomation.HomeautomationRouteBuilder > /var/log/myhome.log 2>&1
Which results in the same as above. So no different exit code because of a timeout, or a not found route class.
What point did I miss here?
Update 1:
Completed the systemctl status output. Looks like this comes from org.apache.camel.main.MainCommandLineSupport
So the question is still why this shows up when starting in systemd context and not when starting within the bash?
Within my jar file the META-INF/MANIFEST.MF looks like:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: PowerStat
Build-Jdk: 17.0.2
Main-Class: de.powerstat.camel.homeautomation.MainApp
while the MainApp class is defined as follows:
public class HomeautomationRouteBuilder extends RouteBuilder
After some research I found out that "> /var/log/myhome.log 2>&1" will be passed as three parameters to args. Because these parameters are not recognized results in the shown help message.
We were using the cf-uaa's gradle tasks to create a docker image but those have been removed in the latest version. I've loaded the war in a recent version, but the service does not seem to be starting correctly.
I've been building the war from the v74 tag, adding it to tomcat:8.5.45-jdk12-openjdk-oracle or tomcat:9.0.24-jdk12-openjdk-oracle, and setting the various env vars that we were passing in to the previous image. I'm not seeing any log entries after the initial tomcat output stating that my war has been deployed and the server startup time.
The Dockerfile is basically just an adaptation of what was being passed in the previous image:
FROM tomcat:8.5.45-jdk12-openjdk-oracle
#FROM tomcat:9.0.24-jdk12-openjdk-oracle
ENV LOGIN_CONFIG_URL WEB-INF/classes/required_configuration.yml
ENV UAA_CONFIG_PATH /uaa
RUN bash -c "rm -r /usr/local/tomcat/webapps/ROOT"
RUN bash -c "rm -r /usr/local/tomcat/webapps/host-manager"
RUN bash -c "rm -r /usr/local/tomcat/webapps/manager"
RUN bash -c "rm -r /usr/local/tomcat/webapps/examples"
RUN bash -c "rm -r /usr/local/tomcat/webapps/docs"
ADD *.war /usr/local/tomcat/webapps/uaa.war
RUN bash -c "echo $LOGIN_CONFIG_URL"
EXPOSE 8080
I would expect to see the service responding to my requests, or some errors in the log indicating that the war failed to deploy. I am not currently getting any log output generated from the application code. When I send a request to the service, the response is a 500 with the an error header from the service.
X-Cf-Uaa-Error:Server failed to start. Possible configuration error.
update: I've located the uaa logs within .../tomcat/logs/uaa.log I'm not seeing anything indicating that the service failed to deploy, but I am also not seeing anything to indicate that it is picking up the env vars I have set in the container. I recreated the service using the war from the original setup which started successfully using the uaa.yml which I mounted as a volume. Comparing the logs, the original setup's first log entry is YamlProcessor which does not show up in the v75 logs at all. In fact, no debug entries show up at all, which suggests to me that my LOG_LEVEL env var is not propagating either.
Update 2: We reverted the image base to FROM tomcat:8.5-jre8 and started seeing flyway errors in the uaa.log. Our previous datasource url format was url: jdbc:postgresql://${POSTGRES_NAME}:5432/${DB}?currentSchema=uaa which caused a flyway exception. After removing the schema reference, it created the tables in the public schema. By creating the uaa schema manually before starting the service, it was able to run with the original format. The flyway version has updated, so perhaps there something new that needs to be set.
The application seems to be running, but when I try to get a token at /uaa/oauth/token I get a 500 with this error in the logs: Caused by: java.lang.NoSuchMethodError: java.nio.CharBuffer.limit(I)Ljava/nio/CharBuffer;
Since Jan 2021, UAA server docker images is now be available on cloudfoundry/uaa dockerhub repository.
docker pull cloudfoundry/uaa:75.0.0
See its Dockerfile for more details.
Can you try following ?
https://github.com/hortonworks/docker-cloudbreak-uaa
This works very well.
I'm attempting to create a new Glassfish domain. I'm logged in to machine A and Glassfish is installed on machine B. The command I'm running is:
invoke-command -Session $session -scriptblock {Start-Process -FilePath "adadmin" -ArgumentList "create-domain --instanceport 28182 --nopassword=true my_gf_domain_name" -WorkingDirectory D:\Glassfish\4.1.13\bin
This does not return an error and I can't see a log anywhere on server B that would give any indication as to why this is failing:
If I login to server B and run:
Start-Process -FilePath "adadmin" -ArgumentList "create-domain --instanceport 28182 --nopassword=true my_gf_domain_name" -WorkingDirectory D:\Glassfish\4.1.13\bin
The domain "my_gf_domain_name" creates fine. A cmd window pops up (with output from the domain create process) in the background when I run the Start-Process on machine B. My guess is that this pop-up is possibly causing my problem.
I have a spring boot application which I run using an executable jar file. Currently to stop the service we are just killing the process. I saw that we can use the following methods to shutdown the application gracefully.
ApplicationContext ctx = SpringApplication.run(Application.class, args);
and then somewhere in the code I can call ctx.close() Or we can use the following static method.
SpringApplication.exit(ApplicationContext, ExitCodeGenerator)
It works for us currently, but we are actually calling this ctx.close() method inside a controller as follows.
#RequestMapping("/shutdownSpringBoot")
public void shutdownApplication() {
MyApplication.ctx.close(); // I'm saving the context returned by spring boot in a static variable inside my main class
}
When we hit this controller method via http the application is gracefully shutdown. But we dont want to do it this way. Is it possible to write a shell / batch script to trigger the java class inside which I can call the ctx.close() method ? We are looking for a shutdown script like the one we get from a standalone tomcat container (shutdown.bat / shutdown.sh), so that we can give our application as a jar file to our customers and they can start or stop the application by executing those scripts. (Which they are used to).
Thanks,
Sanjay
I think you can simply detect that you are running your jar as a command line utility by inspecting command line parameters and instead of loading whole Spring context and booting up the web application, you just HTTP access the local shutdownSpringBoot URL using built-in Java HTTP client.
You can create a shell script and directly shutdown the springboot application. Just make sure you register shutdown hook to your spring boot application. In the shutdown hook, you can close the context. You can check my answer here
I use PID file writer to write file and store Jar and Pid in folder with same name as of application name and shell scripts also have same name with start and stop in the end.
Main Class
#SpringBootApplication
public class MyApplication {
public static final void main(String[] args) {
SpringApplicationBuilder app = new SpringApplicationBuilder(MyApplication.class);
app.build().addListeners(new ApplicationPidFileWriter());
app.run();
}
}
YML FILE
spring.pid.fail-on-write-error: true
spring.pid.file: /server-path-with-folder-as-app-name-for-ID/appName/appName.pid
Here is the start script(start-appname.sh):
shellScriptFileName=$(basename -- "$0")
shellScriptFileNameWithoutExt="${shellScriptFileName%.*}"
appName=${shellScriptFileNameWithoutExt:6}
PROCESS=$1
PIDS=`ps aux |grep [j]ava.*$appName.*jar | awk {'print $2'}`
if [ -z "$PIDS" ]; then
echo "No instances of $appName is running..." 1>&2
else
for PID in $PIDS; do
echo "Waiting for the process($PID) to finish on it's own for 3 mins..."
sleep 3m
echo "FATAL:Killing $appName with PID:$PID."
kill -9 $PID
done
fi
# Preparing the java home path for execution
JAVA_EXEC='/usr/bin/java'
# Java Executable - Jar Path Obtained from latest file in directory
JAVA_APP=$(ls -t /server-path-with-folder-as-app-name/$appName/$appName*.jar | head -n1)
# JVM Parameters and Spring boot initialization parameters
JVM_PARAM="-Xms512m -Xmx1024m -Dspring.profiles.active=sit -Dcom.webmethods.jms.clientIDSharing=true"
# To execute the application.
FINAL_EXEC="$JAVA_EXEC $JVM_PARAM -jar $JAVA_APP"
# Making executable command using tilde symbol and running completely detached from terminal
`nohup $FINAL_EXEC </dev/null >/dev/null 2>&1 &`
echo "$appName has been started successfully."
Here is the stop script(stop-appname.sh):
shellScriptFileName=$(basename -- "$0")
shellScriptFileNameWithoutExt="${shellScriptFileName%.*}"
appName=${shellScriptFileNameWithoutExt:5}
# Script to stop the application
PID_PATH="server-path-with-folder-as-app-name-for-PID/$appName/$appName.pid"
if [ ! -f "$PID_PATH" ]; then
echo "Process Id FilePath($PID_PATH) Not found"
else
pid=`cat $PID_PATH`
if [ ! -e /proc/$pid -a /proc/$pid/exe ]; then
echo "$appName was not running.";
else
kill $pid;
echo "Gracefully stopping $appName with PID:$pid..."
fi
fi
I have a script that needs to run after tomcat has finished starting up and is ready to start deploying applications. I'm using $TOMCAT_HOME/bin/startup.sh which returns immediately. How can I wait until tomcat has finished starting up?
There are probably several ways to do this. The trick we use is:
#!/bin/bash
until [ "`curl --silent --show-error --connect-timeout 1 -I http://localhost:8080 | grep 'Coyote'`" != "" ];
do
echo --- sleeping for 10 seconds
sleep 10
done
echo Tomcat is ready!
Hope this helps!
It's not hard to implement programaticaly. You can implement org.apache.catalina.LifecycleListener and then you'll have
public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
if(lifecycleEvent.getType().equals(Lifecycle.START_EVENT))
//do what you want
}
}
in web.xml :
<Context path="/examples" ...>
...
<Listener className="com.mycompany.mypackage.MyListener" ... >
...
</Context>
Please notice that some things could differ between 6-9 Tomcats.
Are you still looking for an answer? It depends on your definition of started. If your definition of started is "Now its safe to stop" then you might want to verify if port 8005 is listening.
Depends on what you mean by finishing. What do you want to wait for?
You could, for example, have a script that hits a URL repeatedly until it gets a desirable result that would only be available once the app is properly initialized.
You could also have a context listener that writes out an "I'm ready" file that you use to signal the readiness of your application. (If you do this, be sure the file doesn't exist before starting your app container).
I needed this to test from jenkins if the tomcat from the remote server started for a system check.
until [[ `ssh -o StrictHostKeyChecking=no root#${DEPLOY_HOST} 'netstat -tulpn | grep 8005'` != "" ]] ; do echo "waiting for tomcat"; sleep 6; done
There isn't an easy method. As far as startup.sh and catalina.sh are concerned, tomcat is running when they finish. Although, internally, tomcat is still initializing and starting contexts.
It would help to know if you were trying to find out if your context finished loading or if you are just wanting a general, "Tomcat is runnnig although your contexts might not be completely loaded..."
If it is the latter you could create a web app that simply has a context listener that will execute a script using Runtime. If you were handy, you could make the webapp configuable via the web.xml file to accept a parameter that points to the script to execute.
Personally, I would just watch catalinas log for a specific string depending on how your setup and what exact phase your looking for.
I have done it with the following code in jenkins pipelinescript with tomcat.
Before i just call
sudo /bin/systemctl restart tomcat
And have an entry in my sudoers file for the jenkins user.
Now here is the oneliner:
until [ "$(curl -w '%{response_code}' --no-keepalive -o /dev/null --connect-timeout 1 -u USERNAME:PASSWORD http://localhost:8080/manager/text/list)" == "200" ]; do echo --- sleeping for 1 second; sleep 1; done
Better readable:
until [ "$(curl -w '%{response_code}' --no-keepalive -o /dev/null --connect-timeout 1 -u USERNAME:PASSWORD http://localhost:8080/manager/text/list)" == "200" ];
do echo --- sleeping for 1 second;
sleep 1;
done