Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running java code from CRON job

Tags:

java

sh

cron

Facing a problem while running Java code from CRON job.

I am trying to run a ".sh" file from CRON like this:

* * * * * root sh /run-test.sh >> /var/log/cron.log
# empty line

and this run-test.sh has:

java HelloWorld

When, I navigate to root folder i.e. "/" and run command:

sh run-test.sh

It will run my HelloWorld class properly and gives me desired output.

But, through CRON tab it always say's:

Error: Could not find or load main class HelloWorld

I am not able to Judge what can be the reason for this. Immediate replies will be helpful. Thanks

like image 507
gomzee Avatar asked May 18 '26 20:05

gomzee


1 Answers

you might need to change the current working directory for cron.

* * * * * root cd / && sh /run-test.sh >> /var/log/cron.log

you dont have any absolute paths in your script , so it will be running relative to the working directory.

like image 141
Tim Marsh Avatar answered May 23 '26 05:05

Tim Marsh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!