Bash Script unable to run Java Program as cron job

余生长醉 提交于 2019-12-25 02:37:22

问题


I am trying to run a .class file from a bash script every 1 minute using the crontab. I get the error Could not find or load main class Cron_Read_Send_CapacityData_To_Graphite. I have already set environment variable on my machine.

If i run the bash script from the terminal it works fine.Note the bash script and the java class file are in the same folder

Script:

$ cat Run_Cron_Read_Send_CapacityData_To_Graphite.sh
#!/bin/bash

java Cron_Read_Send_CapacityData_To_Graphite >> /home/marshell/graphite_cronjobs/Cron_Read_Send_CapacityData_To_Graphite.log

Cron entry:

$ crontab -e

*/1 * * * * /home/marshell/graphite_cronjobs/Run_Cron_Read_Send_CapacityData_To_Graphite.sh >> /home/marshell/graphite_cronjobs/debug_cronjob.log 2>&1

Error:

Error: Could not find or load main class Cron_Read_Send_CapacityData_To_Graphite

回答1:


You error message indicates that your classpath isn't set correctly for the script to find Cron_Read_Send_CapacityData_To_Graphite.class, I would use something like -

CP=/home/marshell/graphite_cronjobs # The classpath to use
LF=$CP/Cron_Read_Send_CapacityData_To_Graphite.log # the log file
java -cp $CP Cron_Read_Send_CapacityData_To_Graphite >> $LF


来源:https://stackoverflow.com/questions/25894558/bash-script-unable-to-run-java-program-as-cron-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!