当前位置:网站首页>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()
}
边栏推荐
- 跟着CTF-wiki学pwn——ret2shellcode
- Verilog wire type
- Discussion on related configuration of thread pool
- [JS reverse series] analysis of a customs publicity platform
- OSPF LSA message parsing (under update)
- 数据传输中的成帧
- [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
- V-model of custom component
- spark调优
- Verilog 过程赋值 区别 详解
猜你喜欢

West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials

命名块 verilog

Design details of SAP e-commerce cloud footernavigationcomponent

Start a business
![[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)](/img/e0/05890eafdb291c5aaff78cc241f455.jpg)
[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)

Formatting logic of SAP ui5 currency amount display

Systemserver service and servicemanager service analysis

MongoDB非关系型数据库
![[JVM] detailed description of the process of creating objects](/img/6e/0803b6b63c48337985faae8d5cbe1a.png)
[JVM] detailed description of the process of creating objects

2022-2028 global wood vacuum coating machine industry research and trend analysis report
随机推荐
C shallow copy and deep copy
venn图取交集
2022-2028 global manual dental cleaning equipment industry research and trend analysis report
ZABBIX API creates hosts in batches according to the host information in Excel files
About DNS
Detailed explanation of the difference between Verilog process assignment
Baohong industry | what misunderstandings should we pay attention to when diversifying investment
Verilog wire type
V-model of custom component
Force deduction daily question 540 A single element in an ordered array
Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
创业了...
Verilog 过程赋值 区别 详解
Verilog 时序控制
Verilog 状态机
使用 useDeferredValue 进行异步渲染
Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
竞争与冒险 毛刺
结婚后