# Crontab Bash Problem



## Wolfi83 (Sep 8, 2011)

Hello there,

I have great problems with crontab and bash-shells
I wrote some shells for mysql-backup and some other features.
The crontab does not execute the bash-shells, whats the problem?
I am not very special at crontab, so i need help, it is very important for me.
Is it important, to set a new PATH like:


```
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin[color="Red"](:/usr/bin/env:/usr/local/bin/bash)[/color]
```
And the SHELL like:

```
SHELL=/bin/sh[color="Red"](:/bin/bash)[/color]
```


----------



## Wolfi83 (Sep 8, 2011)

Hello,

Problem is fixed now, had an error in my syntax
It must like look this:

```
*	*	*	*	*   root	/usr/local/bin/bash   /root/backup.sh
```


----------



## Wolfi83 (Sep 8, 2011)

Hey,

Now i got a other problem with crontab and bash -.-
If i run the backup-shell via terminal (bash backup.sh) it run AND works.
If the crontab run the backup-shell every minute (/usr/local/bin/bash /root/backup.sh) it run but do not work, on my log theres a error '127' which comes from mysqldump?


----------



## Wolfi83 (Sep 8, 2011)

If i set the real bin-paths it works, but i want to have it via which-xxx u know?
so i have to add a line in pfad/shell at crontab file?


----------



## vwe@ (Sep 8, 2011)

Wolfi83,

you may want to make use of the search path. From your postings, I'm able to suggest to make yourself more familiar with how the shell really works. The environment of cron is different to your interactive shell.

Running a complex script every minute from cron may not be a good idea. Depending on how clever you've written your script and how long it takes to complete, it may end in  overlapped running scripts disturbing each other.


----------



## Wolfi83 (Sep 8, 2011)

vwe@ said:
			
		

> Wolfi83,
> 
> you may want to make use of the search path. From your postings, I'm able to suggest to make yourself more familiar with how the shell really works. The environment of cron is different to your interactive shell.
> 
> Running a complex script every minute from cron may not be a good idea. Depending on how clever you've written your script and how long it takes to complete, it may end in  overlapped running scripts disturbing each other.



That was only a test for every minute


----------



## DutchDaemon (Sep 8, 2011)

Also, please consider running  jobs from the root user's crontab (*crontab -e*), not from the system crontab in /etc/crontab. The latter can be altered or overwritten during an OS upgrade.


----------



## Wolfi83 (Sep 8, 2011)

Ah, okay thanks.
After a system reboot i must create a new crontab or is it saved?


----------



## anomie (Sep 8, 2011)

Any changes you make to root's crontab via `# crontab -e` are permanently saved in the file /var/cron/tabs/root. (That's just FYI. Do _not_ edit that file directly. Use the command instead.) 

As for the more general topic, unless your scripts really require bash (i.e. they use arrays or certain environment variables), you might consider writing them for Bourne shell (sh(1)). The primary argument for this approach is portability -- in my case that means a large number of scripts can be shared between FreeBSD and Linux servers, the former of which may or may not have bash installed.


----------



## Wolfi83 (Sep 9, 2011)

So the crontab is saved if i reboot the system and after the systemreboot i dont have to add the crontab again?


----------



## graudeejs (Sep 9, 2011)

DutchDaemon said:
			
		

> Also, please consider running  jobs from the root user's crontab (*crontab -e*), not from the system crontab in /etc/crontab. The latter can be altered or overwritten during an OS upgrade.



On other hand you can forget to backup root crontab  and lose it next time you reinstall


----------



## anomie (Sep 9, 2011)

Wolfi83 said:
			
		

> So the crontab is saved if i reboot the system and after the systemreboot i dont have to add the crontab again?



Si, senor. Assuming you follow the recommendations on this thread.


----------



## anomie (Sep 9, 2011)

graudeejs said:
			
		

> On other hand you can forget to backup root crontab  and lose it next time you reinstall



So back it up.  Or use one of the handy dump(8) tutorials you've written and advocated in favor of for disaster recovery.


----------



## graudeejs (Sep 9, 2011)

anomie said:
			
		

> So back it up.  Or use one of the handy dump(8) tutorials you've written and advocated in favor of for disaster recovery.



So, why is it better to write to root contab, instead of system contab?
I write to system contab, because when I do dangerous things, I backup whole /etc/
However I usually forget about stuff in /var/

That's 2 Cents.
What I'm saying is that it really doesn't matter where you write it. If you're not careful, you will have to write it again


----------



## anomie (Sep 9, 2011)

/etc/crontab has a higher likelihood of being overwritten during an upgrade. (You could probably manage that possibility with mergemaster(8), though.) On top of that, my gut feeling is most sysadmins would be surprised to see /etc/crontab tweaks on a system they inherit. It's not a usual place for customizations, IMO. 

If you don't like the idea of using root's crontab, you can always add a periodic(8) job instead.


----------



## graudeejs (Sep 9, 2011)

anomie said:
			
		

> /etc/crontab has a higher likelihood of being overwritten during an upgrade. (You could probably manage that possibility with mergemaster(8), though.) On top of that, my gut feeling is most sysadmins would be surprised to see /etc/crontab tweaks on a system they inherit. It's not a usual place for customizations, IMO.



At least that makes some sense.
I've ready many times "Don't use system contab, use root crontab", however no further explanation.
Thanks


----------



## DutchDaemon (Sep 10, 2011)

Did you seriously miss this in the post I dedicated to not using the system's crontab ten posts up?



> The latter can be *altered* or *overwritten* during an *OS upgrade*.


----------



## graudeejs (Sep 10, 2011)

DutchDaemon said:
			
		

> Did you seriously miss this in the post I dedicated to not using the system's crontab ten posts up?



Nope... simply, that doesn't concern me, as I'm super careful with mergemaster


----------

