当前位置:网站首页>Go执行shell命令
Go执行shell命令
2022-07-02 03:07:00 【乐杨俊】
func main() {
cmd := exec.Command("ls", "-lah")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf("combined out:\n%s\n", string(out))
//比如我现在有个定时任务需要执行,封装一个方法自动执行
cmdStr := "cd /go/leyangjun/test/ && ./main crontab --run=single --job=myCrontabName"
runCmd := exec.Command("/bin/bash", "-c", cmdStr)
runCmdOut, err := runCmd.CombinedOutput()
if err != nil {
fmt.Println("命令执行失败")
}else{
fmt.Println("命令执行成功")
}
}
//检查命令是否存在
func checkExists() {
path, err := exec.LookPath("pwd")
if err != nil {
fmt.Printf("pwd不存在\n")
} else {
fmt.Printf("'pwd' executable is in '%s'\n", path)
}
}
//两个命令依次执行
func successivelyExists() {
c1 := exec.Command("ls")
c2 := exec.Command("wc", "-l")
c2.Stdin, _ = c1.StdoutPipe()
c2.Stdout = os.Stdout
_ = c2.Start()
_ = c1.Run()
_ = c2.Wait()
}
//按行读取输出的内容
func readByRowExists() {
cmd := exec.Command("ls", "-h")
stdout, _ := cmd.StdoutPipe()
cmd.Start()
reader := bufio.NewReader(stdout)
for {
line, err := reader.ReadString('\n')
line = strings.TrimSpace(line)
if err != nil || io.EOF == err {
break
}
log.Println(line)
}
cmd.Wait()
}
边栏推荐
- 数据传输中的成帧
- Qualcomm platform wifi-- WPA_ supplicant issue
- [JVM] detailed description of the process of creating objects
- MSI announced that its motherboard products will cancel all paper accessories
- ZABBIX API creates hosts in batches according to the host information in Excel files
- 连通块模板及变式(共4题)
- 結婚後
- Verilog 避免 Latch
- Redis set command line operation (intersection, union and difference, random reading, etc.)
- 流线线使用阻塞还是非阻塞
猜你喜欢
Baohong industry | what misunderstandings should we pay attention to when diversifying investment
el-table的render-header用法
[JVM] detailed description of the process of creating objects
结婚后
命名块 verilog
C reflection practice
C shallow copy and deep copy
连通块模板及变式(共4题)
How to create an instance of the control defined in SAP ui5 XML view at runtime?
[road of system analyst] collection of wrong topics in enterprise informatization chapter
随机推荐
ZABBIX API creates hosts in batches according to the host information in Excel files
buu_ re_ crackMe
C shallow copy and deep copy
AcWing 245. Can you answer these questions (line segment tree)
2022-2028 global wood vacuum coating machine industry research and trend analysis report
Actual battle of financial risk control - under Feature Engineering
Discussion on related configuration of thread pool
venn图取交集
GB/T-2423. XX environmental test documents, including the latest documents
2022-2028 global manual dental cleaning equipment industry research and trend analysis report
结婚后
[Chongqing Guangdong education] Sichuan University concise university chemistry · material structure part introductory reference materials
Stdref and stdcref
Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
Set status bar color
Jvm-01 (phased learning)
Multi threaded query, double efficiency
Use usedeferredvalue for asynchronous rendering
Possible causes of runtime error
2022-2028 global encryption software industry research and trend analysis report