GETPPID |
Demande l'identificateur de processus parent |
---|---|
Perl |
Syntaxe
getppid |
Retour
Valeur | Description |
---|---|
variable | Ces valeurs permet d'indiquer la valeur de l'identificateur parent. |
Description
Cette fonction permet de connaître le numéro de processus d'un processus parent.
Exemple
Voici un exemple permet d'envoyer des listes de courriels massif (contenu dans une base de données MySQL) et de retenir sa position à partir sa position à l'aide du PID et de s'assurer ainsi qu'il n'envoi pas deux fois le même envois :
- #!/usr/bin/perl
-
- # Ce script est destiné envoyer des courriels massif en risquant d'être interrompu
-
- use strict;
- use warnings;
-
- use CGI;
- use DBI;
- use HTML::Entities;
- use MIME::QuotedPrint;
- use MIME::Base64;
- use MIME::Words qw(:all);
- use Mail::Sendmail 0.75;
-
- use constant Verbose => 1;
-
- my $ServerMail = 'localhost';
-
- # Remplace les balises avec les valeurs associées
- sub replaceHashHtml($%) {
- my ($html, %args) = @_;
- foreach my $k (keys(%args)){
- if(defined($args{$k})) {
- $html =~ s/$k/$args{$k}/g;
- } else {
- $html =~ s,Q$kE,,g;
- };
- };
- return $html;
- }
-
- sub envoicourriel($$$$;$$){
- my ($to, $from, $sujet, $contenu, $attachement, $file) = @_;
- my $boundary = 'END-OF-TRANSMISSION-AT-' . time() . '-GOODNIGHT';
- my %msgErr = ();
- my $filestr;
- my @attachement;
- my @file;
- my @to = @$to;
- my %mail;
- if($attachement) {
- @attachement = @$attachement;
- } else {
- @attachement = undef;
- }
- if($file) {
- @file = @$file;
- } else {
- @file = undef;
- }
-
- my $subjectEncodee = MIME::Words::encode_mimeword($sujet);
- $subjectEncodee =~ s/s/_/g; # Compatibilité SquireMail
-
- %mail = (
- 'SMTP' => $ServerMail,
- 'from' => $from,
- 'to' => '',
- 'subject' => $subjectEncodee,
- 'content-type' => qq|multipart/alternative; boundary="$boundary"|,
- 'body' => ''
- );
-
- $contenu = encode_base64(encode_entities($contenu, "200-377"));
- $mail{'body'} = qq|nn--$boundarynContent-Transfer-Encoding: base64nContent-Type: text/html; charset="iso-8859-1"nnnn$contenun|;
-
- # Ajout de fichier joint sur le serveur
- if( @attachement and scalar(@attachement) > 0 ) {
- $mail{'content-type'} = qq|multipart/mixed; boundary="$boundary"|;
-
- while( $attachement = pop @attachement ) {
- $filestr = '';
- if( open (FILE, $attachement) ) {
- binmode FILE;
- while(<FILE>){ $filestr .= $_; };
- close FILE;
-
- $mail{'body'} .= &joinFile($attachement,$filestr,$boundary);
- };
- };
- };
-
- # Ajout des fichier joint par un input type=file de formulaire (fichier client)
- if( @file and scalar(@file) > 0 ) {
- $mail{'content-type'} = qq|multipart/mixed; boundary="$boundary"|;
-
- while( $attachement = pop @file ) {
- $filestr = '';
- while(<$attachement>) { $filestr .= $_; };
-
- $mail{'body'} .= &joinFile($attachement,$filestr,$boundary);
- };
- };
-
- $mail{body} .= qq|--$boundary--n|;
-
- foreach $_ (@to) {
- $mail{'to'} = $_;
- $msgErr{$_} = sendmail(%mail) || "Erreur: $Mail::Sendmail::errorn";
- };
-
- return(%msgErr, $to, $from, $contenu)
- };
-
- my ($Conn,$CurrLine,$sth,$Result);
- $Conn = DBI->connect("DBI:mysql:database=gladir_db;host=localhost","root","",{'RaiseError' => 1});
-
- my $CurrLineFileLog;
- my $CurrPID = getppid();
-
- $sth = $Conn->prepare("UPDATE cdmsendmail SET pid = '" . $CurrPID . "' WHERE NOT deleted AND (CURDATE() >= dateToSend) AND (send = '' OR send IS NULL)");
- $Result = $sth->execute;
-
- # Tentative pour demander la position actuellement rendu dans les résultats
- $sth = $Conn->prepare("SELECT * FROM `cdmsendmail` WHERE NOT deleted AND pid = " . $CurrPID . " AND (dateToSend >= CURDATE()) AND (send = '' OR send IS NULL) LIMIT 0,1");
- $Result = $sth->execute;
- if($CurrLine = $sth->fetchrow_hashref) {
- $CurrLineFileLog = $CurrLine->{"filelog"};
- } else {
- $CurrLineFileLog = "";
- }
-
- $sth = $Conn->prepare("SELECT COUNT(*) As Compteur FROM `cdmsendmail` WHERE send = 'SUCCES' AND filelog = '".$CurrLineFileLog."'");
- $Result = $sth->execute;
- if($CurrLine = $sth->fetchrow_hashref) {
- my $nbrCourrielValide = $CurrLine->{"Compteur"};
- }
-
- $sth = $Conn->prepare("SELECT COUNT(*) As Compteur FROM `cdmsendmail` WHERE send = 'ERROR' AND filelog = '".$CurrLineFileLog."'");
- $Result = $sth->execute;
- if($CurrLine = $sth->fetchrow_hashref) {
- my $nbrCourrielInvalide = $CurrLine->{"Compteur"};
- }
-
- my ($msgErreur,$email);
- my $Found = 1;
- my $nbrCourrielValide = 0;
- my $nbrCourrielInvalide = 0;
- while($Found == 1) {
- $sth = $Conn->prepare("SELECT ".
- "id_sendmail, ".
- "fromEmail, ".
- "toEmail, ".
- "subject, ".
- "contenu, ".
- "files, ".
- "filelog, ".
- "send, ".
- "dateToSend, ".
- "firstname, ".
- "lastname, ".
- "personnel ".
- "FROM cdmsendmail WHERE NOT deleted AND pid = '".$CurrPID."' AND (send = '' OR send IS NULL) LIMIT 0,1");
- $Result = $sth->execute;
- if($CurrLine = $sth->fetchrow_hashref) {
- my $id_sendmail = $CurrLine->{'id_sendmail'};
- if(Verbose) {
- print $CurrLine->{fromEmail} . " -> " . $CurrLine->{toEmail} . "n";
- }
-
- my $contenu = $CurrLine->{contenu};
- if($CurrLine->{personnel}) {
- my %hashReplace = (
- "PRENOM_DESTINATAIRE" => $CurrLine->{firstname},
- "NOM_DESTINATAIRE" => $CurrLine->{lastname},
- "<var: ID_DESTINATAIRE />" => $CurrLine->{id_sendmail}
-
- );
- $contenu = replaceHashHtml($contenu, %hashReplace);
- }
- ($msgErreur, $_, $_, $_) = envoicourriel([$CurrLine->{'toEmail'}], $CurrLine->{'fromEmail'}, $CurrLine->{'subject'}, $contenu,$CurrLine->{'files'},undef);
- my %msgErreur = %$msgErreur;
- foreach $email (keys %msgErreur) {
- if($msgErreur{$email} eq '1') {
- $nbrCourrielValide++;
- $sth = $Conn->prepare("UPDATE `cdmsendmail` SET send='SUCCES',`dateSended`=NOW() WHERE id_sendmail = '" . $id_sendmail . "'");
- $Result = $sth->execute;
- } else {
- $nbrCourrielInvalide++;
- $sth = $Conn->prepare("UPDATE `cdmsendmail` SET send='ERROR',`dateSended`=NOW() WHERE id_sendmail = '" . $id_sendmail . "'");
- $Result = $sth->execute;
- };
- };
- $Found = 1;
- } else {
- $Found = 0;
- }
- }
-
- if(Verbose) {
- print "Envoi réussis: " . $nbrCourrielValide . "\n";
- print "Envoi échoué: " . $nbrCourrielInvalide . "\n";
- print "Terminer !";
- }
Voir également
Langage de programmation - C pour Linux - Référence de procédures et fonctions - getppid
Dernière mise à jour : Dimanche, le 18 octobre 2015