Search code examples
linuxjenkinssystemd

returnStdout doesn't work when jenkins agent is started as a systemd service


I have created the below unit file to create a systemd service on linux. This file launches jenkins agent on the machine. I have a stage in my pipeline in which the slave machine will be rebooted. Creating a service that starts after reboot was the solution that I was able to come up with, so that my pipeline execution can continue after reboot.

[Unit]
Description=Run jenkinsAgent at startup
After=network.target

[Service]
WorkingDirectory=/home/qatest/jenkins
Type=simple
ExecStart=/bin/bash /home/qatest/jenkins/jenkinsAgent.sh
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target

My pipeline (groovy) uses hostName = sh(script: "hostname", returnStdout: true). But when the jenkins agent is started as a systemd service and the job is run, hostName is returned as empty.

If I start the jenkins agent manually in a shell using sudo ./jenkinsAgent.sh, then the sh works fine for the job run. ( my user is in the sudoers list ).

I initially thought that the std out of the systemd service was not directed properly, so I tried StandardOutput=journal+console in the unit file, but to no avail.

What am I missing here ?


Solution

  • I modified the unit file as shown below and the issue got resolved.

    [Unit]
    Description=Run jenkinsAgent at startup
    After=network.target
    
    [Service]
    Type=simple
    ExecStart=/usr/bin/java -jar /home/qatest/jenkins/agent.jar -jnlpUrl http://.../slave-agent.jnlp -secret ... -workDir "/home/qatest/jenkins"
    
    [Install]
    WantedBy=multi-user.target