JAR 文件在 crontab 执行时未从 Scanner(System.in) 读取输入

发布于 2024-12-12 18:57:13 字数 950 浏览 3 评论 0原文


我创建了一些应用程序,它使用以下方法从 System.in 读取:

    Scanner input = new Scanner(System.in);

    while (input.hasNextLine()) {

        String line = input.nextLine();

    }

输入数据通过 linux 命令传递:

cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main

crontab 条目如下所示:

MAILTO=someuser
CLASSPATH="/home/someuser/test.jar:/usr/share/java/jdom.jar:/usr/share/java/mysql-connector-java.jar"

0,10,20,30,40,50 * * * * cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main  >/home/someuser/output.txt

文件的权限显示以下内容:

-rw-r--r-- 1 someuser serhiy 8385601 2011-02-07 10:57 /home/someuser/somefile.txt

我的机器上一切正常( Ubuntu 9),但是在另一台机器 Ubuntu 8 上安装后,我发现程序启动了,但似乎没有读取任何内容。我已经三次检查了所有配置和所有权限,结果仍然相同。当我手动运行命令时,一切正常,当它由 crontab 运行时,它似乎没有读取输入。以前有人遇到过这个问题吗?

感谢您的帮助
谢尔希。

I have created some application, which is reading from System.in using the following method:

    Scanner input = new Scanner(System.in);

    while (input.hasNextLine()) {

        String line = input.nextLine();

    }

Input data is being passed with linux command:

cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main

The crontab entry looks like:

MAILTO=someuser
CLASSPATH="/home/someuser/test.jar:/usr/share/java/jdom.jar:/usr/share/java/mysql-connector-java.jar"

0,10,20,30,40,50 * * * * cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main  >/home/someuser/output.txt

The permissions for the files shows the following:

-rw-r--r-- 1 someuser serhiy 8385601 2011-02-07 10:57 /home/someuser/somefile.txt

Everything is working fine on my machine(Ubuntu 9), but after installation on another machine Ubuntu 8, I figured out that program starts but seems not to be reading anything. I have triple checked all configurations and all permissions and the result still the same. When I run command manually everything is working, when it's ran by crontab it seems not reading input. Anyone experienced this issues before?

Thanks for any help
Serhiy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2024-12-19 18:57:13

您在 crontab 中定义变量吗?这似乎不对。

1) 将命令移至 shell 脚本并从 cron 调用 shell 命令,例如

*/10 * * * * /home/someuser/some_script.sh >/home/someuser/some_script.cronoutput 2>&1

2) some_script.sh 的内容;确保设置了执行位

#!/bin/sh
export MAILTO=someuser  
export CLASSPATH="/home/someuser/test.jar:/usr/share/java/jdom.jar:/usr/share/java/mysql-connector-java.jar"  

cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main  >/home/someuser/output.txt

Are you defining the variables in crontab ? That does not seem right.

1) Move the command to a shell script and invoke the shell command from cron, eg

*/10 * * * * /home/someuser/some_script.sh >/home/someuser/some_script.cronoutput 2>&1

2) Contents of some_script.sh ; make sure the execute bit is set

#!/bin/sh
export MAILTO=someuser  
export CLASSPATH="/home/someuser/test.jar:/usr/share/java/jdom.jar:/usr/share/java/mysql-connector-java.jar"  

cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main  >/home/someuser/output.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文