question

Prabs-7622 avatar image
0 Votes"
Prabs-7622 asked BruceBarker-8516 answered

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

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



dotnet-csharpdotnet-runtime
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork edited

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Prabs-7622 avatar image
0 Votes"
Prabs-7622 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BruceBarker-8516 avatar image
0 Votes"
BruceBarker-8516 answered

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

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.