1

I'm trying to capture the output of the startup script of Jenkins, but all I get is an empty output.

The output of Jenkins appears in the terminal

#!/bin/bash
function sendNotificationOnSlack() {
    curl -X POST ...
}
export -f sendNotificationOnSlack
java -Dhudson.DNSMultiCast.disabled=true -jar /usr/local/opt/jenkins/libexec/jenkins.war --httpPort=3999 | awk '/Jenkins is fully up and running/ {system("bash -c sendNotificationOnSlack")}'

I've tried even to capture the output of the TTY, but appears as empty

#!/bin/bash
currentTTY=$(tty)
echo $currentTTY
function sendNotificationOnSlack() {
    curl -X POST ...
}
export -f sendNotificationOnSlack
java -Dhudson.DNSMultiCast.disabled=true -jar /usr/local/opt/jenkins/libexec/jenkins.war --httpPort=3999
tail -f $currentTTY | awk '/Jenkins is fully up and running/ {system("bash -c sendNotificationOnSlack")}'

I tried to search everywhere but I can't find the jenkins.log file, anyone has any suggestion?

  • I'm not entirely sure about the problem, but if you must capture the output, maybe you should have Jenkins spawn in one process and capture the log from another. That said, maybe you should `tee` the outputs [like so](http://stackoverflow.com/questions/692000/how-do-i-write-stderr-to-a-file-while-using-tee-with-a-pipe) into files and process from there, removing the piping into `awk`. `awk` can be used on the files written by `tee` – alok Nov 04 '16 at 13:13
  • Also, I'm assuming that you do not want the logs falling inside `/var/log/jenkins/jenkins.log` right? – alok Nov 04 '16 at 13:18
  • @alok Thanks for the reply. The problem is that the output doesn't get captured, and even if piped `awk` is displayed on the shell ( a sign that isn't redirected but is still going to the same place). I've already checked on `/var/log/jenkins/` and the folder is empty – Maurizio Carboni Nov 04 '16 at 13:28
  • When output is piped to awk, it's just the stdout. If you're sure that the Jenkins/Java process has spawned, you could retry running with `2>&1` to redirect stderr into stdout and that into your awk. I say this because most Java servers spit logs on stderr or in Java terms: `System.err` – alok Nov 05 '16 at 13:11

1 Answers1

2

Create the following groovy file: $JENKINS_HOME/init.groovy.d/extra-logging.groovy

Restart Jenkins and check directory $JENKINS_HOME/logs/

import java.util.logging.ConsoleHandler
import java.util.logging.FileHandler
import java.util.logging.SimpleFormatter
import java.util.logging.LogManager
import jenkins.model.Jenkins

def logsDir = new File(Jenkins.instance.rootDir, "logs")

if(!logsDir.exists()){
    println "--> creating log dir";
    logsDir.mkdirs();
}

def loggerWinstone = LogManager.getLogManager().getLogger("");
FileHandler handlerWinstone = new FileHandler(logsDir.absolutePath + "/jenkins-winstone.log", 1024 * 1024, 10, true);

handlerWinstone.setFormatter(new SimpleFormatter());
loggerWinstone.addHandler (new ConsoleHandler());
loggerWinstone.addHandler(handlerWinstone);