'Processing a LOG file on an automounted CIFS host, which is frequently turned OFF

So I am new to Ubuntu and I have some log files on a windows machine, which I should process frequently. On my ubuntu server there is a service which watches for file changes and processes the logs.

The windows machines's C drive is mounted on the Ubuntu server, and today I succesfully configured the /etc/fstab file, so the drive is automatically mounted on reboot.

The problem is that the windows machine is turned off at the end of every shift.

Theres a Node.js program on my server, which constantly tries to access the file, and when it's available, watches the log file and processes the constantly inserted new lines. If the machine is turned off, it tries to reach the file over again.

The question is:

What happens if the ubuntu server reboots(because of any known or unknown cause), and cant find the windows machine to mount its C drive? While I was configuring the ftsab and messed up the config, the whole system became read-only so I guess not findig the machine to connect could be a problem. What could be the solution?

Is my approach to the problem is correct? If not, what could be a better solution? I would rather just read the data form the windows machine, I wouldnt like to install any program on it which sends data, or anything like that.



Solution 1:[1]

Current, working solution:

Remove the mounting from fstab and manage it from the Node.js app, with executing shell commands, with execSync:

async function mountDrive() {
  if (process.platform === "win32") {
    //on windows the drive shouldnt be explicitly mounted
    let folderExists = false;
    while (!folderExists) {
      logger.info(
        "Trying to reach destination folder: " + config.sourceFolderPath
      );
      if (fs.existsSync(config.sourceFolderPath)) {
        //if folder found exit the loop
        folderExists = true;
        logger.info("Target folder found!");
      } else {
        //if folder not found try again in config.timeout seconds
        logger.warn(
          "Target folder not found, trying again in " +
            config.timeout +
            " seconds..."
        );
        await sleep(config.timeout * 1000);
        continue;
      }
    }
    return;
  }

  if (process.platform === "linux") {
    let driveMounted = false;
    while (!driveMounted) {
      logger.info(
        "Check if target folder is mounted: " + config.sourceFolderPath
      );
      //the mounted path
      let mountedPath = "";
      //get the mounted path if any
      try {
        let mountGrepResponse = execSync(
          `mount | grep ${config.sourceFolderPath}`
        ).toString("utf-8");

        mountedPath = mountGrepResponse.split("type")[0].split("on")[1].trim();

        logger.info(`Target folder is mounted at: ${mountedPath}`);
      } catch (error) {
        logger.error("Drive isn't mounted, trying to mount it...");
      }

      //if drive is not mounted try to mount it
      if (mountedPath.length === 0) {
        try {
          execSync(
            `sudo mount -t cifs -o username=guest,password= ${config.sourceFolderPath} ${config.mountFolderPath}`
          );
        } catch (error) {
          console.log(
            "Host unavailable, trying again in" + config.timeout + " seconds..."
          );
          await sleep(config.timeout * 1000);
          continue;
        }
      }
      
      driveMounted=true;
    }

    return;
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1