HaltSignalReceived // global boolean variable

You can check HaltSignalReceived anywhere in your script. If it's set to true it means that an administrator (or the Operating System itself) has requested the execution of the current script to be stopped.

Once a halt request is received, the execution of the script will be stopped regardless of whether or not you check this value, but checking it gives you the chance to handle the situation gracefully.


 ConsoleFeedback = true;
 var scli = new SftpClient();
 scli.Host = 'your.sftpserver.com:22';
 scli.User = 'some_username';
 scli.PassFromSecret = 'name_of_the_secret_password';
 if (scli.Connect()) {
  watchr = NewFsWatcher();
  watchr.WatchDir('C:\\Docs', true);
  while (true) {
   if (HaltSignalReceived) {
    break; // break infinite loop if halt signal was received
   evt = watchr.Events() // gets the list of pending events to be handled
   if (evt.length > 0) {
    for (var i = 0; i < evt.length; i++) {
     if (evt[i].Event == 'WRITE') {
      scli.UploadWithPath(evt[i].Object, '/realtimebackup', 0);
 scli = nil;