当前位置:网站首页>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()
}
边栏推荐
- /silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
- What is hybrid web containers for SAP ui5
- Actual battle of financial risk control - under Feature Engineering
- Connected block template and variants (4 questions in total)
- Jvm-01 (phased learning)
- QT uses sqllite
- 寻找重复数[抽象二分/快慢指针/二进制枚举]
- Golang configure export goprivate to pull private library code
- Systemserver service and servicemanager service analysis
- Mmsegmentation series training and reasoning their own data set (3)
猜你喜欢

Baohong industry | what misunderstandings should we pay attention to when diversifying investment

Multi threaded query, double efficiency

Use usedeferredvalue for asynchronous rendering

Baohong industry | four basic knowledge necessary for personal finance

2022-2028 global soft capsule manufacturing machine industry research and trend analysis report

Design details of SAP e-commerce cloud footernavigationcomponent

Feature query of hypergraph iserver rest Service

Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched
![寻找重复数[抽象二分/快慢指针/二进制枚举]](/img/9b/3c001c3b86ca3f8622daa7f7687cdb.png)
寻找重复数[抽象二分/快慢指针/二进制枚举]

图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
随机推荐
Systemserver service and servicemanager service analysis
2022-2028 global soft capsule manufacturing machine industry research and trend analysis report
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
Analysis of FLV packaging format
Jvm-01 (phased learning)
ORA-01547、ORA-01194、ORA-01110
连通块模板及变式(共4题)
Feature query of hypergraph iserver rest Service
数据传输中的成帧
ZABBIX API creates hosts in batches according to the host information in Excel files
Missing numbers from 0 to n-1 (simple difficulty)
PHP notes - use Smarty to set public pages (include, if, else, variable settings)
QT environment generates dump to solve abnormal crash
在QML中加载不同字体
MMSegmentation系列之训练与推理自己的数据集(三)
Leetcode question brushing (10) - sequential question brushing 46 to 50
venn图取交集
Actual battle of financial risk control - under Feature Engineering
JS <2>
/silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult