Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core application exit code. Different behaviour on Linux (RHEL 7)

I seem unable to set the exit code of a dotnet core console application such that the expected behaviour is observed on RHEL7. I am using dotnet 2.0.3.

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Environment.Exit(12345);
        }
    }
}

On windows, the error level is set correctly when executing the program with

dotnet HelloWorld.dll
echo %errorlevel%
12345

On RHEL7, this is not what is seen

dotnet HelloWorld.dll
echo $?
57

I have tried various ways to return an exit code (setting the Environment.ExitCode property, returning an integer from the program) but have had no success.

I will try a test on the latest dotnet core version (2.1.302) as soon as possible but posting this question in the meantime.

Does anyone know what's going on here?

Thanks

like image 443
Andrew Rushby Avatar asked Jun 10 '26 08:06

Andrew Rushby


1 Answers

I found the problem in the end - it was because on RHEL7, unlike Windows, the exit code/error level is restricted to being an 8 bit integer. I'll need to keep my exit codes in the range [0,255].

like image 185
Andrew Rushby Avatar answered Jun 12 '26 05:06

Andrew Rushby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!