【问题标题】:Rscript stuck on a system call to pythonRscript卡在对python的系统调用上
【发布时间】:2015-12-28 00:55:37
【问题描述】:

我在我的 Ubuntu 服务器的后台运行了一个定期脚本。如果我在 RStudio 中执行它,一切都会按预期工作。但是当我使用 Rscript 通过终端执行时,它在调用 python 脚本时会卡住(并不总是......但很多时候它会卡在那里)。 (我知道它卡在那里,因为当我停止 Rscript 时,它总是告诉我它正在运行我的 python 脚本)。我已经chmod 777'ed 到那个 python 脚本,但没有线索。

这一直正常工作,直到几天前,不知道为什么。

Rscript 语句:

Rscript /home/XXX/XXX/scriptServicioBBDDHS.R

在R代码中停止的地方:

outputMACs <- system(ignore.stdout = F, ignore.stderr = T, 
                   "python3 /home/XXX/XXX/MACsPrincipal.py 'Y.Y.Y.Y' 'user' 'password'", intern = T)

python 脚本是 MikroTik 路由器的 API。尝试读取路由器的响应时卡住了。在这句话中:

r = select.select([s, sys.stdin], [], [], None)

我把代码放在python脚本的main()里:

def main():
    s = None
    for res in socket.getaddrinfo(sys.argv[1], "8728", socket.AF_UNSPEC, socket.SOCK_STREAM):
        af, socktype, proto, canonname, sa = res
        try:
             s = socket.socket(af, socktype, proto)
        except (socket.error, msg):
            s = None
            continue
        try:
            s.connect(sa)
        except (socket.error, msg):
            s.close()
            s = None
            continue
        break
    if s is None:
        print ('could not open socket')
        sys.exit(1)

    apiros = ApiRos(s);
    apiros.login(sys.argv[2], sys.argv[3]);

    inputsentence = ['/ip/hotspot/active/print', '=detail=']
    apiros.writeSentence(inputsentence)

    t_end = time.time() + 2
    while time.time() < t_end:
        r = select.select([s, sys.stdin], [], [], None)
        if s in r[0]:
            # something to read in socket, read sentence
            x = apiros.readSentence()

感谢您的帮助。当我第一次使用这个脚本时,这个脚本总是可以工作的,使用 crontab。现在它只是失败了。

塞尔吉奥。

【问题讨论】:

    标签: python r crontab rscript mikrotik


    【解决方案1】:

    我在 MikroTik API 中发现了一个可能的错误。问题出在句子里

    r = select.select([s, sys.stdin], [], [], None)
    

    我不得不重写它以省略 sys.stdin

    r = select.select(s, [], [], 0.0)
    

    为此,我必须通过 R 使用以下参数进行系统调用:

    system(ignore.stdout = F, ignore.stderr = T, "python3 /home/XXX/XXX/MACsPrincipal.py 'Y.Y.Y.Y' 'user' 'password'", intern = T)
    

    如果你不忽略stderr 输出,它就不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 2016-08-30
      相关资源
      最近更新 更多