# crontab woes



## chellea (Jul 24, 2013)

I have a script in a jail, /home/aisling/BackupJob.sh, that calls a job that backs up to an external server. After the backup is complete the server sends me an email with the details, which works great when I fire the script manually. The problem is when I add the job to crontab, After the scheduled time I run `tail /var/log/cron` and see no errors, but the backup does not happen. Below is the cron job and the results of the tail.

Thank you.


```
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
#
#minute hour mday month wday who command 3
#
44 08 * * * root /home/aisling/BackupJob.sh
```
 

```
Jul 24 08:44:00 syncrify /usr/sbin/cron[12337]: (operator) CMD (/usr/libexec/save-entropy)
Jul 24 08:44:00 syncrify /usr/sbin/cron[12338]: (root) CMD (root /home/aisling/BackupJob.sh)
Jul 24 08:45:00 syncrify /usr/sbin/cron[12370]: (root) CMD (/usr/libexec/atrun)
```


----------



## ondra_knezour (Jul 24, 2013)

98% probability that your script does not use full paths


----------



## SirDice (Jul 24, 2013)

Yep, that would be my guess too :e

And I suggest using `# crontab -e` and leave the system's /etc/crontab alone. The reason is that /etc/crontab may get overwritten during an update. Whereas a user's (that includes the root user) crontab isn't.


----------



## chellea (Jul 24, 2013)

Yes, that makes sense, thank you. I believe the issue lies in my use of JRE in the script:


```
java -Dsyncrify.custom.home=/root/.syncrify -jar /home/aisling/SyncrifyClient.jar -console tcdc-backup.syncrify
```
 
My next problem as a noob is how do I find the path of the Java JRE?


----------



## SirDice (Jul 24, 2013)

chellea said:
			
		

> My next problem as a noob is how do I find the path of the Java JRE?




```
root@molly:~ # whereis -b java
java: /usr/local/bin/java
```


----------



## chellea (Jul 24, 2013)

So, I ran the following as instructed;


```
syncrify# whereis -b java
java: /usr/local/bin/java
```
 
but got the same results... Below is my script;


```
#!/bin/sh
/usr/local/bin/java -Dsyncrify.custom.home=/root/.syncrify -jar /home/aisling/SyncrifyClient.jar -console tcdc-backup.syncrify
```
 
I am clearly still doing something wrong, any thought would be greatly appreciated.


----------



## ondra_knezour (Jul 24, 2013)

It looks like the recommended cron method.

I would try following:
 - see if any logs exist
 - check related FreeNAS how-to (probably not too useful if everything works under an regular user)
 - check the file rights (same as above, script runs under the root account)
 - compare the PATH enviroment variable for your user and root. Put something like

```
printenv PATH > /home/user/cronpath.txt
```
at start of the failing script and then compare file content with `$ printenv PATH` as regular user


----------



## chellea (Jul 24, 2013)

The results were identical.

`printenv PATH`

```
/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin
```
 

```
#!/bin/sh
printenv PATH > /home/aisling/cronpath.txt /usr/local/bin/java -Dsyncrify.custom.home=/root/.syncrify -jar /home/aisling/SyncrifyClient.jar -conso
le tcdc-backup.syncrify
```

Result:


```
/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin
```
 
I am running the script manually as root, and the cron job is set to run as root as well.

Thanks.


----------



## ShelLuser (Jul 25, 2013)

chellea said:
			
		

> Below is my script;
> 
> 
> ```
> ...


You say that this script works as expected when you run it from the console. From which directory are you usually running this script, and even more importantly: have you ever tried to execute this script from another location?

Of course I'm not sure here, but I wonder if your Java program isn't using any external files or data (libraries for example), and therefore requires a specific environment to be in place.

For example; in their most basic form some Java programs need the classpath parameter to be set before you can execute them from another location. This is required so that the program knows where to look for any classes.

Although I doubt this is the case here (you're using a jar file after all which should contain everything required) it might be worth looking into.


----------



## ondra_knezour (Jul 25, 2013)

ShelLuser said:
			
		

> Although I doubt this is the case here (you're using a jar file after all which should contain everything required)



Good point. I have one java application, which has to be started from the application directory like `$ java -jar lib/app.jar [params]`, otherwise it runs but silently fails to work. Only long digging in the embeded Tomcat logs hints me what is going wrong.

Also running the failing utility under truss(1) may show something like file not found errors etc.

So you can try this

```
#!/bin/sh
/path/to/truss -fa -o /where/to/store/output /usr/local/bin/java -Dsyncrify.custom.home=/root/.syncrify -jar /home/aisling/SyncrifyClient.jar -console tcdc-backup.syncrify
```

and of course try @ShelLuser's advice

```
#!/bin/sh
cd /home/aisling #or directory where it works manually
/usr/local/bin/java -Dsyncrify.custom.home=/root/.syncrify -jar /home/aisling/SyncrifyClient.jar -console tcdc-backup.syncrify
```


----------

