zoukankan      html  css  js  c++  java
  • go 在crontab里面运行报错 解决方案

    问题背景

    你高高兴兴的写好了一个go脚本,放到你的服务器上,打算定期运行这个脚本,你打开crontab -e,
    然后输入:

    */1 * * * * go run /root/test/main.go
    

    然后发现迟迟没有反应,你就查看一下输出什么错误:

    */1 * * * * go run /root/test/main.go >> /root/test/log.txt
    

    然后报错如下:

    build command-line-arguments: cannot load test/mypath: malformed module path "test/mypath": missing dot in first path element
    

    missing dot in first path element ? What ? 你会好奇,你明明使用了:

    go mod init test
    go mod tidy
    

    然后你测试了一下,如果cd /root/test/,然后run 是非常正常的,
    一旦在非/root/test/目录下,就会出现这种路径报错问题,

    类似的问题还有:

    ➜  root go run /Users/thoth/program/gocode/alarm15m/main.go 
    alarm15m/main.go:12:2: package alarm15m/mypath is not in GOROOT (/usr/local/go/src/alarm15m/mypath)
    alarm15m/main.go:13:2: package alarm15m/tools is not in GOROOT (/usr/local/go/src/alarm15m/tools)
    

    Xxx is not in GOROOT , 总是在导包上出问题!
    这b玩意,为啥这样,我也不知道,我也不想研究他(每天的学习任务安排很满,我觉得这种就是特定语言特定的场景没必要,通识性知识才是重要的,这种Xxx语言我一天就会切换3~4种,很难每个都去研究,完成任务就好),估计就是有他路径设置的考虑,管他呢,反正有一点确定的就是在项目路径下执行一定是成功的,基于这个很确定的点,我想到一些曲线救国方案。


    解决

    直接写sh脚本
    run.sh

    #!/bin/sh  
    cd /root/test/
    go run main.go
    

    crontab -e

    */15 * * * * bash /root/test/run.sh
    

    完美解决。

  • 相关阅读:
    爬虫时http错误提示
    json.dumps()和json.loads()
    scrapy框架原理学习
    利用tushare进行对兴业银行股价的爬取,并使用numpy进行分析
    随机生成60位同学成绩,并求他们的平均数,中位数,众数等
    numpy中random的使用
    matplotlib中subplot的使用
    使用matplotlib画饼图
    乔坟,乔坟!
    c#控件的动画显示效果
  • 原文地址:https://www.cnblogs.com/mysticbinary/p/13371189.html
Copyright © 2011-2022 走看看