go - 为什么不在项目文件夹内执行命令?

标签 go cron sh

我正在尝试使用 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/

相关文章:

sql - GoLang、REST、PATCH 和构建 UPDATE 查询

google-app-engine - 如何将 App Engine 数据存储中的数据存储到 csv 文件中?

json - Reflect vs Regex 检查 Golang 中的空 JSON 数组属性

php - 仅在特定时间每分钟运行一次 cron 作业?

linux - 给 Linux awk 中的外部变量赋值

windows - 带有命令替换的 git 命令不返回任何内容

Windows OpenFile 标志

ruby-on-rails - Cron 作业不执行命令

linux - 用于重新启动的 crontab 不起作用

bash - Bash 子字符串替换 ${foo/a/b} 的 Shell 通用等价物