Bash script runs manually but not in cron

bigdessert

Active Member
Jul 21, 2006
37
0
156
I have a bash script that is like this:

Code:
#!/bin/bash

rm -rf /home/domain/mail/domain.com/user/cur/*
rm -rf /home/domain/mail/domain.com/user/maildirsize
If I run the script locally it works correctly. If I schedule it via root crontab job per the cron log it runs but doesn't actually delete the files like it should.

Any Ideas why it runs manually but via cron it doesn't delete any files?

Also on another side note, obviously this is being used to delete all email in a users inbox. Is this a proper way to do this or should I be performing this task a different way?
 

bigdessert

Active Member
Jul 21, 2006
37
0
156
Code:
4,19,34,49 * * * * /usr/local/cpanel/whostmgr/bin/dnsqueue > /dev/null 2>&1
0 0 * * * /usr/local/cpanel/whostmgr/docroot/cgi/cpaddons_report.pl --notify
*/5 * * * * /usr/local/cpanel/bin/dcpumon >/dev/null 2>&1
MAILTO=""
*/45 * * * * /home/smilecen/backup
10 4 * * * cd /usr/local/assp;perl /usr/local/assp/rebuildspamdb.pl
*/59 * * * * /usr/local/cpanel/3rdparty/bin/php-cgi /usr/local/assp/deluxe/ex_localdomains.php clean_logs=yes
*/3 * * * * /usr/local/cpanel/3rdparty/bin/php-cgi /usr/local/assp/deluxe/update_email.php debug=yes
*/4 * * * * /usr/local/cpanel/3rdparty/bin/php-cgi /usr/local/assp/deluxe/spam_cronjob.php
26 3 * * * /usr/local/cpanel/3rdparty/bin/php-cgi /usr/local/assp/deluxe/clear_spambox.php sday=5 noemail=yes
*/20 * * * * /usr/local/cpanel/3rdparty/bin/php-cgi /usr/local/assp/deluxe/find_abusers.php sw=10 rl=15 sc=15 er=8 lm=8 dc=10 on=1 bl=1 lu=8 sz=250
*/1 * * * * /usr/local/cpanel/3rdparty/bin/php-cgi /usr/local/assp/deluxe/status.php
*/2 * * * * /usr/local/cpanel/3rdparty/bin/php-cgi /usr/local/assp/deluxe/status.php check_ssl=yes
0 4 * * * /usr/local/cpanel/3rdparty/bin/php-cgi /usr/local/assp/deluxe/signatures.php
*/5 * * * *  /usr/local/bin/php /scripts/custom/dynamic_update.php
0 3 * * * /scripts/custom/backup.sh
0 0 * * * /scripts/custom/exchange.sh > /exchange.log 2>&1
MAILTO=root
*/15 * * * * /scripts/custom/PWSmonitorchanges.cgi >/dev/null 2>&1
*/15 * * * * /scripts/custom/alert/PWSmonitorchanges.cgi >/dev/null 2>&1
30 */4 * * * /usr/bin/test -x /usr/local/cpanel/scripts/update_db_cache && /usr/local/cpanel/scripts/update_db_cache
45 */8 * * * /usr/bin/test -x /usr/local/cpanel/bin/optimizefs && /usr/local/cpanel/bin/optimizefs
20 4 * * * /usr/local/cpanel/scripts/upcp --cron > /dev/null 2>&1
0 7 * * * /usr/local/cpanel/scripts/cpbackup > /dev/null 2>&1
35 * * * * /usr/bin/test -x /usr/local/cpanel/bin/tail-check && /usr/local/cpanel/bin/tail-check
30 */2 * * * /usr/local/cpanel/bin/mysqluserstore >/dev/null 2>&1
15 */2 * * * /usr/local/cpanel/bin/dbindex >/dev/null 2>&1
25 4 * * * /usr/bin/freshclam --quiet --no-warnings
0 6 * * * /usr/local/cpanel/scripts/exim_tidydb > /dev/null 2>&1
0 6 * * * /usr/local/cpanel/scripts/exim_tidydb > /dev/null 2>&1
45 */4 * * * /usr/bin/test -x /usr/local/cpanel/scripts/update_mailman_cache && /usr/local/cpanel/scripts/update_mailman_cache
15 */6 * * * /usr/local/cpanel/scripts/recoverymgmt >/dev/null 2>&1
2,58 * * * * /usr/local/bandmin/bandmin
0 0 * * * /usr/local/bandmin/ipaddrmap
 

sirdopes

Well-Known Member
PartnerNOC
Sep 25, 2007
141
0
66
Do you see the cron running in /var/log/cron ? Also, is there a reason that you are deleting the email? I guess i am confused on why you would want an email address setup in the first place if you are erasing the email.
 

bigdessert

Active Member
Jul 21, 2006
37
0
156
Yes, it is showing as run in var/log/cron.

The reason for having the email is the email comes to our server first, then gets static routed in exim to another host. We do spam checking and it does collection to a spam folder in the users mailbox. In order for spam collection to happen there has to be a mailbox. Because all emails are being forwarded I don't need to keep copies in the user inbox as it will just keep growing. This was my simple solution to clean their inbox daily.
 

d-woo

Well-Known Member
Aug 9, 2003
88
0
156
in /var/log/cron where you see that the cron is running, just before CMD you will see in parentheses either (root) or ({username})

If it is root, I'm not sure what the issue is.

I entered my cron via cPanel, so it is run as {username} and not root.

So...if as {username} you will need to ensure that your script:
a. is chown as {username}:{username}
b. is chmod as 744 (executable)
c. is located in /home/{username}

so when you enter your cron command in cPanel it will look like:
/bin/bash /home/{username}/backup.sh

Now...if your script is writing to or accessing an area other than /home/{username} then it will not work.

You will need to run the cron as root.

My script was backing up to /var/lib/mysql/backups...it ran perfectly when I tested it as root, but during the Cron job it ran, but nothing was executed because it was run as {username} because {username} was not allowed into /var/lib/mysql

Hope this helps you.
 
Last edited:

cPanelTristan

Quality Assurance Analyst
Staff member
Oct 2, 2010
7,607
43
348
somewhere over the rainbow
cPanel Access Level
Root Administrator
What is the actual cronjob in /var/spool/cron/root location? You mention is being run by the root user and you provided the full crontab entry for root, but I am uncertain what cronjob it would be under that file. Please only provide the entry for that specific cronjob in /var/spool/cron/root location.

Also, it might be better to have the full path to rm rather than the partial path in the script you are using:

Code:
#!/bin/bash

/bin/rm -rf /home/domain/mail/domain.com/user/cur/*
/bin/rm -rf /home/domain/mail/domain.com/user/maildirsize