title: How make a node.js script to be restarted every hour?
body: <p>I have a node.js script that creates a websocket connection to a crypto trading site (bitmex). It streams price data.</p>
  
  <p>For some reason after an hour or two the stream goes bad and the prices (if streamed at all) are inacurate.</p>
  
  <p>For now I restart it manually every hour but I need that to be done automatically. How can I do that?</p>
  
  <p>Here is the code of the script:</p>
  
  <pre><code>var WebSocket = require('ws');
  var ws = new WebSocket("wss://www.bitmex.com/realtime");
  
  var couchbase = require('couchbase')
  var cluster = new couchbase.Cluster('couchbase://localhost/');
  cluster.authenticate('xxxxxx', 'xxxxxxx');
  var bucket = cluster.openBucket('test');
  var N1qlQuery = couchbase.N1qlQuery;
  
  let num_msg = 0;        
  ws.onopen = function(){
      ws.send(JSON.stringify({"op": "subscribe", "args": [    
      "trade:XBTUSD",
      "trade:LTCZ18"]
      }))
  };
  ws.onmessage = function(msg){
      var response = JSON.parse(msg.data);
      num_msg++
      if(num_msg &gt; 50) {
          var coin = response['data'][0]['symbol'];
          var price = response['data'][0]['price'];
          //console.log(coin + ":" + price + "\n");
          bucket.manager().createPrimaryIndex(
              function(){
                  bucket.upsert(  coin, 
                                  {
                                  'price': price
                                  },
                                  function (err, result){
              });
          });
  
      }
  
  };
  </code></pre>
  
  <p>EDIT: I missed to mention that currently I use Windows 7 system (eventhough I do need to move to Ubuntu or similar).</p>
  
  <p>EDIT 2: Final version of my code :)</p>
  
  <pre><code>const cron = require("node-cron");
  var WebSocket = require('ws');
  var couchbase = require('couchbase');
  var dateTime = require('node-datetime');
  
  let now = new Date();
  minutes = now.getMinutes() + 1;
  if(minutes + 30 &gt; 59) {
      minutes1 = minutes - 30;
  } else {
      minutes1 = minutes - 30;
  }
  if(minutes &gt; minutes1) {
      s_cron = minutes1 + "," + minutes +  " * * * *";
  } else {
      s_cron = minutes + "," + minutes1 +  " * * * *";
  }
  cron.schedule(s_cron, () =&gt; {
      console.log("---------------------");
      console.log("Running Cron Job");
      var dt = dateTime.create();
      var formatted = dt.format('Y-m-d H:M:S');
      console.log(formatted);    
      // create bitmex ws
          var ws = new WebSocket("wss://www.bitmex.com/realtime");
          // connect to couchbase
          var cluster = new couchbase.Cluster('couchbase://localhost/');
          cluster.authenticate('xxxxxxxx', 'xxxxxxxxxx');
          var bucket = cluster.openBucket('test');
          var N1qlQuery = couchbase.N1qlQuery;
  
          let num_msg = 0;        
          ws.onopen = function(){
              ws.send(JSON.stringify({"op": "subscribe", "args": [    
                  "trade:XBTUSD",
                  "trade:LTCZ18"]
              }))
          };
          ws.onmessage = function(msg){
              var response = JSON.parse(msg.data);
              num_msg++
              if(num_msg &gt; 50) {
                  var coin = response['data'][0]['symbol'];
                  var price = response['data'][0]['price'];
                  //console.log(coin + ":" + price + "\n");
                  bucket.manager().createPrimaryIndex(
                      function(){
                          bucket.upsert(  coin, 
                                          {
                                          'price': price
                                          },
                                          function (err, result){
                                              //bucket.disconnect()
                      });
                  });
  
              }
  
          };
    });
  </code></pre>