简体   繁体   中英

Script doesn't work from cronjob

I have a java program that has to run over the DB to reindex the entries. This is a job that has to be done once a week. I have written a script that executes the program:

var=$(which java)
nohup $var -Xmx2048m -jar javaProgram.jar $* -d javaProgramResource -re > /nfs/inf/app_storage/logs/service/service_refresh.log 2>&1 &

The reason I put the Java path in a variable is becouse I want it to be generic, we have Java installed on different locations on every environment. By executing this script manually, it works perfect.

./reindex.sh

Now, I wanted to create a cron job that executes this script once a week (every saturday at 6 o'clock in the morning - the duration of this job is about 16 hours becouse there are a lot of entries in the DB).

0    6    *    *    6  cd /locattion/of/the/file; ./reindex.sh

Instead of getting the edited indexes in the log file (like when I start the script manually), I just get a message that says:

nohup: invalid option -- 'X'
Try `nohup --help' for more information

I guess it's a syntax error, but I'm not familiar with bash scripts and commands.

I found a solution. In the .profile file were all the variables needed in the script. I simply added the path of the .profile file to the cronjob and it worked just fine.

0    6    *    *    6  . $HOME/.profile; cd /locattion/of/the/file; ./reindex.sh

Now the cronjob knows the Java-Path (becouse it's written in the .profile file) and the name of javaProgramResource, which is also different on every environment. You guys gave me some very important input in which direction I should continue my investigation on this topic. Thanks!

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