2

I have a .NET Core 3.0 Console App, running on a Ubuntu 18.04 server. There is a simple launcher script in my home folder, called my-app.sh:

cd /home/service/my-app
./My-App

I wanted to start the file after reboot, so I created a cronjob for the launcher script, using crontab -e:

@reboot /home/service/my-app.sh

After reboot, MyApp lauches perfectly fine. But, when calling df repeatedly, I noticed that the free disk space of / was continuously decreasing! I tried to find out which file it is, but ncdu did not show any difference over time. I am not writing a file in MyApp, i.e. the total size of /home/service does not increase. After about 10 minutes, the whole free space is gone, and MyApp is quit by the system.

When I cancel MyApp after reboot, I see that the "memory leak" is immediately stopped. But: When I restart MyApp manually, there is no memory leak any more.

Now I tried to remove the cronjob and added the launcher script to the /etc/local.rc file instead:

# Start MyApp
su service -c 'sh /home/service/my-app.sh' &

Again, it started perfectly fine after reboot, and there is also no memory leak any more.

I have no idea at all what could be the problem. There is no problem when starting manually or from /etc/local.rc, but just when starting from the cronjob. Any idea what could be the problem?

Andi
  • 3,234
  • 4
  • 32
  • 37

1 Answers1

1

tl;dr Avoid user interaction in the console app (e.g. ReadLine() waiting for a certain input).

Details: I found out what happened. Here is the last part of the C# code of my console application:

// App can be quit by keyboard input "exit"
while (true) {
    Console.WriteLine("Enter 'exit' to close MyApp.");
    if (Console.ReadLine() == "exit")
        break;
}

Using this code, I wanted to keep the program running until a user inputs exit or until this "service" is killed. Unfortunately, what happens, is that the program, started by Cron or by /etc/rc.local skips the ReadLine() and runs into an endless loop, messing up the memory (and maybe swap on hard drive) with an endless number of Enter 'exit' to close MyApp. lines. While /etc/rc.local seems to free the memory continuously, the cronjob requires more and more memory. No idea why, just an observation!

The solution is simple. I removed the interaction and added the following harmless endless loop, waiting for the process to be killed:

// App can only be stopped by killing the process
Console.WriteLine("Started in service mode. To close MyApp, kill the process.");
while (true) {
    Thread.Sleep(1000);
}
Andi
  • 3,234
  • 4
  • 32
  • 37