Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-60593

Unable to run a background process using sshCommand

      I'm trying to run the below command as background process and it seems successful, but it doesn't create any log file mentioned in the command.

      sshCommand remote: remote, failOnError: true, command: 'sar 2 10 -r > sar-mem.log &', sudo: true

          [JENKINS-60593] Unable to run a background process using sshCommand

          Can you debug more and provide some inputs, like can you try run that command without sudo and see if that works, or try some other command and provide more information?

          Naresh Rayapati added a comment - Can you debug more and provide some inputs, like can you try run that command without sudo and see if that works, or try some other command and provide more information?

          Closing as there isn't much activity on this PR. Please reopen if you are still facing this issue and have more information. Thank you for logging an issue.

          Naresh Rayapati added a comment - Closing as there isn't much activity on this PR. Please reopen if you are still facing this issue and have more information. Thank you for logging an issue.

          Madan Kv added a comment -

          I'm trying to run below and it is not working 

          sshCommand remote: remote, failOnError: true, command: 'pbrun java -jar /home/users/km/Utils-0.0.1.jar 20 & ' , sudo: false

          Madan Kv added a comment - I'm trying to run below and it is not working  sshCommand remote: remote, failOnError: true, command: 'pbrun java -jar /home/users/km/Utils-0.0.1.jar 20 & ' , sudo: false

          Madan Kv added a comment -

          Reopening.

          Madan Kv added a comment - Reopening.

          Madan Kv added a comment -

          I'm trying to run below and it is not working 

          sshCommand remote: remote, failOnError: true, command: 'pbrun java -jar /home/users/km/Utils-0.0.1.jar 20 & ' , sudo: false

          Madan Kv added a comment - I'm trying to run below and it is not working  sshCommand remote: remote, failOnError: true, command: 'pbrun java -jar /home/users/km/Utils-0.0.1.jar 20 & ' , sudo: false

          David added a comment - - edited

          I can reproduce this. I run:

          sshCommand remote: remote, pty: true, command: 'nohup java -jar demo.jar > nohup.out &'

           And the Jenkins job runs successfully, but the Java jar is not running. I can run the command manually on the server though.

          If I remove the &, it runs the Java jar successfully, but the command obviously never completes because we're not running it in the background, so the Jenkins job hangs on that step.

          I also tried putting the command in a script file and running it with sshCommand, but it has the same problem.

          For what it's worth, I was able to run this command using a different plugin, the Publish Over SSH plugin:

          sshTransfer(
            execCommand: 'nohup java -jar demo-0.0.1-SNAPSHOT.jar > nohup.out &',
            removePrefix: 'target',
            sourceFiles: 'demo.jar'),

           So I could get the desired behaviour by mixing and matching the plugins, but that's not ideal. The SSH Steps plugin has better support for sudo.

          David added a comment - - edited I can reproduce this. I run: sshCommand remote: remote, pty: true , command: 'nohup java -jar demo.jar > nohup.out &'  And the Jenkins job runs successfully, but the Java jar is not running . I can run the command manually on the server though. If I remove the & , it runs the Java jar successfully, but the command obviously never completes because we're not running it in the background, so the Jenkins job hangs on that step. I also tried putting the command in a script file and running it with sshCommand , but it has the same problem. For what it's worth, I was able to run this command using a different plugin, the Publish Over SSH plugin: sshTransfer( execCommand: 'nohup java -jar demo-0.0.1-SNAPSHOT.jar > nohup.out &' , removePrefix: 'target' , sourceFiles: 'demo.jar' ),  So I could get the desired behaviour by mixing and matching the plugins, but that's not ideal. The SSH Steps plugin has better support for sudo .

          David added a comment -

          OK, I got something working.

          I have a script file on the remote server with this:

          nohup java -jar demo.jar > nohup.out &
          sleep 5
          

          It's the sleep 5 that fixed it for me. Now in my pipeline script I have:

          sshPut remote: remote, from: 'target/demo.jar', into: 'demo.jar'
          sshCommand remote: remote, command: './script.sh'
          

          This works. I guess maybe the sshCommand finishes so fast it closes the connection before nohup can really start? Just a guess, I'm not a Linux guy.

          I also tried sshCommand remote: remote, command: 'nohup java -jar demo.jar > nohup.out & && sleep 5', but that didn't work, and neither did nohup java -jar demo.jar > nohup.out & ; sleep 5. In both cases it results in a "Failed command error".

          David added a comment - OK, I got something working. I have a script file on the remote server with this: nohup java -jar demo.jar > nohup.out & sleep 5 It's the sleep 5 that fixed it for me. Now in my pipeline script I have: sshPut remote: remote, from: 'target/demo.jar' , into: 'demo.jar' sshCommand remote: remote, command: './script.sh' This works. I guess maybe the sshCommand finishes so fast it closes the connection before nohup can really start? Just a guess, I'm not a Linux guy. I also tried sshCommand remote: remote, command: 'nohup java -jar demo.jar > nohup.out & && sleep 5' , but that didn't work, and neither did nohup java -jar demo.jar > nohup.out & ; sleep 5 . In both cases it results in a "Failed command error".

            nrayapati Naresh Rayapati
            kmadan Madan Kv
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: