Page 1 of 1

Why Cron works, but nothing happens when launch bash script?

Posted: Sun Jun 17, 2018 2:14 pm
by jurchello
I created bash script "auto-gzip.sh" to zip my public files:

Code: Select all

#!/bin/bash

cd /home/admin/web/site.com/public_html/
find . -name "*.gz" -type f -delete
gzip -kfr --best .
Added this file to cron via Vesta interface:

Code: Select all

sudo bash /home/admin/web/site.com/scripts/auto-gzip.sh
Try this command manually - it works good. But it doesn't work via Cron although logs are added every minute:

Code: Select all

Jun 17 09:11:01 source CRON[6307]: (admin) CMD (sudo bash /home/admin/web/site.com/scripts/auto-gzip.sh)
So, Cron works, but nothing happens. Why?

Re: Why Cron works, but nothing happens when launch bash script?

Posted: Tue Jun 19, 2018 5:14 am
by plutocrat
Quite often problems with cron are down to not finding the correct paths for executables. Try to specify the full path to find and gzip for a start.
~$ which gzip
/bin/gzip
~$ which find
/usr/bin/find
So ...

Code: Select all

cd /home/admin/web/site.com/public_html/
/usr/bin/find /home/admin/web/site.com/public_html/ -name "*.gz" -type f -delete
/bin/gzip -kfr --best .
I'd also suggest not storing the gz file in the web root as that's publicly accessible.

You might also try adding the cron job not using the vesta cron interface (which will run the commands with sudo), but by ssh-ing into the server and using 'crontab -e'. You'll see all the vesta cronjobs at the top. You can just add yours at the bottom:
# My cronjobs:
30 11 * * * /home/admin/path/to/script/backup.sh > /dev/null 2>&1