用C#写一个多进程监控自动关机工具
因为据说某server开着就很贵,所以我们跑完测试的job后就要赶紧关机才行,但是测试的job要跑很久,过程中又不需要干什么,所以就得有个守家的,有时候会走很晚。如果有一个自动化关机的工具就好了,当指定的进程结束了以后系统就会自动关机。这件事我在上一篇中已经做好了。这一次领导又有新需求,说要监控多个进程而不单单是一个了,需要有一个配置文件来配置所需要监控的进程名,而且想要可以自主选择检查的间隔,于是就有了下文。

代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.IO; namespace AutoShutDown2
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
} private void chooseFileButton_Click(object sender, EventArgs e)
{
OpenFileDialog fileName = new OpenFileDialog();
fileName.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
if (fileName.ShowDialog() == DialogResult.OK)
{
filePath.Text = fileName.FileName;
}
} private void filePath_Click(object sender, EventArgs e)
{
filePath.Text = "";
} private void startButton_Click(object sender, EventArgs e)
{
if (filePath.Text.ToString().Substring(filePath.Text.Length - , ) == "txt")
{
if (Regex.IsMatch(duration.Text, "^([0-9]{1,})$"))
{
if (int.Parse(duration.Text) >= )
{
MessageBox.Show("PCAS will check with a duration of " + duration.Text + "s.");
this.Hide();
//Check the processes with the duration.
DurationStart();
}
else
{
MessageBox.Show("The integer number should be greater than 10 seconds.");
}
}
else
{
MessageBox.Show("You can only type in an integer for duration.");
duration.Text = "";
}
}
else
{
MessageBox.Show("You can only choose a txt to be a configuration file.");
filePath.Text = "";
}
} private void DurationStart()
{
//Check the process's status with the duration.
System.Timers.Timer tmr = new System.Timers.Timer(int.Parse(duration.Text)*);
tmr.Elapsed += new System.Timers.ElapsedEventHandler(CheckProcess);
tmr.AutoReset = true;
tmr.Enabled = true;
} private void CheckProcess(object source, System.Timers.ElapsedEventArgs e)
{
//Check the processes's status in the config file.
FileStream fs = new FileStream(filePath.Text, FileMode.Open);
StreamReader sr = new StreamReader(fs);
string line;
int numOfTheProcesses = ;
while ((line = sr.ReadLine()) != null)
{
var processes = System.Diagnostics.Process.GetProcesses();
foreach (var process in processes)
{
if (process.ProcessName == line)
{
//Find the objective process.
//MessageBox.Show(line);
numOfTheProcesses++;
}
}
}
if (numOfTheProcesses == )
{
//No such process, shut down the computer.
//MessageBox.Show("The computer is ready to be shut down.");
//Shut down the computer
ShutDown();
}
sr.Close();
fs.Close();
} private void ShutDown()
{
//Shut down the computer.
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.StandardInput.WriteLine("shutdown -s -t 0");
}
}
}
你需要输入一个大于10的整数,并且填写的路径一定要是一个txt文本。否则会给予提示。
配置文件中的配置是这样的,每一行填写一个要监控的进程名:

路径和时间间隔填写正确以后点击Start就会开始按你设定的时间间隔自动监控配置文件中的所有进程,等到所有进程都停止后系统就会自动关机。
用C#写一个多进程监控自动关机工具的更多相关文章
- 多进程监控自动关机工具升级远程关闭多台server——C# works with PowerShell
之前给单位做过一个多进程监控的自动关机工具,详见那篇blog. 这次领导又加了需求,说要等进程监控结束后,不止需要关闭主控端server,还需要关闭其他servers.于是就用到了我上篇文章所介绍的知 ...
- java 写一个JSON解析的工具类
上面是一个标准的json的响应内容截图,第一个红圈”per_page”是一个json对象,我们可以根据”per_page”来找到对应值是3,而第二个红圈“data”是一个JSON数组,而不是对象,不能 ...
- 利用 Python 写一个颜值测试小工具
我们知道现在有一些利用照片来测试颜值的网站或软件,其实使用 Python 就可以实现这一功能,本文我们使用 Python 来写一个颜值测试小工具. 很多人学习python,不知道从何学起.很多人学习p ...
- 通过npm写一个cli命令行工具
前言 如果你想写一个npm插件,如果你想通过命令行来简化自己的操作,如果你也是个懒惰的人,那么这篇文章值得一看. po主的上一篇文章介绍了定制自己的模版,但这样po主还是不满足啊,项目中我们频繁的需要 ...
- 基于 vite2 + Vue3 写一个在线帮助文档工具
提起帮助文档,想必大家都会想到 VuePress等,我也体验了一下,但是感觉和我的思路不太一样,我希望的是那种可以直接在线编辑文档,然后无需编译就可以直接发布的方式,另外可以在线写(修改)代码并且运行 ...
- 写一个nginx监控日志
下面的代码是实现一个nginx监控日志功能,是不是很好玩呢.
- 用 Python 写一个多进程兼容的 TimedRotatingFileHandler
我前面有篇文章已经详细介绍了一下 Python 的日志模块.Python 提供了非常多的可以运用在各种不同场景的 Log Handler. TimedRotatingFileHandler 是 Pyt ...
- 10min 手写一个内存监控系统
本文的目的在于,尽可能用简单的代码,让大家了解内存监控的原理,及思想.更容易去理解Nagios.Zabbix.Ganglia监控原理,文章最后还有视频教程链接哦,从零敲出来的全过程 思路分为下面几块: ...
- 教你写一个web远程控制小工具
惯例先上图 晚上躺床上了,发现忘关电脑了,又不想起来关,来用手机控制电脑多好,百度了下,果然一大把.哈,我自己为什么不自己也实现个呢,任意的自己diy.Just do it. 如果不想看如何实现,那么 ...
随机推荐
- eclipse Java代码折叠工具
eclipse Java代码折叠工具 CreateTime--2018年5月17日15点09分 Author:Marydon 1.问题描述 eclipse自带的代码折叠工具,无法折叠try{}ca ...
- 如何在MyEclipse中将项目部署Tomcat
如果系统里面还没有配置Tomcat,请参照<Windows下如何配置tomcat环境变量>. 1.新建一个web project项目.如下: 效果如下: 2.编写一个实现Servlet接口 ...
- 揭开JDBC的神秘面纱,让JDBC数据库的连接参数不再神秘
1.JDBC是什么? JDBC(Java DataBase Connectivity)java数据库连接 2.JDBC可以做什么? 简单地说,JDBC 可做三件事:与数据库建立连接.发送 ...
- CYDIA装了个插件,想删除怎么都删除,电脑如何删除插件?
http://bbs.weiphone.com/read-htm-tid-3670917.html 装了个插件,想删除怎么都删除不掉不要跟我说在CYDIA里面删除.,在CYDIA里点击该插件就会闪退C ...
- Android学习系列(19)--App离线下载
宜未雨而绸缪,毋临渴而掘井.----朱用纯<治家格言> 离线下载,在有网络的情况下下载服务器数据,以便无网络时也能阅读,就是离线阅读. 离线下载的功能点如下: ...
- js时间戳转成日期格式
将时间戳转换成日期格式:// 简单的一句代码var date = new Date(时间戳); //获取一个时间对象 注意:如果是uinx时间戳记得乘于1000.比如php函数time()获得的时间戳 ...
- Python rindex() 方法
描述 Python rindex() 方法返回子字符串最后一次出现在字符串中的索引位置,该方法与rfind() 方法一样,只不过如果子字符串不在字符串中会报一个异常. 语法 rindex() 方法语法 ...
- [WARNING] mod_event_socket.c:2641 IP 172.18.1.112 Rejected by acl "loopback.auto"
一.目标修改event_socket配置,使之能够建立远端ESL链接. 二.步骤 . vim ../autoload_configs/event_socket.conf.xml . 默认的监听地址配置 ...
- ubuntu 16.04 apt-get 更新使用中科大镜像源
1 备份系统配置 sudo cp /etc/apt/sources.list /etc/apt/source.list.bak 2 编辑配置 sudo vi /etc/apt/sources.list ...
- 安装ubuntu和windows双系统后,如何修改默认启动项
在安装了Ubuntu16.04系统之后,系统会默认自启动Ubuntu16.04,而我们大多数情况下可能都在使用windows系统,不修改默认设置,不经意间便会启动了Ubuntu16.04,通过我的经历 ...