当前位置:网站首页>Go execute shell command
Go execute shell command
2022-07-02 03:11:00 【Le Yangjun】
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))
// For example, I now have a scheduled task to perform , Encapsulate a method for automatic execution
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(" Command execution failed ")
}else{
fmt.Println(" Command executed successfully ")
}
}
// Check if the command exists
func checkExists() {
path, err := exec.LookPath("pwd")
if err != nil {
fmt.Printf("pwd non-existent \n")
} else {
fmt.Printf("'pwd' executable is in '%s'\n", path)
}
}
// The two commands are executed in sequence
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()
}
// Read the output by line
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()
}
边栏推荐
- SAML2.0 notes (I)
- MongoDB非關系型數據庫
- Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
- MongoDB非关系型数据库
- Verilog state machine
- Apple added the first iPad with lightning interface to the list of retro products
- What are the common proxy servers and what are the differences?
- 跟着CTF-wiki学pwn——ret2shellcode
- Pychart creates new projects & loads faster & fonts larger & changes appearance
- Mongodb non relational database
猜你喜欢
Form custom verification rules
Detailed explanation of the difference between Verilog process assignment
[JS reverse series] analysis of a customs publicity platform
AcWing 245. Can you answer these questions (line segment tree)
2022-2028 global manual dental cleaning equipment industry research and trend analysis report
[staff] restore mark (Introduction to the use of restore mark | example analysis of Metaphone mark and restore mark)
Just a few simple steps - start playing wechat applet
2022-2028 global aluminum beverage can coating industry research and trend analysis report
2022-2028 global nano abrasive industry research and trend analysis report
Cache processing scheme in high concurrency scenario
随机推荐
Verilog avoid latch
Load different fonts in QML
Baohong industry | what misunderstandings should we pay attention to when diversifying investment
Form custom verification rules
跟着CTF-wiki学pwn——ret2shellcode
C#联合halcon脱离halcon环境以及各种报错解决经历
Common means of modeling: aggregation
Cache processing scheme in high concurrency scenario
Intersection of Venn graph
Design details of SAP e-commerce cloud footernavigationcomponent
[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the
3048. Number of words
GB/T-2423. XX environmental test documents, including the latest documents
Principle of computer composition - interview questions for postgraduate entrance examination (review outline, key points and reference)
Apple added the first iPad with lightning interface to the list of retro products
C reflection practice
流线线使用阻塞还是非阻塞
halcon图像矫正
3124. Word list
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination