- 나는 완전한 C# 초보입니다(비록 Java를 알고 있지만).
- C# 스크립트를 수정했습니다. 시작 시 데몬과 systemctl을 사용하여 실행하고 싶었습니다(원래는NZXT 자동 팬 컨트롤러 스크립트)
- 이 코드는 4초마다 코드 조각을 실행하고 다음을 사용하여 종료 스크립트를 차단하는 반복 타이머를 생성합니다.
Console.ReadLine();
- msc와 mono를 사용하여 스크립트를 컴파일하고 테스트했습니다. 터미널에서 예상대로 작동합니다(직접 종료되지 않음).
- 데몬으로 실행하면
Console.ReadLine();
대기 시간이 없으며 스크립트가 기본 메소드를 직접 완료하고 종료됩니다. 노트:
- 저도 Linux moob이므로 데몬에서 뭔가 잘못하고 있는 경우 지적해 주시기 바랍니다. 내 목표는 부팅 시 시스템을 시작하고 중지할 수 있는 백그라운드 스크립트를 만드는 것입니다.
- 아래에 첨부된 모든 코드를 찾으세요.
주요 코드:
using System;
using System.IO;
using System.Timers;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Text.RegularExpressions;
public class Program
{
private static string root;
private static string confFile = "/home/mohammadah/scripts/grid+v2/conf.d";
private static System.Timers.Timer aTimer;
private static int maxSpeed = 100;
private static int minSpeed = 35;
// do not change this. all fan speeds have to rounded to the nearest multiple of 5
private static int minimumRound = 5;
private static int timerTime = 4000;
private static int defaultFanSpeed = 40;
// fans:
// 1: empty
// 2: empty
// 3: bottom
// 4: top
// 5: CPU
// 6: Back
private static int[] gpuFans;
private static int gpuMaxTemp;
private static int gpuMinTemp;
private static int[] cpuFans;
private static int cpuMaxTemp;
private static int cpuMinTemp;
private static float lastCPUSpeed = 0;
private static float lastGPUSpeed = 0;
public static void Main()
{
// Console.WriteLine(System.IO.Path.GetDirectoryName(Application.ExecutablePath));
ParseConfigFile(confFile);
if(!Regex.IsMatch(DoGridCommand("get fan 1"), @"Fan 1: \d{1,5} RPM")){
DoGridCommand("init");
}
DoGridCommand("set fans all speed " + defaultFanSpeed);
SetTimer();
Console.WriteLine("\nPress the Enter key to exit the application...\n");
// Process.GetCurrentProcess().WaitForExit();
Console.ReadLine();
aTimer.Stop();
aTimer.Dispose();
Console.WriteLine("Terminating the application...");
}
private static float GetGPUTemp(){
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c nvidia-smi -q -d temperature");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
char first = '-';
char second = '-';
string temp = "";
foreach (char c in result)
{
if(c=='1' || c == '2'|| c == '3'|| c == '4'|| c == '5'|| c == '6'|| c == '7'|| c == '8'|| c == '9'|| c == '0'|| c == 'C'){
if(first == '-'){
first = c;
}else if(second == '-'){
second = c;
}else if(c == 'C'){
temp = "" + first + second;
break;
}
}else{
first = '-';
second = '-';
}
}
return float.Parse(temp);
}
private static int GetCPUTemp(){
string tempLineStart = "Packageid0:+";
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c sensors");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
int temp = 0;
string line = "";
while ((line = proc.StandardOutput.ReadLine()) != null) {
line = Regex.Replace(line, @"\s+", "");
if(Regex.IsMatch(line, tempLineStart)){
line = line.Trim().Replace(tempLineStart.Trim(), "");
line = line.Substring(0, 6);
line = Regex.Replace(line, "[^0-9.]", "");
float t = float.Parse(line);
temp = (int)t;
break;
}
}
return temp;
}
private static void SetTimer()
{
// Create a timer with a two second interval.
aTimer = new System.Timers.Timer(timerTime);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
private static void ParseConfigFile(string fileName){
string gridfanLocation = "GridfanLocation:";
string gpuFanLineStart = "GPUFans:";
string gpuMaxLineStart = "GPUMaxTemp:";
string gpuMinLineStart = "GPUMinTemp:";
string cpuFanLineStart = "CPUFans:";
string cpuMinLineStart = "CPUMinTemp:";
string cpuMaxLineStart = "CPUMaxTemp:";
StreamReader reader = File.OpenText(fileName);
string line;
while ((line = reader.ReadLine()) != null) {
line = line.Trim();
// Regex.Replace(line, @"\s+", "");
// ignore commented out (starts with "//") or empty lines
if(line.StartsWith("//") || line.Equals("")) continue;
// set gpu temp at which gpu fans will be set at 100% speed
else if(Regex.IsMatch(line, gridfanLocation)){
root = line.Trim().Replace(gridfanLocation, "");
// set gpu fan list using the numbering found on gridfan git repo
}else if(Regex.IsMatch(line, gpuFanLineStart)){
line = line.Trim().Replace(gpuFanLineStart, "");
string[] fans = line.Split(",");
gpuFans = new int[fans.Length];
for (int i =0;i<fans.Length;i++){
gpuFans[i] = int.Parse(fans[i].Trim());
}
// set gpu temp at which gpu fans will be set at 100% speed
} else if(Regex.IsMatch(line, gpuMaxLineStart)){
line = line.Trim().Replace(gpuMaxLineStart, "");
gpuMaxTemp = int.Parse(line);
// set gpu temp at which gpu fans will be set at lowest speed
} else if(Regex.IsMatch(line, gpuMinLineStart)){
line = line.Trim().Replace(gpuMinLineStart, "");
gpuMinTemp = int.Parse(line);
// set cpu fan list using the numbering found on gridfan git repo
} else if(Regex.IsMatch(line, cpuFanLineStart)){
line = line.Trim().Replace(cpuFanLineStart, "");
string[] fans = line.Split(",");
cpuFans = new int[fans.Length];
for (int i =0;i<fans.Length;i++){
cpuFans[i] = int.Parse(fans[i].Trim());
}
// set cpu temp at which gpu fans will be set at 100% speed
} else if(Regex.IsMatch(line, cpuMaxLineStart)){
line = line.Trim().Replace(cpuMaxLineStart, "");
cpuMaxTemp = int.Parse(line);
// set cpu temp at which gpu fans will be set at lowest speed
} else if(Regex.IsMatch(line, cpuMinLineStart)){
line = line.Trim().Replace(cpuMinLineStart, "");
cpuMinTemp = int.Parse(line);
}
else throw new Exception ("Something went wrong while parsing config file \"./conf.d\"");
}
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Console.WriteLine("====================================================");
float gpuTemp = GetGPUTemp();
Console.WriteLine("Graphics (GPU) temp: " + gpuTemp+"`C");
float gpuPercentage = 100 * ((gpuTemp - gpuMinTemp) / (gpuMaxTemp - gpuMinTemp));
Console.WriteLine("Graphics (GPU) percentage: " + gpuPercentage+"`%");
float gpuFanSpeed = gpuPercentage > maxSpeed ? maxSpeed : gpuPercentage < minSpeed ? minSpeed : gpuPercentage;
gpuFanSpeed = (int) Math.Ceiling(gpuFanSpeed / minimumRound) * minimumRound;
if(lastGPUSpeed != gpuFanSpeed){
if(!Regex.IsMatch(DoGridCommand("get fan 1"), @"Fan 1: \d{1,5} RPM")){
DoGridCommand("init");
}
foreach (int gpuFan in gpuFans){
DoGridCommand("set fans " + gpuFan + " speed " + gpuFanSpeed);
}
}
float cpuTemp = GetCPUTemp();
Console.WriteLine("Central (CPU) temp: " + cpuTemp+"`C");
float cpuPercentage = 100 * ((cpuTemp - cpuMinTemp) / (cpuMaxTemp - cpuMinTemp));
Console.WriteLine("Central (CPU) percentage: " + cpuPercentage+"`%");
Console.WriteLine("");
float cpuFanSpeed = (cpuPercentage > maxSpeed ? maxSpeed : cpuPercentage < minSpeed ? minSpeed : cpuPercentage);
cpuFanSpeed = (int) Math.Ceiling(cpuFanSpeed / minimumRound) * minimumRound;
if(lastCPUSpeed != gpuFanSpeed){
if(!Regex.IsMatch(DoGridCommand("get fan 1"), @"Fan 1: \d{1,5} RPM")){
DoGridCommand("init");
}
foreach (int cpuFan in cpuFans){
DoGridCommand("set fans " + cpuFan + " speed " + cpuFanSpeed);
}
}
lastCPUSpeed = cpuFanSpeed;
lastGPUSpeed = gpuFanSpeed;
Console.WriteLine("====================================================");
Console.WriteLine();
}
private static string DoGridCommand(string command){
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", root + "/gridfan " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Console.WriteLine("Command result: " + result.Trim());
// System.Threading.Thread.Sleep(500);
return result;
}
}
메인 코드에 사용되는 conf.d (매우 중요하지는 않지만 완성도를 위해):
// gridFan repo = https://github.com/CapitalF/gridfan
// root of gridFan script location
GridfanLocation:/home/mohammadah/scripts/grid+v2
// GPU fans id/ports as per the schema in gridFan repo readme
GPUFans: 3
// GPU temp at which GPU fans will be set at 100% speed
GPUMaxTemp: 75
// GPU temp at which GPU fans will be set at lowest speed
GPUMinTemp: 35
// GPU fans id/port as per the schema in gridFan repo readme
CPUFans: 4, 5, 6
// CPU temp at which CPU fans will be set at 100% speed
CPUMaxTemp: 75
// CPU temp at which CPU fans will be set at lowest speed
CPUMinTemp: 35
daemon.service 파일
[Unit]
Description=Grid+ v2 automatic fan controller
[Service]
ExecStart=/usr/bin/mono /home/mohammadah/scripts/grid+v2/LinuxGrid-v2Controller.exe
Restart=on-failure
[Install]
WantedBy=multi-user.target
답변1
은 모르겠지만 c#
문제는 systemd 서비스로 시작할 때 프로그램의 STDIN이 에 연결되어 있기 때문에 읽기를 시도 /dev/null
하면 Console.ReadLine();
즉시 결과(0바이트 읽기)를 얻고 반환하기 때문이라고 생각됩니다.
다음과 같은 방법으로 동일한 것을 수동으로 재현할 수 있습니다.
myprog < /dev/null
프로그램이 종료되지 않도록 하려면 이를 수행하는 다른 메커니즘이 필요합니다.