当前位置:网站首页>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()
}
边栏推荐
- [staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
- halcon图像矫正
- Baohong industry | 6 financial management models at different stages of life
- A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
- MongoDB非關系型數據庫
- Form custom verification rules
- C shallow copy and deep copy
- Rotating frame target detection mmrotate v0.3.1 learning model
- C reflection practice
- Verilog state machine
猜你喜欢

GB/T-2423. XX environmental test documents, including the latest documents
On redis (II) -- cluster version

MVVM and MVC

JS <2>

After marriage

2022-2028 global nano abrasive industry research and trend analysis report

C reflection practice

2022-2028 global deep sea generator controller industry research and trend analysis report

Discussion on related configuration of thread pool

結婚後
随机推荐
Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
MongoDB非關系型數據庫
2022-2028 global wood vacuum coating machine industry research and trend analysis report
How does proxy IP participate in the direct battle between web crawlers and anti crawlers
MSI announced that its motherboard products will cancel all paper accessories
Learn PWN from CTF wiki - ret2shellcode
Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
How to create an instance of the control defined in SAP ui5 XML view at runtime?
使用 useDeferredValue 进行异步渲染
C#联合halcon脱离halcon环境以及各种报错解决经历
Baohong industry | four basic knowledge necessary for personal finance
[Chongqing Guangdong education] Sichuan University concise university chemistry · material structure part introductory reference materials
Design details of SAP e-commerce cloud footernavigationcomponent
Possible causes of runtime error
verilog REG 寄存器、向量、整数、实数、时间寄存器
Verilog timing control
Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
旋转框目标检测mmrotate v0.3.1 学习模型