# cron job runs, but doesn't do anything



## ant111 (Nov 12, 2009)

Hi all,

I have difficulties setting up a cron job. This is the relevant piece of my crontab:

```
*/5     *       *       *       *       root    bash /path/to/script.sh | mail -s SUCCESS my_email
```

I've set it to run every 5 minutes, and every 5 minutes, I get an email.. So I know it runs. When I tail /var/log/cron, it shows that it's successful:


```
Nov 12 07:35:01 gc /usr/sbin/cron[76657]: (root) CMD (bash /path/to/script.sh | mail -s SUCCESS my_email)
```

However.. that's where it ends - nothing happens! The script is supposed to upload some files through FTP, and it doesn't do that. WHen I run the same exact command (bash /path/to/script.sh) from the command line, it executes the script and everything is cool... 

I tried redirecting the output from the script to a local file - nothing happens. I've set the permissions for the script and all the files in the folder to 777, still no result ...

I'm at loss, don't even know where to look for further debugging.. Any ideas?


----------



## SirDice (Nov 12, 2009)

Can you show the script?

And as a side note, try not to use bash use the 'regular' bourne shell (/bin/sh) for scripting.

Also note that _any_ output created by a cronjob will get mailed anyway. Just run it on an account and set the /etc/aliases file accordingly.


----------



## DutchDaemon (Nov 12, 2009)

Also avoid using the system's crontab (/etc/crontab), use the root user's crontab (`# crontab -e` as the root user) instead -- you may lose your cron jobs due to a careless system upgrade run. Note that the sixth field (the user to run as) will no longer be needed in a user's crontab.

Cron has a limited path, so remember to either set a full path in the crontab, or use full paths to all commands in scripts.


----------



## SirDice (Nov 12, 2009)

DutchDaemon said:
			
		

> use the root user's crontab (`# crontab -e` as the root user) instead


In addition, if the script only FTPs some files, don't use root for that.


----------



## ant111 (Nov 12, 2009)

Hi all,

I've changed the command to /bin/sh, and added it to "crontab -e" - same result 

This is what my "crontab -e" looks like


```
*/5     *       *       *       *       /bin/sh /root/cmp/htdocs/cron_jobs/upload_indeed.sh >> /root/cmp/htdocs/cron_jobs/indeed.log
```

And I know it runs, this is the log:


```
Nov 12 18:35:01 gc /usr/sbin/cron[99636]: (root) CMD (/bin/sh /root/cmp/htdocs/cron_jobs/upload_indeed.sh >> /root/cmp/htdocs/cron_jobs/indeed.log)
```

This is the script:

```
bash-3.2# more upload_indeed.sh
#!/bin/sh

cd /root/cmp/htdocs/cron_jobs
php indeed_ftp.php
```

The php file uploads some stuff through FTP. When I run the sh command exactly as above from command line, everything is ok,and it uploads... But through cron job, even though it shows as ok in the log, nothing is sent, and indeed.log file is not updated, either.

What am I missing? Thank you all very much for your help!


----------



## SirDice (Nov 12, 2009)

Remove that /bin/sh from crontab and make the /root/cmp/htdocs/cron_jobs/upload_indeed.sh executable. 
You could even run that php script directly from cron, the script doesn't add anything.


```
*/5     *       *       *       *     /usr/local/bin/php /root/cmp/htdocs/cron_jobs/indeed_ftp.php >> /root/cmp/htdocs/cron_jobs/indeed.log 2>&1
```

The 2>&1 will redirect STDERR to STDOUT, STDOUT is redirected to the log file, so both STDERR and STDOUT will end up in it.


----------



## ant111 (Nov 12, 2009)

It's finally working!!!! WHOOOOOOOOOOO!!!! 

THANK YOU SO SO MUCH!!!!!!!!


----------



## SirDice (Nov 12, 2009)

:beergrin


----------

