Linux系统执行命令方法
现在我们无论是工作中还是学习中很多情况下用到Linux系统,当我们需要在C#代码中调用类似与cmd窗口执行命令时候,就需要用到此方法
public static Process CommitCommand(string commandStr, string workingDir, Action<DataReceivedEventArgs> action)
{
var logger = LogManager.GetCurrentClassLogger();
try
{
logger.Debug("Get into CommitCommand");
Process process = new Process();
process.StartInfo.FileName = commandStr.Substring(0, commandStr.IndexOf(" ")).Trim();
var pathDir = Environment.GetEnvironmentVariable("PATH").Split(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? ":" : ";", StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault(s =>
File.Exists(Path.Combine(s, process.StartInfo.FileName)) ||
File.Exists(Path.Combine(s, process.StartInfo.FileName + ".exe")));
if (string.IsNullOrEmpty(pathDir))
{
process.StartInfo.FileName = Path.GetFullPath(process.StartInfo.FileName, workingDir);
}
process.StartInfo.Arguments = commandStr.Substring(commandStr.IndexOf(" ")).Trim();
process.StartInfo.WorkingDirectory = string.IsNullOrWhiteSpace(workingDir) ? process.StartInfo.WorkingDirectory : workingDir;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.ErrorDataReceived += (sender, args) =>
{
action(args);
logger.Error($"ErrorDataReceived:commandStr:{commandStr}. workingDir:{workingDir}. Error:{args.Data}");
};
process.EnableRaisingEvents = true;
process.OutputDataReceived += (sender, args) =>
{
action(args);
//logger.Debug($"OutputDataReceived:{args.Data}");
};
process.Exited += (sender, args) =>
{
logger.Debug("程序退出!");
logger.Error($"Exited:commandStr:{commandStr}. workingDir:{workingDir}");
};
process.Start(); process.StandardInput.AutoFlush = true;//process.StandardInput.WriteLine("")
process.BeginErrorReadLine();
process.BeginOutputReadLine();
logger.Debug("Sign out CommitCommand");
return process;
}
catch (Exception e)
{
logger.Error(e, "process can not start.");
logger.Debug(e.Message);
return null;
}
}
调用也非常简单,参数1:执行得命令 参数2:路径 比如说我们需要运行mysql,就需要在对应文件夹内执行对应命令,这个时候第二个参数就可以传入路径
然后我再给出一个调用得例子
Process pc = new Process();
pc = ToolClass.CommitCommand("nvidia-smi", "/", args =>
{
if (!string.IsNullOrEmpty(args.Data))
{
useGpuNumber.Add(args.Data);
}
});
pc.WaitForExit();
pc.Dispose();
我这里时查看显卡也就是GPU得信息的命令,可以再root下执行,然后我这里就传入了一个/,拿到执行命令返回的数据
Linux系统执行命令方法的更多相关文章
- Android 开发进入Linux系统执行命令 2018-5-25 Fri.
/** * 进入linux cmd执行命令 * * @param command * @return */ private boolean runRootCommand(String command) ...
- 修改linux系统时间的方法(date命令)
修改linux系统时间的方法(date命令) 来源:互联网 作者:佚名 时间:11-18 23:22:27 [大 中 小] date命令不仅可以显示系统当前时间,还可以用它来修改系统时间,下面简单的介 ...
- [转帖]Linux后端执行命令的方法
Linux 后台执行命令的方法 http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4241330&fromuid=212883 ...
- php 执行计划任务方式之 linux crontab 执行命令
一.crond简介 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动c ...
- 查看linux系统版本命令 (转)
查看linux系统版本命令 分类: Linux 知识小结2011-10-10 15:26 240162人阅读 评论(9) 收藏 举报 linuxredhatdebianx86susesun 一.查看内 ...
- Linux系统——awk命令
awk命令不仅仅是Linux系统的命令,也是一种编程语言,用来处理数据和生成报告(Exel),处理的数据可以是一个或多个文件(标准输入和管道获取标准输入).可在命令行上编辑操作,也可以写成awk程序运 ...
- Linux系统ifconfig命令找不到,centos ifconfig Command not found
centos ifconfig Command not found,Linux系统ifconfig命令找不到 >>>>>>>>>>>& ...
- Linux系统基础命令
这是看itercast的学习笔记 Linux系统基础命令 日期时间 命令date用以查看.设置当前系统时间:格式化显示时间: +%Y--%m--%d 命令hwclock(clock)用以显示硬件时钟时 ...
- Linux系统-解压缩命令集合
Linux系统-解压缩命令集合 linux zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. ...
随机推荐
- (Math.round(num*100)/100).toFixed(2); 将输入的数字变成保留两位小数
<input type="number" @input="onInputPrice" @blur="onPrice" data-id= ...
- 全面系统讲解CSS工作应用+面试一步搞定
[TOC] 一.课程介绍 二.HTML基础强化 html常见元素和理解 html常见元素分类 head区元素:(不会在页面上留下元素) * meta * title * style * link * ...
- git生成和添加SSH公钥
一 .前言: 大家换电脑.换公司的时候,经常要关联本地git和git线上仓库, 在这里我就顺便记一下,好记性不如烂笔头, 以后找起来来方便 二 .查看自己是否生成过公钥,有的话可以直接拿过来用, 也可 ...
- oracle 多列求和
第一种: select sum(decode(count1,null,0,count1) +decode(count2,null,0,count2) +decode(count3,null,0,cou ...
- ruby 版本管理RVM (ruby version manager)
macOS. 自带的ruby 版本目录权限比较高, 经常有很多 操作需要权限而不能执行 虽然 macOS 自带了一个 ruby 环境,但是是系统自己使用的,所以权限很小,只有 system. 而/Li ...
- spring原始注解(value)-03
本博客依据是是spring原始注解-02的代码 注入普通数据类型:@Value注解的使用 1.添加driver属性,使用value注解 @Service("userService" ...
- MongoDB 数据库开发规范
MongoDB 数据库开发规范 转载自-落雨_ https://developer.aliyun.com/article/255536 简介: mongoDB库的设计 mongodb数据库命名规范:d ...
- babel 的介绍及其配置
vue/cli -- babel Babel 是一个工具链,主要用于将 ECMAScript 2015+ 版本的代码转换为向后兼容的 JavaScript 语法,以便能够运行在当前和旧版本的浏览器或其 ...
- React + TypeScript + Taro前端开发小结
前言 项目到一段落,先来记录一下,本文以前端新手的角度记录React.TypeScript.Taro相关技术的开发体验以及遇到的问题和解决方法. 之前总说要学React(这篇博客:代码使我头疼之Rea ...
- baiyang网站二代域名及短链接
http://985.so/wesv https://cloud.tencent.com/developer/column/93900