我正在尝试使用 sh
文件执行命令,如下所示:
#!/bin/bash
echo "executing......................................"
wget -i http://example.com -O /dev/null
当我直接从我的桌面文件夹访问时,它运行正常。
但是当我将它与我的 golang 项目集成时,例如:
我创建了一个名为 myProject/sh
的文件夹并将文件粘贴到那里。
现在在我的另一个包中使用 cron,我正在尝试访问 test.sh
文件。
func RunCron() {
c := cron.New()
c.AddFunc("10 * * * * *", ExecuteFunction)
c.Start()
}
func ExecuteFunction(){
fmt.Println("test----------------")
utils.ExecuteCommand("sh "+config.GetBasePath()+"sh/test.sh")
}
现在它输出类似的东西
test----------------
Result: executing......................................
exit status 4: --2018-01-16 18:25:10-- http://example.com
Resolving http://example.com)... 1.1.1.1
Connecting to example.com (example.com)|2.2.2.2|:8080... failed: Connection refused.
No URLs found in example.com.
我无法弄清楚为什么相同的代码在我的桌面文件夹中运行良好,但在我的项目文件夹中停止执行。你能告诉我我做错了什么来节省我的时间吗?
谢谢!
最佳答案
你考虑过使用"os/exec"
cmd := exec.Command("/bin/sh", test.sh)
编辑:是的,我当时没有建议,我自己测试了。 .
func main() {
cmd := exec.Command("/bin/sh", "test.sh")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf("output is :\n%s\n", string(out)) }
关于go - 为什么不在项目文件夹内执行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48281827/