简体   繁体   English

Cron作业未运行jar

[英]Cron job not running the jar

This is my Java file for which I have created Delete.jar 这是我为其创建Delete.jar的Java文件

import java.io.*;

public class Delete {
 public static void main(String[] args)
    {
        try{
            int i =1;
            while(i<5){
                File directory = new File("downloads");
                System.out.println("I am running");
                for(File file: directory.listFiles()) file.delete();
                i++;
             }
        }catch(Exception e){

                e.printStackTrace();

        }

    }
}

This is my Script to run the jar file if it is not running 如果未运行jar文件,这是我的脚本

#!/bin/bash
processid=`pgrep -f 'Delete.jar high'`
echo "Processes:"$processid
if [ -n "$processid" ]
then
echo "Process is running. No action will be taken"
else
echo "Process is not running. Executing ResponseHandler-fast now !"
cd /home/ubuntu/;
java -jar Delete.jar high
fi

This is line I have added to my crontab -e 这是我添加到我的crontab -e中的行

* * * * * sh /home/ubuntu/check.sh

I rebooted my System I was expecting that my script will run check that jar is not running and it will run it but it is not doing so. 我重新启动了系统,当时我希望我的脚本可以运行,请检查jar是否未运行,并且它将运行它,但它没有运行。 What I am doing wrong here. 我在这里做错了。

If I execute ps after 2 -3 minutes still I am not getting java as an entry. 如果我在2 -3分钟后仍执行ps,则仍然无法获取java作为条目。

Thanks. 谢谢。

Please send output of your shell script to a log file as shown below(make changes to your crontab entry): 请将您的Shell脚本的输出发送到如下所示的日志文件中(对crontab条目进行更改):

* * * * * sh /home/ubuntu/check.sh >> /home/ubuntu/output.log 2>&1

In this way, you will know what exactly is being run and then finding the exact cause will become easier. 这样,您将知道正在运行的是什么,然后找到确切的原因将变得更加容易。

Did you try running /home/ubuntu/check.sh manually? 您是否尝试/home/ubuntu/check.sh手动运行/home/ubuntu/check.sh May be it's not finding the jar file or even java program. 可能是找不到jar文件甚至Java程序。

Cron doesn't magically make the program "run forever". Cron不会神奇地使程序“永远运行”。 Start the program manually. 手动启动程序。 It will probably take 1-2 seconds to run, then exit. 运行大概需要1-2秒,然后退出。 This is exactly what happens when running with cron, as well. 同样,在使用cron运行时,也会发生这种情况。 So, unless you run ps the second your program gets started, you won't see anything in the process list. 因此,除非您在程序启动的第二秒运行ps,否则在进程列表中将看不到任何内容。

Your loop 1..5 won't help, as after the files are deleted in the first round, the rest is effectively a no-op. 您的循环1..5将无济于事,因为在第一轮删除文件后,其余的实际上是无操作的。

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

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