Hi All,
I have compiled a small script I would like to run as a service. The script is using inotifywait to monitor a directory. inotifywait has an option to run as daemon ( switch -d), but all output has to be redirected to a file (switch -o) so my code never executes. Any advise would be appreciated.
Here is the code:
Code:<?php require_once('/usr/share/php/PHPMailer/class.phpmailer.php'); include("/usr/share/php/PHPMailer/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded include("/opt/CouchPotato/scripts/resize-class.php"); include("/opt/CouchPotato/scripts/imdb.php"); date_default_timezone_set('America/St_Johns'); //watch directory /storage/movies $watchedDir = '/storage/movies'; $in = popen("inotifywait --monitor --quiet --format '%e %f' --event create,moved_to '$watchedDir'", 'r'); if ($in === false) throw new Exception ('fail start notify'); while (($line = fgets($in)) !== false) { list($event, $movie) = explode(' ', rtrim($line, PHP_EOL), 2); $isdir ='ISDIR'; if (strlen(strstr($event,$isdir))>0) { // New movie directory found //imdb scraping $imdb = new Imdb(); $movieArray = $imdb->getMovieInfo($movie); $poster_url = $movieArray['poster']; $img = 'poster.jpg'; file_put_contents($img, file_get_contents($poster_url)); $title = $movieArray['title']; $plot = $movieArray['plot']; $year = $movieArray['year']; //resize image for email $resizeObj = new resize('poster.jpg'); //Resize image (options: exact, portrait, landscape, auto, crop) $resizeObj -> resizeImage(300, 400, 'auto'); //Save image $resizeObj -> saveImage('poster-resized.jpg', 100); //email $mail = new PHPMailer(true); // the true param means it will throw exceptions o$ $mail->IsSMTP(); // telling the class to use SMTP $mail->IsHTML(true); try { $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the server $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->Username = "xxx@gmail.com"; // SMTP account username $mail->Password = "xxxxxxx"; // SMTP account password $mail->AddAddress('xxxxx@xxx.com', 'xxxxxx xxxx, Esq.'); $mail->SetFrom('MovieServer@localhost.com', 'Movie Server'); //$mail->AddReplyTo('MovieServer', 'Movie Server'); $mail->Subject = $title.' ('.$year.')'; $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically $mail->AddEmbeddedImage('poster-resized.jpg', $movie, 'poster-resized.jpg'); $mail->Body ='<DIV ALIGN=CENTER><FONT FACE="Impact" SIZE="+2">'.$title.'</font></div><table align="center" border="1"><tr><td><img alt="PHPMailer" align="middle" src="cid:$movie"></td></tr></table><br><blockquote>'.$plot.'</blockquote>'; $mail->Send(); echo "Message Sent OK"; } catch (phpmailerException $e) { echo $e->errorMessage(); //Pretty error messages from PHPMailer } catch (Exception $e) { echo $e->getMessage(); //Boring error messages from anything else! }//try //clean up unlink('poster.jpg'); unlink('poster-resized.jpg'); }//end if }//end while ?>


Reply With Quote
Bookmarks