简体   繁体   English

从 crontab 运行的脚本会留下一个空文件

[英]Script run from crontab leaves an empty file

I've tried to run a script from crontab on my Linux system which is leaving an empty file.我试图在我的 Linux 系统上从 crontab 运行一个脚本,该脚本留下一个空文件。 When I run the script logged on the terminal, it works fine.当我运行登录终端的脚本时,它工作正常。

Cron line:克隆线:

@reboot sleep 1m; /bin/bash /root/start_reader_services

The script "start_reader_services" calls a Python script as below:脚本“start_reader_services”调用 Python 脚本如下:

/root/java/tag_output >> $TAGS_PATH/tags_$DATE_FILE.log

tag_output basically prints out a series of IDs. tag_output 基本上打印出一系列 ID。 The same mechanics used to work when I was sending the stdout to my serial port (tag_output > /dev/ttyO0), but now, writing it to the file from cron, the file is created, but is empty.当我将标准输出发送到我的串行端口 (tag_output > /dev/ttyO0) 时,同样的机制曾经起作用,但现在,从 cron 将其写入文件,该文件已创建,但为空。

As I mentioned, running start_reader_services or any piece of that on command line, it works as expected.正如我所提到的,在命令行上运行 start_reader_services 或其中的任何部分,它都能按预期工作。

Have done: - Set bash as cron shell - Set java environments on cron已完成: - 将 bash 设置为 cron shell - 在 cron 上设置 java 环境

As requested:按照要求:

ls -l /root/java/tag_output
-rwxr-xr-x 1 root root 1981 Aug  6 12:06 /root/java/tag_output

First line of tag output:标签输出的第一行:

#!/usr/bin/python

Any help?有什么帮助吗?

Is it possible that tag_output is writing to stderr instead of stdout ? tag_output是否有可能写入stderr而不是stdout Try redirecting stderr too:也尝试重定向stderr

/root/java/tag_output >> $TAGS_PATH/tags_$DATE_FILE.log 2>&1

As an aside, you might also want to quote and use braces with the shell variable expansion;顺便说一句,您可能还想在 shell 变量扩展中引用和使用大括号; that makes it easier to read and probably a little safer:这使得阅读更容易,可能更安全:

/root/java/tag_output >> "${TAGS_PATH}/tags_${DATE_FILE}.log" 2>&1

After initialising and updating the crontab using crontab python and before issuing在使用 crontab python 初始化和更新 crontab 之后和发布之前

cron.write(), give the command, cron.__reduce__()[2]['lines'] = filter(None, cron.__reduce__()[2]['lines'])

Doing this will remove the empty lines being printed in Linux crontab.这样做将删除 Linux crontab 中打印的空行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM