title: PHP Cron Job with exec() function
body: <p>I am trying to program a Cron Job that fetches some data from multiple websites. The script uses <code>phantomjs</code> that runs a js file responsible for saving each website's content to a file locally.</p>
  
  <p>My cron job command:</p>
  
  <pre><code>/usr/local/bin/php /home/user/public_html/fetch.php &gt;&gt; /home/user/public_html/fetch-log.txt
  </code></pre>
  
  <p><strong>fetch.php</strong></p>
  
  <pre><code>ini_set('max_execution_time', 300);
  require_once 'Utils.php';
  
  $sources = Utils::get_sources();
  
  foreach ($sources as $source) {
      $cmd = "/home/user/bin/phantomjs get-website.js '$source-&gt;url' $source-&gt;id";
      exec($cmd);
  }
  
  echo date("Y-m-d H:i:s") . "&gt;&gt; Done fetching sources\n";
  </code></pre>
  
  <p><strong>get-website.js</strong></p>
  
  <pre><code>var system = require('system');
  var webPage = require('webpage');
  var fs = require('fs');
  
  var page = webPage.create();
  var url = system.args[1];
  var file = system.args[2];
  
  var requestsArray = [];
  
  page.onResourceRequested = function (requestData, networkRequest) {
      requestsArray.push(requestData.id);
  };
  
  page.onResourceReceived = function (response) {
      var index = requestsArray.indexOf(response.id);
      requestsArray.splice(index, 1);
  };
  
  page.open(url, function (status) {
      var interval = setInterval(function () {
          if (requestsArray.length === 0) {
              clearInterval(interval);
              try {
                  fs.write("/home/user/public_html/rss_contents/" + file + ".txt", page.content, 'w');
              } catch (e) {
                  console.log(e);
              }
              phantom.exit();
          }
      }, 500);
  });
  </code></pre>
  
  <p>The script works normally when running from browser, and the cron job also works normally when run with a simple <code>echo("done");</code> php script, but when I try to run the cron job with the <code>fetch.php</code> script, the last line is echoed to the file, but nothing has happen.</p>
  
  <p>I am using shared hosting.</p>
  
  <p>EDIT:
  When trying to run <code>phantomjs</code> from ssh console directly, I get this error</p>
  
  <blockquote>
    <p>2019-06-27T11:34:12 [WARNING] QThread::start: Thread creation error:
    Resource temporarily unavailable</p>
  </blockquote>
  
  <p>And the console just hangs without any response.
  I couldn't find any posts talking about this warning on Google.</p>