1

I wrote a bash file in which I am executing java file, its working properly if I am executing it but when I am trying with crontab it is not ,please help.

this is my crontab :

*/5 * * * * /home/import.sh >/dev/null 2>&1

this is my bash file:

                - me=$(date +%Y-%m-%d)
                mkdir -p  /home/importRequirement"$foldername"
                  {
                  java -jar ImportRequirement1.jar 
                  java -jar ImportRequirement1.jar 
                  }

         2>importRequirement"$foldername"/log$(date +%Y-%m-%d-%H-%M-%S).txt

I have deleted the url.

fedorqui
  • 275,237
  • 103
  • 548
  • 598

2 Answers2

1

When a script is intended to be started from a non interactive environment (cron or init), none of the usual goodies such as custom path or other environment variable are set.

The rule is:

  • ensure all the commands (except at most those in /bin and /usr/bin) use full path
  • ensure all required environment variables are set

If you use many scripts that way, you could build a setenv script that declares all environment variables and create ones for every command you use. Here it will contain (more or less):

export JAVAHOME=...
export JAVA=/path/to/java

Then you can use in your script:

$(JAVA) -jar ImportRequirement1.jar

but here again you should either have a previous cd to the expected directory or use absolute path of jar

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

The environment variables for cron jobs is not the same as what users get when they are logged in. Double check the environment variables you need to run your script (ie JAVA_HOME and PATH). cron might not know with what to interpret your script, you might want to place

#!/bin/bash

on the first line of your bash script.

louigi600
  • 716
  • 6
  • 16