Crontab无法运行Go / Golang程序。

huangapple go评论86阅读模式
英文:

Crontab won't run go / golang program

问题

我已经设置了crontab来运行一个编译好的golang脚本。该脚本负责将一个文件夹复制到FTP服务器上。在这方面没有什么特别的。

我可以通过在命令行中输入"/backup/main"来运行程序,一切正常。

现在我想每天晚上运行这个程序。我已经设置了cronjob每5分钟运行一次以测试是否正常工作。但是它没有工作!

如果我检查syslog,我可以看到程序被调用了,但是没有任何反应。

文件"/backup/main"的权限是chmod a+x。
crontab是在"sudo"(sudo crontab -e)下设置的。所以所有的权限应该都没问题。

crontab中的一行是:

*/5 * * * * /backup/main

希望有人能帮忙。

英文:

I have setup crontab to run a compiled golang script. The script is responsable for copying a folder to a ftp. Nothing special in this.

I can run the program in the commandline by just typing "/backup/main" And it all works.

Now I want to run this program every night. I have setup the cronjob to run every 5 minutes to test if it works. And it dont!

If I check the syslog I can see the program is beeing called, but nothing happens.

The file "/backup/main" is chmod a+x
The crontab is set in "sudo" (sudo crontab -e). So alle permissions should be ok.

The line in crontab:

*/5 * * * * /backup/main

Hops someone can help.

答案1

得分: 4

希望能帮助那些像我一样找不到解决方案的人:

可以通过将路径和GOPATH变量添加到crontab -e文件中来解决此问题。

您可以通过运行echo $PATHecho $GOPATH来找到它们。

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/go/bin:/root/bin
GOPATH=/var/projects/src

# +---------------- 分钟 (0 - 59)
# |  +------------- 小时 (0 - 23)
# |  |  +---------- 日期 (1 - 31)
# |  |  |  +------- 月份 (1 - 12)
# |  |  |  |  +---- 星期几 (0 - 6) (星期天=0)
# |  |  |  |  |
  */5 * * * * /backup/main

您的代码可能无法运行,因为go无法找到包的错误。

例如,您通过运行go get安装的第三方包

go get gopkg.in/gomail.v2

问题是您可能已经从您的工作目录中运行了go get
在我的情况下,即GOPATH=/var/projects/src

您可以在以下位置看到包

/your/working_directory/src/pkg/linux_amd64

英文:

Hoping to help others who couldn't find the solution as me:

The issue can be resolved by adding path and gopath variables to the crontab -e file.

you can find them by running echo $PATH echo $GOPATH

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/go/bin:/root/bin
GOPATH=/var/projects/src

# +---------------- minute (0 - 59)
# |  +------------- hour (0 - 23)
# |  |  +---------- day of month (1 - 31)
# |  |  |  +------- month (1 - 12)
# |  |  |  |  +---- day of week (0 - 6) (Sunday=0)
# |  |  |  |  |
  */5 * * * * /backup/main

Your code may not be running due error that go was not able to find packages

i.e. the third packages that you installed by running go get example

go get gopkg.in/gomail.v2

The problem is that you may have go get from your working directory!
In my case which is GOPATH=/var/projects/src

you can see the package inside

/your/working_directory/src/pkg/linux_amd64

答案2

得分: 3

也许出现了一些问题,程序会打印一些日志,但是这些日志会被重定向到/dev/null,你无法查看错误日志。
你可以添加以下内容将日志打印到某个日志文件中。

*/5 * * * * /backup/main 1>> /path/to/log/file 2>> /path/to/error_log/file
英文:

Maybe something wrong, and the program print some log, but the log will be redirected to the /dev/null, and you can't check the error log.
you can add this to print the log to some log file.

*/5 * * * * /backup/main 1>> /path/to/log/file 2>>/path/to/error_log/file

答案3

得分: 3

关于cron静默失败的常见观点是可能是环境的问题。通过将您的golang程序包装在一个shell脚本中,您可以控制环境(并使您的配置文件运行)。您还可以添加有用的调试信息,如"env"。

要模拟没有环境的cron运行,请执行以下命令:

env -i ./myscript.sh

其中myscript.sh已经设置为可执行(chmod +x),并且只运行"env"命令。然后将您的golang命令放在其中,看看是否在没有环境的情况下仍然可以正常工作。然后,一旦您证明了问题是环境引起的(希望如此),您可以在cron中使用一个shell脚本。

英文:

The conventional wisdom about cron silent failures is that it's probably the environment. By wrapping your golang program in a shell script, you can control the environment (and get your profiles to run). You can also put in useful debug bits like "env".

To simulate cron with it's empty env run:

env -i ./myscript.sh

where myscript.sh is chmod +x and just runs "env". Then put your golang command in there and see if it still works without the environment. Then when you've proved it's the environment (hopefully), use a shell script in cron.

答案4

得分: 2

我偶然发现了这篇帖子,因为这正是我遇到的问题。
crontab没有运行我的编译后的golang二进制文件。
对我来说,问题是crontab行的配置不正确。
我按照以下格式进行配置,这对我起到了作用:

* * * * * cd包含exe的文件夹 && ./exe名称

*注意!*这将每分钟运行一次二进制文件
来源:https://stackoverflow.com/questions/3474280/how-to-set-up-a-cron-job-to-run-an-executable-every-hour
希望这可以帮助遇到类似问题的人。

英文:

I stumbled onto this post as this was exactly the problem that I encountered.
crontab not running my compiled golang binary.
For me, the problem was the incorrect configuration of the crontab line.
I followed the following format in the configuration and that did the trick for me:

* * * * * cd folder_containing_exe && ./exe_name

note! This runs the binary every minute
source: https://stackoverflow.com/questions/3474280/how-to-set-up-a-cron-job-to-run-an-executable-every-hour
Hope this can perhaps help someone with a similar issue.

答案5

得分: -1

由于我的代码是基于包的,有一个 go.mod 文件,查看 crontab 日志后,我得到的错误是脚本找不到导入项(因为它们是从脚本所在位置进行本地引用,而不是在 go.mod 中进行全局引用)。为了解决这个问题并使其正常工作:

  1. 创建一个 shell 脚本 scriptrunner.sh
#!/bin/bash
cd /full/path/mygocode/ # 你的 go 代码所在的位置
/snap/bin/go run script.go #(对于 Ubuntu 22+,或者 /usr/bin/go)
  1. chmod +x scriptrunner.sh
  2. 在 crontab 中更新路径,指向新的文件名,例如 */5 * * * * ./path/scriptrunner.sh

请注意,如果这是一个已编译的二进制文件,它也可以正常工作。将可执行文件包装为 bash 脚本有助于确保你控制执行的内容和执行的位置。

英文:

Since my code was package based, with a go.mod file, the error I was getting after viewing crontab logs was that the script could not find the imports (since they were local referenced from the location of the script instead of globally referenced in go.mod). To mitigate this and get it working:

  1. Create a shell script scriptrunner.sh
#!/bin/bash
cd /full/path/mygocode/ # the place where your go code is
/snap/bin/go run script.go # (for Ubuntu 22+, or /usr/bin/go)
  1. chmod +x scriptrunner.sh
  2. Update path in crontab to point to new filename like */5 * * * * ./path/scriptrunner.sh

Note this works the same if this has been a compiled binary. It is helpful to wrap the executable as a bash script to ensure you are controlling what you are executing and from where.

huangapple
  • 本文由 发表于 2015年11月26日 17:24:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/33934764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定