How to run .Net Core 3.1 Console application in background in Linux

Prabs 1 Reputation point
2021-07-29T06:51:41.197+00:00

Hi Team,

I have a C# .NET Core 3.1 console application which is running on terminal window in RedHat Linux.
It is a long running application. it runs 24/7. Currently application is blocking the terminal window when it is running.

Now i got new change request which i need to implement

Change Request

The application has to run on background by following way in Linux terminal window,
Linux terminal window should not be blocked.

The commands are like below,

START appName
STOP appName
STATUS appname

Could you please help me on this?

Regards,
Prabs

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,193 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 54,711 Reputation points
    2021-07-29T16:10:12.457+00:00

    linux does not have the equivalent of nt services. you have cron jobs, or daemons. cron redirects stdin, stdout and stderr before it starts the job.

    linux does not have a create process like nt. instead it has a fork() which creates an exact copy of the code running in memory. the return value of fork() tells the caller if it the parent or the child after the fork().

    to start a process, your code does a fork(), then the child process reassigns files handles as desired and loads a new image. the new image inherits the open file handles.

    to stop a program you send it a signal(9). the kill command can do this. typically ^c is converted to signal(9).

    while you can code your program to auto detach, usually shell programs are used to start a process, they usually have a command to start a job in the background (typically & at end). they also have a command to list jobs, and attach to a job and kill a job

    bash>my program&
    bash>jobs

    see the ^c, ^z, fg, bg, jobs commands for the shell of your choice.

    for status, the program typically writes to a log file, and you use the tail command to get the current status.

    so if your .net command tool is name foo, then

    ! start foo in background
    foo > foo.log &

    ! get status
    tail foo.log

    ! stop foo
    pkill foo

    0 comments No comments

  2. Prabs 1 Reputation point
    2021-08-03T07:39:43.617+00:00

    How to handle the below commands in C#.Net core console application?

    ! start foo in background
    foo > foo.log &

    ! get status
    tail foo.log

    ! stop foo
    pkill foo

    0 comments No comments

  3. Bruce Barker 801 Reputation points
    2021-08-03T14:45:09.18+00:00

    Why write a c# program instead of a simple shell script?

    You use Process.Start() to run a utility.

    0 comments No comments