0

I have a Spring Boot Java20 application that I have set up to run on a Linux server using AWS CodeDeploy. Everything works as expected, except when CodeDeploy runs the following command from the deploy.sh:

nohup java -jar -Dspring.profiles.active=uat target/pow-wow-0.0.2-SNAPSHOT.jar &

Problem

It does not create an expected nohup.out file.

When I run the deploy.sh manually it does create the nohup.out file.

Question

How do I get the deploy.sh to create the nohup.out file?

More Info

When CodeDeploy executes the deploy.sh it does so using the root user. When I run it manually, I use my own user.

deploy.sh

echo "about to deploy to pow-wow ($USER) ..."
export JAVA_HOME=/home/jboss/.sdkman/candidates/java/current
echo $JAVA_HOME
export PATH=$JAVA_HOME/bin:$PATH
cd /home/jboss/bitbucket/pow-wow
pwd
echo "stopping pow-wow ..."
echo | lsof -t -i tcp:8080
kill -9 $(lsof -t -i tcp:8080)
mv pow-wow-0.0.2-SNAPSHOT.jar target/
echo "Sorting logs  ..."
cd /home/jboss/bitbucket/pow-wow/logs
pwd
if [ -e nohup.out ]
then
  cat nohup.out >> nohup-$(date --iso).out
  echo "archive logs to nohup-date.out and remove nohup.out"
  rm nohup.out
else
  echo "could not find /home/jboss/bitbucket/pow-wow/logs/nohup.out"
  ls
fi
echo "starting pow-wow ..."
cd /home/jboss/bitbucket/pow-wow
nohup java -jar -Dspring.profiles.active=uat target/pow-wow-0.0.2-SNAPSHOT.jar &
if [ -e nohup.out ]
then
  mv nohup.out logs/
  echo "Created /home/jboss/bitbucket/pow-wow/logs/nohup.out"
else
  echo "Could not find nohup.out that should have been created from nohup java -jar"
  pwd
  ls
fi
echo "Run Start pow-wow."

Output

[stdout]Could not find nohup.out that should have been created from nohup java -jar
Richard
  • 8,193
  • 28
  • 107
  • 228
  • Out of curiosity, are you *assuming* the file isn't created because it prints that message or is the file not actually there? Because in the former case I would try to add a delay before checking whether the file exists. – Federico klez Culloca Aug 24 '23 at 10:08
  • Hi Federico, the `nohup.out` never gets added. Even if I go look after the process has run, it does not exist. – Richard Aug 24 '23 at 10:10
  • I have tried adding `-u` parameter, but it just got an error saying it's an invalid parameter. i.e. `nohup java -u -jar -Dspring.profiles.active=uat target/pow-wow-0.0.2-SNAPSHOT.jar &`. People were suggesting that https://stackoverflow.com/questions/12919980/nohup-is-not-writing-log-to-output-file. – Richard Aug 24 '23 at 10:12
  • 1
    That's a python option, not a `nohup` one. So you're passing it to java. It may be a problem with writing permissions in the current directory. Check whether it created `nohup.out` inside the user's home directory instead. As a last ditch effort, try `nohup java -u -jar -Dspring.profiles.active=uat target/pow-wow-0.0.2-SNAPSHOT.jar > nohup.out &`. – Federico klez Culloca Aug 24 '23 at 10:23
  • CodeDeploy executes its own process, and is logging to its own deployment, e.g. /opt/codedeploy-agent/deployment-root/xxxx/yyyy/logs/scripts.log. However, the `deploy.sh` is starting the application, so I would have thought it would output the logs. – Richard Aug 24 '23 at 10:27
  • Thanks, adding `> nohup.out` did create the file: `root root 0 Aug 24 10:33 nohup.out`. However, it is empty. – Richard Aug 24 '23 at 10:43
  • I think the logs cannot be outputted to `nohup.out`. I am going to find a solution using Couldwatch: https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-cloudwatch-agent.html – Richard Aug 24 '23 at 10:47
  • Hi @ Federico klez Culloca, I was wring, the `> nohup.out` works, it is now writing to the file. You did answer my question, thank you. – Richard Aug 24 '23 at 11:04
  • Why not just modify java logging to log to a file instead of the console? – cant-code Aug 26 '23 at 17:13

0 Answers0