【问题标题】:Lua - Error running script via Crontab with os.execute command - Exit code = 32512Lua - 使用 os.execute 命令通过 Crontab 运行脚本时出错 - 退出代码 = 32512
【发布时间】:2021-06-27 07:53:42
【问题描述】:

我有一个尝试通过 crontab 运行的 Lua 脚本 - 请参阅下面的请求/计划。

# m h  dom mon dow   command
0 8 * * * /usr/bin/lua /home/pi/shared/TS_cabin_graph_watts_12h.lua >/home/pi/shared/watts.log 2>&1
1 8 * * * /usr/bin/lua /home/pi/shared/TS_cabin_graph_temp_12h.lua >/home/pi/shared/temp.log 2>&1 

整个脚本做了很多不同的事情,但下面是具体的 os.execute 图请求。

print("Start : graph command")
local x = os.execute('graph /home/pi/shared/feed12hr.csv -y 9,10 --ylabel Temp --title CabinTemp --figsize 1600x1000 --output /home/pi/shared/cabingraph12hr.png') 
print(x)
print("End : graph command") 

当我自己在命令行手动运行它时,它工作正常 - 请参阅下面的输出。

Start : graph command
/usr/local/lib/python2.7/dist-packages/pandas/plotting/_converter.py:129: FutureWarning: Using an implicitly registered datetime converter for a matplotlib plotting method. The converter was registered by pandas on import. Future versions of pandas will require you to explicitly register matplotlib converters.

To register the converters:
        >>> from pandas.plotting import register_matplotlib_converters
        >>> register_matplotlib_converters()
  warnings.warn(msg, FutureWarning)
0
End : graph command

但是当我通过 cronjob 运行后检查日志时,它失败并报告以下内容

Part 4
Start : graph command
32512
End : graph command

知道原因和需要的解决方法吗?

【问题讨论】:

    标签: shell lua cron


    【解决方案1】:

    错误的意思是“找不到文件”。
    这是因为 cron 以非常简约的 $PATH 运行您的任务,因此您必须在调用 os.execute() 时指定 graph 可执行文件的完整路径。

    【讨论】:

    • 嗨,我添加了完整路径 /usr/local/bin/graph,首先手动检查它是否可以工作 m 然后我允许它通过 cron 运行,现在给我一个“256”错误/退出代码?关于如何解决这个问题的任何想法?
    【解决方案2】:

    在 Lua 5.1 中,os.execute 返回 C system 函数返回的任何内容。在 Linux 中,使用man 3 system 中解释的解码宏,返回值32512 解码为WIFEXITED(s) && WEXITSTATUS(s) == 127。在较新版本的 Lua 中,您不必手动执行此解码,因为它会为您解码并返回 nil, 'exit', 127。该退出代码的原因是system 运行一个shell,当shell 返回127 时,这是因为它找不到您告诉它执行的文件。您在 cron 作业中遇到该问题但不是交互式的最可能的原因是 cron 作业使用了一组不同的环境变量,并且 cron 中的 PATH 环境变量不包含 graph 的位置。以交互方式执行 which graph 以查看它的位置,然后更新您的脚本以使用该完整路径。

    【讨论】:

    • 嗨@Joseph - 谢谢我按照建议做了并输入了完整路径,但我现在收到 256 错误/退出代码.. 仅供参考 - 我也尝试了 io.popen 脚本,但是给了我一个“图表”错误消息,我将不得不与 graph-cli 应用程序所有者讨论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 2013-08-26
    • 2016-12-18
    • 2020-09-04
    • 2019-08-28
    • 2011-02-09
    相关资源
    最近更新 更多