Results 1 to 6 of 6

Thread: service script and iNotifywait

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    20

    service script and iNotifywait

    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
    
    ?>

  2. #2
    Senior Member divreg's Avatar
    Join Date
    May 2011
    Location
    Washington
    Posts
    407
    This might be more readily answered on stackoverflow. Only a handful of users here have experience with PHP.

    This looks cool though. Might have to check this out. What's it supposed to do?

  3. #3
    Administrator andrew's Avatar
    Join Date
    Nov 2008
    Location
    New Hampshire
    Posts
    3,614
    Yes can you tell us more about what your try to do?

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    20
    Hi,

    My script notifies me by email when a new movie has been downloaded; sending the movie poster and other details (plot, year, etc).

    inotifywait watches my movie path for new directories added and passes the directory as the movie name.

    The movie details are then scrapped from IMDB (poster, plot, year), processed, and emailed via Gmail's smtp server to my blackberry address.

    CouchPotato and Transmission queue and download my movies.

    inotifywait has a daemon option, but it requires output to be directed to a file and so no code gets executed.

    I'm currently running this php script in the background, with inotifywait using the switch --monitor, which is very similar to daemon mode. So In fact I don't really need inotifywait's daemon option.

    I would like to create a service script to control this php process (start, stop, status) and have it start on boot. I've checked the wiki, but I'm in over my head. I couldn't follow other examples either. I'm looking for direction.

    Thanks for the interest.

  5. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    20
    I just discovered incron. This may be the solution.

  6. #6
    Junior Member
    Join Date
    Mar 2009
    Posts
    20
    I've abandoned inotifywait in favor of incron.
    For reference, easily watch directories for changes with incron.
    Incron has it's own daemon, permits easily passing parameters and executing scripts.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •