Hướng dẫn tạo CronJob xóa toàn bộ mail chờ DirectAdmin - How to clear queue mail with CronJob DirectAdmin

Admin

AdminAdmin is verified member.

Well-Known Member
Staff member
Administrator
Hôm nay mình xin hướng dẫn các bạn cách xóa mail chờ trên DirectAdmin một cách tự động bằng CronJob

Đầu tiên bạn chạy lệnh sau
Code:
nano tuoitreit.sh
Sau đó dán code sau vào
Code:
use strict;

use Getopt::Std;


my $exim = '/usr/sbin/exim';
my $eargs = '-bpu';
my %id;
my %opt;
my $count = 0;
my $mcount = 0;
my @tab62 =
  (0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,     # 0-9
   0,10,11,12,13,14,15,16,17,18,19,20,  # A-K
  21,22,23,24,25,26,27,28,29,30,31,32,  # L-W
  33,34,35, 0, 0, 0, 0, 0,              # X-Z
   0,36,37,38,39,40,41,42,43,44,45,46,  # a-k
  47,48,49,50,51,52,53,54,55,56,57,58,  # l-w
  59,60,61);                            # x-z


my $base;
if ($^O eq 'darwin') { # aka MacOS X
  $base = 36;
 } else {
  $base = 62;
};


getopts('hf:r:y:o:s:zxlibRc',\%opt);
if ($opt{h}) { &help; exit;}


&collect();


&selection();


&display();
exit;




sub help() {
        print <<'EOF'
Exim message queue display utility.


        -h              This help message.


Selection criteria:
        -f <regexp>     Match sender address sender (field is "< >" wrapped)
        -r <regexp>     Match recipient address
        -s <regexp>     Match against the size field from long output
        -y <seconds>    Message younger than
        -o <seconds>    Message older than
        -z              Frozen messages only (exclude non-frozen)
        -x              Non-frozen messages only (exclude frozen)


[ NB: for regexps, provided string sits in /<string>/ ]


Display options:
        -c              Display match count
        -l              Long Format [Default]
        -i              Message IDs only
        -b              Brief Format
        -R              Reverse order
EOF
}


sub collect() {
        open(QUEUE,"$exim $eargs |") or die("Error openning pipe: $!\n");
        while(<QUEUE>) {
                chomp();
                my $line = $_;
                if ($line =~ /^\s*(\w+)\s+(\S+)\s+(\w{6}-\w{6}-\w{2})\s+(<.*?>)/) {
                        my $msg = $3;
                        $id{$msg}{age} = $1;
                        $id{$msg}{size} = $2;
                        $id{$msg}{from} = $4;
                        $id{$msg}{birth} = &msg_utc($msg);
                        $id{$msg}{ages} = time - $id{$msg}{birth};
                        if ($line =~ /\*\*\* frozen \*\*\*$/) {
                                $id{$msg}{frozen} = 1;
                        } else {
                                $id{$msg}{frozen} = 0;
                        }
                        while(<QUEUE> =~ /\s+(.*?\@.*)$/) {
                                push(@{$id{$msg}{rcpt}},$1);
                        }
                        
                        $count++;
                } else {
                        print STDERR "Line mismatch: $line\n"; exit 1;
                }
        }
        close(QUEUE) or die("Error closing pipe: $!\n");
}


sub selection() {
        foreach my $msg (keys(%id)) {
                if ($opt{f}) {
                        
                        next unless ($id{$msg}{from} =~ /$opt{f}/);
                }
                if ($opt{r}) {
                        
                        my $match = 0;
						            foreach my $rcpt (@{$id{$msg}{rcpt}}) {
                                $match++ if ($rcpt =~ /$opt{r}/);
                        }
                        next unless ($match);
                }
                if ($opt{s}) {
                        
                        next unless ($id{$msg}{size} =~ /$opt{s}/);
                }
                if ($opt{y}) {
                        
                        next unless ($id{$msg}{ages} < $opt{y});
                }
                if ($opt{o}) {
                        
                        next unless ($id{$msg}{ages} > $opt{o});
                }
                if ($opt{z}) {
                        
                        next unless ($id{$msg}{frozen});
                }
                if ($opt{x}) {
                        
                        next if ($id{$msg}{frozen});
                }
                
                $id{$msg}{d} = 1;
               
                $mcount++;
        }
}


sub display() {
        if ($opt{c}) {
                printf("%d matches out of %d messages\n",$mcount,$count);
                exit;
        }
        foreach my $msg (sort { $opt{R} ? $id{$b}{birth} <=> $id{$a}{birth} : $id{$a}{birth} <=> $id{$b
}{birth} } keys(%id) ) {
                if (exists($id{$msg}{d})) {
                        if ($opt{i}) {
                                
                                print $msg, "\n";
                        } elsif ($opt{b}) {
                                
                                printf("%s From: %s To: %s\n",$msg,$id{$msg}{from},join(';',@{$id{$msg}
{rcpt}}))
                        } else {
                                
                                printf("%3s %5s %s %s%s\n",$id{$msg}{age},$id{$msg}{size},$msg,$id{$msg
}{from},$id{$msg}{frozen} ? " *** frozen ***" : "");
                                foreach my $rcpt (@{$id{$msg}{rcpt}}) {
                                        printf("          %s\n",$rcpt);
                                }
                                print "\n";
                        }
                }
        }
}


sub report() {
sub report() {
        foreach my $msg (keys(%id)) {
                print "$id{$msg}{birth} $msg\tAge: $id{$msg}{age}\tSize: $id{$msg}{size}\tFrom: $id{$ms
g}{from}\tTo: " . join(" ",@{$id{$msg}{rcpt}}). "\n";
        }
}


sub msg_utc() {
        my $id = substr((pop @_), 0, 6);
        my $s = 0;
        my @c = split(//, $id);
        while($#c >= 0) { $s = $s * $base + $tab62[ord(shift @c) - ord('0')] }
        return $s;
}



Sau đó bạn tạo 1 CronTab đến tập tin tuoitreit.sh bằng lệnh crontab -e

Mọi thắc mắc vui lòng phản hồi bên dưới
Chúc các bạn thành công
Nguồn tuoitreit.vn
 

Facebook Comments

Similar threads

Admin
Replies
2
Views
3K
thahtrung06
T
Admin
Replies
0
Views
10K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
10K
AdminAdmin is verified member.
Admin
Back
Top