当前位置:网站首页>Spawning Processes and Exec‘ing Processes
Spawning Processes and Exec‘ing Processes
2022-04-21 07:46:00 【Xiong Xiaohan】
Derived external process
dateCmd := exec.Command("date")
dateOut, err := dateCmd.Output()
if err != nil {
panic(err)
}
fmt.Println("> date")
fmt.Println(string(dateOut))
> date
2022 year 4 month 20 Japan Wednesday 19 when 16 branch 44 second CST
exec.Command The results of the implementation of , Consistent with the results executed on the command line .
grepCmd := exec.Command("grep", "hello")
grepIn, _ := grepCmd.StdinPipe()
grepOut, _ := grepCmd.StdoutPipe()
grepCmd.Start()
grepIn.Write([]byte("hello grep\ngoodbye grep"))
grepIn.Close()
grepBytes, _ := io.ReadAll(grepOut)
grepCmd.Wait()
fmt.Println("> grep hello")
fmt.Println(string(grepBytes))
> grep hello
hello grep
Different from the above code , Here, the input is explicitly obtained / Output pipe , Start the process , Enter data into the pipeline , Read pipeline output , Finally, wait for the process to end .
lsCmd := exec.Command("bash", "-c", "ls -a -l -h")
lsOut, err := lsCmd.Output()
if err != nil {
panic(err)
}
fmt.Println("> ls -a -l -h")
fmt.Println(string(lsOut))
> ls -a -l -h
total 3992
drwxr-xr-x 4 baiye staff 128B 4 20 19:16 .
drwxr-xr-x 83 baiye staff 2.6K 4 2 10:38 …
-rwxr-xr-x 1 baiye staff 1.9M 4 20 19:16 spawning-processes
-rw-r–r-- 1 baiye staff 703B 3 28 18:11 spawning-processes.go
When generating commands , An array of commands and parameters that need to be explicitly described , You can't just pass in a command line string . If you need to execute the full command of string style , have access to bash -c Options .
Replace current process
binary, lookErr := exec.LookPath("ls")
if lookErr != nil {
panic(lookErr)
}
args := []string{
"ls", "-a", "-l", "-h"}
env := os.Environ()
execErr := syscall.Exec(binary, args, env)
if execErr != nil {
panic(execErr)
}
total 3016
drwxr-xr-x 4 baiye staff 128B 4 20 20:20 .
drwxr-xr-x 83 baiye staff 2.6K 4 2 10:38 …
-rwxr-xr-x 1 baiye staff 1.5M 4 20 20:20 execing-processes
-rw-r–r-- 1 baiye staff 300B 3 28 18:53 execing-processes.go
版权声明
本文为[Xiong Xiaohan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210627175496.html
边栏推荐
- 星际迷航-发现号-第三季最后一集
- PLSQL14软件包下载、汉化、注册
- Regular Expressions
- ETL之Kettle工具十大功能特性详解
- H + background UI framework, click the button to create a new tab
- .net core 部署至Linux平台上访问SQL SERVER 2008 R2时提示连接超时异常
- C#linq的group、count、sum,记一下
- You cannot set a form field before rendering a field associated with the value
- php 格式化数字
- UMLet初学者使用步骤
猜你喜欢

论文阅读:Cached and Confused: Web Cache Deception in the wild

论文阅读:Supporting Early and Scalable Discovery of Disinformation Websites

PLSQL developer 14 installation details

Principle and introduction of elk log analysis system

leetcode 59.螺旋矩阵Ⅱ

window 系统丢失北京时区解决方案

H + background UI framework, click the button to create a new tab

VS2019官方的免费打印控件

UMLet初学者使用步骤

分布式事务Seata
随机推荐
服务器部署svn环境
OmniPlan工具使用手册
设置谷歌浏览器深色黑色背景
MySQL数据库运行代码后,中文显示的是问号 ?
Common weak passwords in network security devices
asp.net re正则表达式,筛选返回的字符串中时间单号金额,把不规范的时间格式转化为正确的时间格式
When deploying. Net core on Linux platform to access SQL Server 2008 R2, it prompts the connection timeout exception
Exit and Status
C# asp.net 调百度文字识别接口
leetcode 19.删除链表倒数的第N个节点
sqlserver 两个表之间进行模糊查询,sqlserver 导入excel数据步骤
GeoServer 2.20.1 解决跨域问题 CORS
ETL之Kettle工具十大功能特性详解
C#中listView列自动适应缩放的完美效果
虚拟机 主机 ping ssh 校园网 桥接 NET
MYSQL中批量替换某个字段的部分数据
动态生成三级菜单
leetcode 707.设计链表
C#linq的group、count、sum,记一下
Command-Line