Run a background process permanently on a node through a script on Jenkins and let Jenkins build successfully

后端 未结 2 897
孤街浪徒
孤街浪徒 2020-12-20 18:08

I am running a background process through a script , this script is invoked when Jenkin starts building. However, the jenkins build gets stuck and on looking at the console

2条回答
  •  礼貌的吻别
    2020-12-20 18:56

    There is a trick you can do in order you free a Jenkins thread.

    What you can do is to execute a bash script through a ssh connection and send it to the background while saving the pid of the process somewhere so you can make checks further.

    The format of the command would be:

    ssh -n _hostname_ "_commands_ & echo \$! > \"_path_to_pid_file_\"" &
    

    Example with a never-ending program:

    ssh -n myhost.domain.com "tail -f /var/log/my.log & echo \$! > \"$WORKSPACE/pid\"" &
    

    This example will spawn the tail process listening forever for new changes in the /var/log/my.log file and store its pid in the $WORKSPACE/pid file.

    When executed from a Jenkins job the ssh process will exit immediately while the commands sent to the background will remain in execution in the specified host.

    I do this in order to maintain always one of the services I run in my build farm in-sync with the latest code modification of it in the repository. Just have a job that ssh' into the target machine and then kill the process, update the service and re-launches it.

    This could be a bit cumbersome but it works great!

提交回复
热议问题