当前位置:网站首页>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()
}
边栏推荐
- Systemserver service and servicemanager service analysis
- 4. Find the median of two positive arrays
- What are the characteristics of common web proxy IP
- Pychart creates new projects & loads faster & fonts larger & changes appearance
- Delphi xe10.4 installing alphacontrols15.12
- [road of system analyst] collection of wrong topics in enterprise informatization chapter
- ORA-01547、ORA-01194、ORA-01110
- [JS reverse series] analysis of a customs publicity platform
- What is the difference between an intermediate human resource manager and an intermediate economist (human resources direction)?
- Verilog wire type
猜你喜欢

Verilog 过程赋值 区别 详解

C reflection practice

Design details of SAP e-commerce cloud footernavigationcomponent

2022-2028 global aluminum beverage can coating industry research and trend analysis report

QT environment generates dump to solve abnormal crash

Common means of modeling: aggregation

How to develop digital collections? How to develop your own digital collections

SAML2.0 笔记(一)

Systemserver service and servicemanager service analysis

表单自定义校验规则
随机推荐
Share the basic knowledge of a common Hongmeng application
Actual battle of financial risk control - under Feature Engineering
結婚後
Verilog 过程赋值 区别 详解
2022-2028 global military computer industry research and trend analysis report
Verilog 过程连续赋值
Ten minutes will take you in-depth understanding of multithreading - multithreaded teamwork: synchronous control
ORA-01547、ORA-01194、ORA-01110
2022-2028 global deep sea generator controller industry research and trend analysis report
Gradle notes
Redis set command line operation (intersection, union and difference, random reading, etc.)
Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
JDBC details
JS <2>
Intersection of Venn graph
Possible causes of runtime error
Framing in data transmission
【JVM】创建对象的流程详解
Verilog timing control
Missing numbers from 0 to n-1 (simple difficulty)