简体   繁体   中英

cronjob to run java program

I am trying to run a java program using cronjob, let's suppose it is the HelloWorld example. When I do which java I get the following path /opt/java/bin/java and when I try to the program in the cronjob it does not run, I think cronjob can not see the absolute path of java because when I do the following:

* * * * * /opt/java/bin/java -help > /absolute/path/to/file.txt

or the following

* * * * * /opt/java/bin/java HelloWorld > /absolute/path/to/file.txt

I get an empty file as a result. cronjob runs because I get the file.txt created and java also works because I can run my HelloWorld manually using the absolute path of java.

First of all, output from a cronjob is normally mailed to the user which the cronjob runs as. So if your mailsystem is correctly configured (you can run date | mail $(whoami) and then read that mail) you should see what your program prints when run. That mail is sent if the program creates any output. If no output is created, no mail is sent.

Also note that program > out.log only redirects standard output. If you do not want standard error to keep going in the mail, you can redirect it with program 2> err.log . If you want both to go in the same output file use program > out.log 2>&1 . All this is standard Unix so you can use any system administration book on Linux to learn more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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