我使用的是RaspberryPi (3B+)Linux ARM IOT板,操作系统是Debian Stretch 9,我的控制台应用程序是在.NET CORE 2.1上开发的。
我的应用程序非常简单,只需打开几个到远程服务器的TCP连接,在构建我的应用程序(使用符号Linux ARM
)之后,我就可以看到输出文件包括myApp
和myApp.dll
。我已经通过直接命令行运行了很多次:
pi@raspberrypi:~/Desktop/myApp $ ./myApp
或:
pi@raspberrypi:~/Desktop/myApp $ dotnet ./myApp.dll
两者运行良好,通过
top
的CPU(进程名为myApp
,后者为dotnet
)都小于20。今天我想把我的应用程序添加到daemon中,以便一直运行,这是
/etc/systemd/system
下的daemon serivce文件:[Unit]
Description=myApp for controlling Tcp devices
[Service]
WorkingDirectory=/home/pi/Desktop/myApp
#
ExecStart=/usr/local/bin/dotnet myApp.dll
Restart=always
# Restart service after 10 seconds if this service crashes:
RestartSec=10
SyslogIdentifier=myApp
# User=pi
[Install]
WantedBy=multi-user.target
在通过
enable, start
命令执行服务之后,我可以看到应用程序正在通过systemctl
运行(进程名为top
),但是现在CPU相当高(对于进程dotnet
),超过100。你知道CPU是如何提升的吗?有没有办法让我的进程名保持在原来的位置而不是
dotnet
? 最佳答案
最后我发现原因是Main
中的代码:
static void Main(string[] args)
{
//my business logic code
//balabala
while (true)
Console.ReadLine();
}
这意味着在deamon模式下,
Console.ReadLine()
总是可以读取一个空间并导致一个无限循环,这个循环消耗了CPU,我不确定这个空间是怎么来的,是不是.NET核心bug?