1

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`
Abhishek
  • 39
  • 3
  • 14
  • 1
    This is a common FAQ, though I find it hard to locate a proper canonical. See e.g. https://stackoverflow.com/questions/70718720/why-is-the-output-order-different-from-the-call-order-when-using-fprintf-with-st or https://stackoverflow.com/questions/64128283/order-of-events-in-bash-shell-redirection-of-standard-output-and-error-to-the-sa – tripleee Jan 23 '23 at 09:17

1 Answers1

4

[ 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

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Buffering may be the case in here, but if the second script runs longer you could be having real problems. The subprocess answer is better. – WombatPM Jan 30 '23 at 05:39