Having issues getting cron job to work

Tipiford

Active Member
Feb 5, 2014
26
1
3
cPanel Access Level
Website Owner
I am still wrestling with this issue. It seems like the staff here at cpanel could offer more solutions or at least some suggestions as to what to try next. As it now stands, here is the code that is not working:

find /home2/mail/domain.com/donotreply/cur -type f "\*.\*" -mtime +2 -exec rm {} \;

And here is the cron email I am getting:

find: paths must precede expression
Usage: find [path...] [expression]

To me, it appears that this is a path issue, could it be that "home2" is a subset of "home" and is blocking any code in the crontab?

Thanx,

T
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,904
2,237
463
It seems like the staff here at cpanel could offer more solutions or at least some suggestions as to what to try next. As it now stands, here is the code that is not working:
It's generally against company guidelines to offer guidance on custom bash scripts, particularly concerning commands that involve removing files. That being said, if you look at the first post from page two of this thread, I offered an updated command, tested it, and confirmed it should work well if you replace the values with your own account information. The script from your last response does not reflect that script.

Thank you.
 

Tipiford

Active Member
Feb 5, 2014
26
1
3
cPanel Access Level
Website Owner
I am fully aware of any disclaimer regarding company policy. I have forever been responsible for my own actions, and have never looked to the government, any company or any other person to be accountable for my actions.

The difference in scripts is attributed to 1.) originally copying the path from my FTP client which did not show the "home2" part of the path, and 2.) I added "\*.\*" to the mix based on forum member Vanessa's suggestion. But neither,
find /home2/mail/domain.com/donotreply/cur -type f -mtime +5 -exec rm {} \;
nor
find /home2/mail/domain.com/donotreply/cur -type f "\*.\*" -mtime +5 -exec rm {} \;
is working, and therefore am suspecting a path issue.
Could the "home2" be a pseudo address blocking me from the "true" address?
Thanx
T
 

vanessa

Well-Known Member
PartnerNOC
Sep 26, 2006
833
28
178
Virginia Beach, VA
cPanel Access Level
DataCenter Provider
find /home2/mail/domain.com/donotreply/cur -type f -mtime +5 -exec rm {} \;
What I see wrong with this is your path. You don't have a username after /home2:

Code:
find /home2/USER/mail/domain.com/donotreply/cur -type f -mtime +5 -exec rm {} \;

I tested this exactly on one of my servers and it works fine. Stop being so whiney.
 
  • Like
Reactions: pabumake

magicalwonders

Well-Known Member
Nov 21, 2012
112
2
18
cPanel Access Level
Root Administrator
I am fully aware of any disclaimer regarding company policy. I have forever been responsible for my own actions, and have never looked to the government, any company or any other person to be accountable for my actions.
But you have been looking for help. :)

Forum members have tried to assist you as best they can, (and solved your problem) yet you have come across as a bit ungrateful. That may not have been your intention, but that is how it looks.
 

hhazeley

Registered
Mar 7, 2014
2
0
1
cPanel Access Level
Reseller Owner
I believe you are looking for a command more like this:

Code:
find /home/cphwtest04/mail/domain.com/cptest1/cur -type f -mtime +5 -exec rm {} \;
Ensure you are careful when using any command with "rm" as any slight typing mistake could result in the deletion of additional files.

Thank you.
I used the code format above email got deleted but it still showing i have 99% usage but there are no mails. Did i miss a switch or something?

Thanks
 

vanessa

Well-Known Member
PartnerNOC
Sep 26, 2006
833
28
178
Virginia Beach, VA
cPanel Access Level
DataCenter Provider
I used the code format above email got deleted but it still showing i have 99% usage but there are no mails. Did i miss a switch or something?

Thanks

Wait for your mail quota to update (happens every night during cpanel updates), or delete the quota file from the email account and let it regenerate.
 

magicalwonders

Well-Known Member
Nov 21, 2012
112
2
18
cPanel Access Level
Root Administrator
I seem to be having an issue getting this working. I have set up two cron jobs to empty two mailboxes. Here's the cron for one of them -

find /home/magicalw/mail/domain.com/myles/cur -type f -mtime +1 -exec rm {} \;
This is set to run once a day. 0 0 * * *

The first time I add the cron it seems to work just once and the mailbox is emptied. However, no cron output is received. After that the mailboxes continue to accumulate mail.

If I edit the cron to change the frequency of when it runs, it will action successfully again just once and empty boxes, and then fail again when it is next due to run.

Not sure what is happening. I'd be grateful If anyone can advise on what I can try next?

Many thanks,

Myles
 
Last edited by a moderator:

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,904
2,237
463
The first time I add the cron it seems to work just once and the mailbox is emptied. However, no cron output is received. After that the mailboxes continue to accumulate mail.
Have you verified the "crond" service is running? Try restarting it via:

Code:
service crond restart
Also, did you verify emails exist in the directory right after the cron job runs?

Thank you.
 

magicalwonders

Well-Known Member
Nov 21, 2012
112
2
18
cPanel Access Level
Root Administrator
Have you verified the "crond" service is running? Try restarting it via:

Code:
service crond restart
I've looked in WHM and the "Crond" is enabled. Although the tick is much greyer than other items enabled. I don't know if that's relevant. I have another cron job for the domain which does produce a daily report, so that part of the system does seem to be working OK.

Should I still restart the service? Would I do that from the command line? and use "service crond restart"

Also, did you verify emails exist in the directory right after the cron job runs?
I've checked the mailbox in Webmail when the cron first runs or if I edit it. (changing frequency of cron) This does delete a bunch of emails, but then it seems to fail after that. I've just edited one cron to run every five minutes. The mailbox is now showing mail from 20th April, having deleted anything prior to that. I'll check again tomorrow and see if there is any change.
 

magicalwonders

Well-Known Member
Nov 21, 2012
112
2
18
cPanel Access Level
Root Administrator
Just an update to my last mail.

I should have mentioned that the other cron job on the domain that produces a report is for an autoresponder script, and nothing to do with emptying mailboxes.

I've just checked my mailbox for the cron that I set to run every five minutes, and it shows emails from 20th April. So the cron doesn't appear to have impacted on the mailbox since I edited it.

I looked at the disk usage for the email account in cPanel and it's showing usage of 55mb for that mailbox! I only have 22 emails in there according to Webmail, so not sure what has gone wrong there. I checked another account and emptied a mailbox that showed 10mb usage. After logging out. clearing cache, and logging back in, the disk usage is still showing 10mb for that email account.

It does look like something is broken?

I've just downloaded the log from - /var/log/cron and here is an extract for today -

Code:
Apr 22 11:45:01 webhost1 CROND[10119]: (magicalw) CMD (find /home/magicalw/mail/domain.com/myles/cur -type f -mtime +1 -exec rm {} \; )
Apr 22 11:45:01 webhost1 CROND[10121]: (root) CMD (/usr/local/cpanel/bin/dcpumon >/dev/null 2>&1)
Apr 22 11:50:01 webhost1 CROND[11397]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Apr 22 11:50:01 webhost1 CROND[11401]: (root) CMD (/usr/local/maldetect/maldet --mkpubpaths >> /dev/null 2>&1)
Apr 22 11:50:01 webhost1 CROND[11404]: (magicalw) CMD (find /home/magicalw/mail/domain.com/myles/cur -type f -mtime +1 -exec rm {} \; )
Apr 22 11:50:01 webhost1 CROND[11405]: (root) CMD (/usr/local/cpanel/bin/dcpumon >/dev/null 2>&1)
Apr 22 11:55:01 webhost1 CROND[11656]: (magicalw) CMD (find /home/magicalw/mail/domain.com/myles/cur -type f -mtime +1 -exec rm {} \; )
Apr 22 11:55:01 webhost1 CROND[11659]: (root) CMD (/usr/local/cpanel/bin/dcpumon >/dev/null 2>&1)
Apr 22 11:58:01 webhost1 CROND[11802]: (root) CMD (/usr/local/bandmin/bandmin)
Apr 22 11:58:01 webhost1 CROND[11804]: (root) CMD (/usr/local/cpanel/whostmgr/bin/dnsqueue > /dev/null 2>&1)
Apr 22 12:00:01 webhost1 CROND[11902]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Apr 22 12:00:01 webhost1 CROND[11904]: (root) CMD (/usr/local/maldetect/maldet --mkpubpaths >> /dev/null 2>&1)
Apr 22 12:00:01 webhost1 CROND[11914]: (magicalw) CMD (/usr/bin/perl /home/magicalw/public_html/cgi-bin/arp3/arp3-auto.pl 1>&2 > /dev/null)
Apr 22 12:00:01 webhost1 CROND[11917]: (root) CMD (/usr/local/cpanel/bin/dcpumon >/dev/null 2>&1)
Apr 22 12:00:02 webhost1 CROND[11920]: (magicalw) CMD (find /home/magicalw/mail/domain.com/myles/cur -type f -mtime +1 -exec rm {} \; )
Apr 22 12:01:01 webhost1 CROND[12004]: (root) CMD (run-parts /etc/cron.hourly)
Apr 22 12:01:01 webhost1 run-parts(/etc/cron.hourly)[12004]: starting modsecparse.pl
Apr 22 12:01:01 webhost1 run-parts(/etc/cron.hourly)[12011]: finished modsecparse.pl
Apr 22 12:02:01 webhost1 CROND[12051]: (root) CMD (/usr/local/bandmin/bandmin)
It does show that the cron job - find /home/magicalw/mail/domain.com/myles/cur -type f -mtime +1 -exec rm {} \; is running every five minutes.

Not sure what the next step might be?
 
Last edited by a moderator:

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,904
2,237
463
The custom cron job itself falls outside our scope of support, and in reality is not a good solution to the problem. It was offered as a command for a user that determined it was absolutely necessary. In reality, the better solution is to determine why the email account is filling up with unnecessary emails. Have you reviewed the messages to determine where they are coming from?

Note that email disk space usage is only recalculated every four hours.

Thank you.
 

magicalwonders

Well-Known Member
Nov 21, 2012
112
2
18
cPanel Access Level
Root Administrator
The custom cron job itself falls outside our scope of support, and in reality is not a good solution to the problem. It was offered as a command for a user that determined it was absolutely necessary. In reality, the better solution is to determine why the email account is filling up with unnecessary emails. Have you reviewed the messages to determine where they are coming from?
The emails are just regular mail. I have forwarders setup, so the mail is not downloaded. I just wanted a system that would empty the mailboxes automatically so I wouldn't have to keep logging in and doing it manually. My autoresponder needs to be a catch-all address that is emptied regularly. The perl script that was supposed to do this is always triggering cron errors. So it is absolutely necessary I find a solution to emptying my mailboxes.

Note that email disk space usage is only recalculated every four hours.
It's been about five or six hours since I last checked. Disk usage is now showing as 56mb. I have an extra 9 messages showing in webmail. The extra 1mb is about right looking at the size of the latest mail (images embedded) However, a total of 56mb is way wrong. About 2mb would be nearer!
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,904
2,237
463
Feel free to open a support ticket so we can take a closer look. You can post the ticket number here so we can update this thread with the outcome.

Thanks.
 

kelvin1950

Registered
Nov 17, 2014
1
0
1
Hook, Wokingham, United Kingdom, United Kingdom
cPanel Access Level
Reseller Owner
Hi ladies and gents,
This is just what I'm looking for as I have some mailboxes that are accessed by two or more people and the mail is downloaded but left on the server. ATM I'm deleting it by hand but it's a PITA! I want to delete mail after 30 days so may I sense check the script with you please?

find /home/SERVER_LOGIN_USERNAME/mail/DOMAIN_NAME/MAIL_ACCOUNT_NAME/cur -type f -mtime +30 -exec rm {} \;

TIA.
 

magicalwonders

Well-Known Member
Nov 21, 2012
112
2
18
cPanel Access Level
Root Administrator
Hi ladies and gents,
This is just what I'm looking for as I have some mailboxes that are accessed by two or more people and the mail is downloaded but left on the server. ATM I'm deleting it by hand but it's a PITA! I want to delete mail after 30 days so may I sense check the script with you please?

find /home/SERVER_LOGIN_USERNAME/mail/DOMAIN_NAME/MAIL_ACCOUNT_NAME/cur -type f -mtime +30 -exec rm {} \;

TIA.
Hi Kelvin,

I had problems with the cron as the usage quota for mailbox wasn't being updated. My support ticket with cPanel resulted in the following bash script. You can call it anything you like, but it needs to be saved with a sh extention. Mine is clearmail.sh

Code:
#!/bin/bash
# remove mail from [email protected] email account.
find /home/exampled/mail/exampledomain.com/info/ -type d \( -name cur -o -name new \) -exec find {} -type f -delete \;
# regenerate maildirsize (to update quota)
/scripts/generate_maildirsize --confirm exampled
This worked well when tested, and the usage quota of mailbox would be updated to zero MB each time.

You would just set a cron job to run this script to the frequency you require.

Hope this helps. :)
 

fro8888

Registered
Jan 26, 2015
1
0
1
Medford, Oregon, United States
cPanel Access Level
Reseller Owner
You would just set a cron job to run this script to the frequency you require.
Hi All,

I am extremely new to all of this. How would I go about implementing this bash script?
I'm not familiar with doing anything in SSH or Setting up Cron Jobs.
I would really like to use this script because I can tell email quota levels are going to be breached a lot if I forget to manually delete them, which is a big pain. Also I just got into reseller hosting, which only allows me 60GB of space, and emails will definitely be a problem. I would greatly appreciate the help on this.

Thanks!