Home > Back-end >  Jenkins Execute shell behaving differently
Jenkins Execute shell behaving differently

Time:01-30

I am creating a Jenkins project which executes a shell on build. Inside the execute shell I am running a python script like `python3 pythonScriptFile.py "${arg1}" "${arg2}" "${arg3}"

the python file internal call a shell script.

python -> shell1 -> shell2 -> return back to python file to continue execution.

when i execute the python file with arguments in terminal the the execution is synchronous one after the other.

but when I run the same in Jenkins first the shell is executed then the python file.

`print("SCRIPT Started")
 process = os.system("""sh script.sh -t {arg1} -e {arg2}""")
 process.wait()
 if process.returncode != 0:
     sys.exit()
     print("Error executing build script")

 print("SCRIPT COMPLETED")`

Output:

Script executed (which is a echo inside shell)
SCRIPT Started
SCRIPT COMPLETED`

Expected Output:

SCRIPT Started
Script executed (which is a echo inside shell)
SCRIPT COMPLETED`

CodePudding user response:

[ Why does that happen ? ]

The buffering of a standard output stream depends on the environment and program settings.

In Jenking the output stream of python program is fully buffered, while interactive program connected to a terminal is line buffered.

[ How to fix it ? ]

Disable output buffering

CodePudding user response:

It appears that the issue is with the way you're running the shell script inside the python script. The os.system() function runs the command specified as a string in a shell, and returns the exit status of that command. However, it does not wait for the command to complete before continuing execution of the python script.

To make the python script wait for the shell script to complete before continuing, you can use the subprocess module instead of os.system(). The subprocess.run() function allows you to run a command and wait for it to complete, and also gives you access to the output and return code of the command.

Here is an example of how you can modify your code to use subprocess.run():

import subprocess

print("SCRIPT STARTED")
process = subprocess.run(["sh", "script.sh", "-t", arg1, "-e", arg2])
if process.returncode != 0:
    sys.exit("Error executing build script")

print("SCRIPT COMPLETED")

This should run the shell script and wait for it to complete before continuing execution of the python script, and you will see the expected output.

  • Related