setting up cron job as root -not working

sardineit

New member
Joined
Jun 15, 2022
Messages
9
Hi DA users,

I am trying to setup a cronjob to delete all emails older than 1 day so the server doesnt get filled up. This is what i am trying (after login as root via ssh).

Code:
crontab -e 0 23 * * * /home/admin/delete-emails-older-than-1-day.sh

and i get this response.

Response:
Code:
crontab: usage error: no arguments permitted after this option
usage:  crontab [-u user] file
        crontab [ -u user ] [ -i ] { -e | -l | -r }
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)

what am i doing wrong? How can i run this as root?
 
Thank you.
so by adding the cron to that file i setup the job. how do i get it to email me after it finishes?
 
Put as first line this:
MAILTO=[email protected]

Ofcourse replace with your own e-mail address.

Is this for admin only? Because I see /home/admin and if yes, why don't you create the cronjob via DA then? It's easier to do and e-mails you too.
 
MAILTO=[email protected] 0 23 * * * /home/admin/delete-emails-older-than-1-day.sh

like this?
I tried setting up cron from DA but it failed saying 'permission denied' when it ran. So iassume since it is deleting files, it needs root privileges.
 
No.
You have to put that MAILTO string into the cronjob itself. So in the delete-emails-older-than-1-day.sh files.
Could you share the content of that file? We can see instantly if it's correct.

The permission denied can also be caused by other reasons.
 
Thank you Richard. Here are the details

cron file name: delete-emails-older-than-1-day.sh
cron file location: /home/admin/
command inside the file: find /home/*/imap/*/*/Maildir -mtime +1 -type f -regex ".*/\(new\|cur\)/.*" -delete
this finds all emails in all users that are 1 day old & deletes it. i have tried it in terminal (ssh loggin in as root) & it works.

now that you have all the info, what do i write & where please? (exact command would be great)
 
this finds all emails in all users that are 1 day old & deletes it.
With permission i hope:LOL:

I tried setting up cron from DA but it failed saying 'permission denied' when it ran. So iassume since it is deleting files, it needs root privileges
You indeed need root access for this because your are accessing the home dirs of other users.

Did you google already? For example: https://serverfault.com/questions/691033/how-to-send-an-e-mail-after-a-cron-job
 
yes, with permission. they all use pop3 & pull emails into their gmail/outlook.

yes I googled. but just like last time, they just give rough idea and i spend hours trying to figure out. for example to run a cron as root, most sites said just use crontab -e . i spent half a day trying to run commands like crontab -e 0 23 * * * /home/admin/delete-emails-older-than-1-day.sh

that is why i asked for exact command if you can.
 
Crontab is where you manually add jobs and DirectAdmin is a GUI on top of that. So any changes you make in DA are updated in the crontab, and every user has one.

Code:
usage:  crontab [-u user] file
        crontab [ -u user ] [ -i ] { -e | -l | -r }
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)

crontab -e to edit, crontab -l to display all active cron jobs.

Execute crontab -e to go to editing mode and insert (each job on a seperate line)

Code:
0 23 * * * /home/admin/delete-emails-older-than-1-day.sh

Save and exit.

crontab -l to verify if the job is now active. Let’s try that first and then the e-mail.
 
crontab -e
it likely file editor in tools "vi, vim" ..etc..

after you make change contents finish please " Esc " back to interact mode
when you want to save just use " :w " on the keyboard, and quit from editor " :q "
 
You indeed need root access for that one.
You could at it to root like @Erulezz showed, but you could also add the content to the /etc/crontab file.

So you can do it as mentioned above, or just edit /etc/crontab and then add this command:
0 23 * * * find /home/*/imap/*/*/Maildir -mtime +1 -type f -regex ".*/\(new\|cur\)/.*" -delete
root will get automatically an email of that, or you can change the e-mail address of root in the MAILTO above.

If this is only once a day you could also copy your delete-emails-older-than-1-day.sh file the /etc/cron.daily directory and if all is well it will be run automatically each night.
The file should look like this (in Centos/alma/rocky):
Code:
#!/bin/sh
[email protected]
find /home/*/imap/*/*/Maildir -mtime +1 -type f -regex ".*/\(new\|cur\)/.*" -delete

So you see you have various options to choose from.

However I'm just wondering. If all the people use pop3, then mails will normally not be left in folders on the server.
 
Crontab is where you manually add jobs and DirectAdmin is a GUI on top of that. So any changes you make in DA are updated in the crontab, and every user has one.

Code:
usage:  crontab [-u user] file
        crontab [ -u user ] [ -i ] { -e | -l | -r }
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)

crontab -e to edit, crontab -l to display all active cron jobs.

Execute crontab -e to go to editing mode and insert (each job on a seperate line)

Code:
0 23 * * * /home/admin/delete-emails-older-than-1-day.sh

Save and exit.

crontab -l to verify if the job is now active. Let’s try that first and then the e-mail.
It took my entire day (17/06/2022). A full of my life to fugure this bit out. you did it in 3min. So thank you
 
You indeed need root access for that one.
You could at it to root like @Erulezz showed, but you could also add the content to the /etc/crontab file.

So you can do it as mentioned above, or just edit /etc/crontab and then add this command:
0 23 * * * find /home/*/imap/*/*/Maildir -mtime +1 -type f -regex ".*/\(new\|cur\)/.*" -delete
root will get automatically an email of that, or you can change the e-mail address of root in the MAILTO above.

If this is only once a day you could also copy your delete-emails-older-than-1-day.sh file the /etc/cron.daily directory and if all is well it will be run automatically each night.
The file should look like this (in Centos/alma/rocky):
Code:
#!/bin/sh
[email protected]
find /home/*/imap/*/*/Maildir -mtime +1 -type f -regex ".*/\(new\|cur\)/.*" -delete

So you see you have various options to choose from.

However I'm just wondering. If all the people use pop3, then mails will normally not be left in folders on the server.
Thank you Richard.
 
Back
Top