Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab a particular log file and show its content in jenkins Console output

I have the following Jenkins post-build shell script:

ssh user@my_server <<EOF
  service my_service stop
  service my_service start
  tail -f /opt/services/my_service/logs/current
  exit
EOF

This script restarts my_service on a remote host (my_server).

My problem is: command service my_service start just makes a request to RUNIT to run a my_service, i.e service my_service start returns immediately after execution.

But service my_service start runs a SpringBoot java web application that writes all log info into .../logs/current log file. To catch this log info I've added command tail -f /opt/services/my_service/logs/current but in this case Jenkins build is never ends)) such as tail -f command never stops.

Is there a way to execute my post-build script (which only start my web app on a remote server) and grabbing the .../logs/current log file during 2 minutes or until this log has the line "Web app MyApplication has been Started".

I wanna see the content of .../logs/current log file right in Jenkins's Console output and kill tail -f after 2 minutes

like image 971
Sb Lon Avatar asked Dec 25 '15 21:12

Sb Lon


People also ask

How can we get the Jenkins console output in a text file?

You have to build the file name by taking the JOB_URL, stripping off the leading host name part, adding in the path to JENKINS_HOME, replacing "/job/" to "/jobs/" to handle all nested folders, adding the current build number and the file name.

Where are Jenkins console logs stored?

Log files should be at /var/log/jenkins/jenkins. log , unless customized in org. jenkins-ci. plist .

How do I download Jenkins job console output?

In order to read the console output in Jenkins, All you need to do is copy and paste the code as a stage in your pipeline script. After the build is run, a file named buildConsolelog. txt will be stored in the home directory of your project.


1 Answers

tail -f will not end until it gets interrupted, so your script will never finish running.

what you can do is use grep -q on your log, which will exit with 0 exit status when it finds it's pattern:

grep -q 'Web app MyApplication has been Started' <(tail -f /opt/services/my_service/logs/current)
like image 127
Nir Levy Avatar answered Nov 07 '22 17:11

Nir Levy