Run Bash script using WSL

How to run bash script, with script block using WSL?

Without construction like bash -c "my script", because in this case I will have to call it every line.

And without using "Executable with parameters", in this case I will have to separate all scripts by files, but this is not very good for my case.

16
6 comments

Hello,

Is your script for windows or for a unix like environment? Using Powershell you can use script block by using Powershell in the runner type and setting the script from source code:

 

If you are using a script for an unix-like environment, can you share an example with us via our upload service and send the id back to us so that we check what might be going wrong?

https://uploads.jetbrains.com/

Please let me know if this works for you or if you have any other questions.

Thanks,

Guilherme

 

 

0

Hello, my is script is for unix-like system. But agent run on windows. I need run bash script on windows machine using WSL.

0

Hello,

It should be possible to do it using the command line runner as in your screenshot above. You can try to prepare a script, set its path in the working directory and run the WSL executable passing the script to it.

Let me know if you are having any specific errors

Thank you,

Guilherme

1

Hi,

I'm currently also trying to have my windows agent run a bash script using WSL. Although the original author of this post did not report any problems, I'd like to share my issue here in hopes of finding a solution.

WSL works great on the Windows agent locally and the bash script executes as it should, however during a TeamCity build the WSL calls are problematic:

  1. All WSL commands given as a command line runner scripts produce no output, but %errorlevel% is set to 0:
    wsl --help
    wsl echo foo
    No output in build log
  2. Attempting to run a bash script using WSL produces no output and the following error in build log can be seen:
    Starting: C:\opt\BuildAgent\temp\agentTmp\custom_scriptxxxxxxxxxxxxxxxxx.cmd
    in directory: C:\opt\BuildAgent\work\xxxxxxxxxxxxxx
    echo foo
    Process exited with code -1073740791

After some research, I found that this may be caused by the TeamCity agent using a 32 bit version of java and attempting to run WSL which is 64 bit. This turned out to not be the case, as I could see the java that came bundled with the TeamCity agent installer is in fact 64 bit.

Digging deeper into Windows logs, I could see the error with the exception code logged. The exception code hex (0x0000409) matches the exit code seen in TeamCity build log. I've tried looking into this exception code more but with no success. The most common solution I could find online was running sfc /scannow on the Windows machine but it did not help.

Any and all help is much appreciated!

0

I solved my issue, hopefully this solution helps others!
Digging through the TeamCity documentation, I noticed this page https://www.jetbrains.com/help/teamcity/start-teamcity-agent.html.
There it states the following:

To run builds, the build agent must be started under a user with sufficient permissions for performing a build and managing the service. By default, a Windows service is started under the SYSTEM account which is not recommended for production use due to extended permissions this account has. To change it, use the standard Windows Services applet (Control Panel | Administrative Tools | Services) and change the user for the TeamCity Build Agent service

I proceeded to change the TeamCity Build Agent service's LogOn user account to a local user account and voila! The issue was solved. WSL now runs perfectly and the output is produced and logged as expected.

Cheers, I hope this helps someone!

 
0

Kajusn stated, this was one thing that kept wsl from starting, another thing that needs to be checked is the java version the agent is using, it must be 64 bit to be able to run in wsl.
I used jdk-18.0.2 64 bit to solve this.

0

Please sign in to leave a comment.