# Using crontab for the first time



## c00kie (Sep 14, 2013)

Hi,

I'm struggling to get crontab to run shell scripts. I'm running the csh shell. I've tried both shebangs, csh and sh in my script. `# cd`

In /root/restart_mysqld.sh:

```
#!/bin/sh
/usr/local/etc/rc.d/mysql-server restart
```

And my `crontab -e`:

```
5 * * * * sh /root/restart_mysqld.sh
```

So I'm expecting cron to re-start mysqld every five minutes. It doesn't, it fails to execute. I've checked:

`# cat /etc/cron.allow`

```
cat: /etc/cron.allow: No such file or directory
```
 and
`# cat /etc/cron.deny`

```
cat: /etc/cron.deny: No such file or directory
```

Although I'm running cron as root so it shouldn't be a problem. If I run the script directly from the shell: 

`# sh restart_mysqld.sh`

```
mysql not running? (check /var/db/mysql/my.domain.net.pid).
Starting mysql.
```

...the script works fine, everything is back to normal, mysqld restarted, websites up and running. What am I doing wrong?


----------



## J65nko (Sep 14, 2013)

Instead of giving you a fish, I will teach you how to fish 


Run `# /usr/bin/env` and save the output somewhere.
Add the following three crontab tasks:


```
MAILTO = root 
# 
*/1     *       *       *       *       (date; /usr/bin/env; echo) >>/tmp/rootcron.txt
*/1     *       *       *       *       date; /usr/bin/env; echo Mailed
*/1     *       *       *       *       bla
```

In one terminal run `# tail -f /var/mail/root`
In a second one run `# tail -f /tmp/rootcron.txt`


----------



## ShelLuser (Sep 14, 2013)

c00kie said:
			
		

> In /root/restart_mysqld.sh
> 
> 
> ```
> ...


Two things come to mind here. First; why specify sh when you already have a shebang present in your shell script? That's a bit overkill, and it will most likely get you different results.

Second; I'd really recommend going over crontab(5) again. Because you didn't set a range up there, you simply set a static value which will result in a static time. So in this case you don't get a job which will run every five minutes. Instead you'll get a job which will run 5 minutes within the hour. And since you used asterisks in the rest of the fields this ends up with "_5 minutes within the hour, every hour._".

So in this case you should either fill out the rest of the minutes when this should run (0,5,10,15,20,25,30,35,40,45,50,55) or simply use a step value in the likes of */5.

(Edit)

PS: Resetting MySQL every 5 minutes doesn't sound like a healthy thing to do here


----------



## c00kie (Sep 14, 2013)

My server is in America although I'm in the UK:

`# history`

```
106	12:53	/usr/local/etc/rc.d/mysql-server stop // 17:53 English time
 107	12:53	history
```

and my mysql server was stopped @ at 17:53 manually, by myself at the command line. _I c_heck_ed_ back @ at 17:59 and it still hasn't started. Although @ at 18:05 it HAS restarted successfully. 

But that's a 12 min*ute* period, not 5? WTF!


----------



## c00kie (Sep 14, 2013)

ShelLuser said:
			
		

> PS: Resetting MySQL every 5 minutes doesn't sound like a healthy thing to do here



Indeed, it's mainly a test to see some quick results in the short term. I'm going to try (in crontab);


```
@daily /root/restart_mysqld.sh
```

for now, and see if it starts automatically given 24 hours or so. Thanks again.


----------

