#!/usr/bin/node // // // // // data_collector_status source // // // // // // // File: data_collector_status // // Author: Francesco.Prelz@mi.infn.it // // Description: Sample CGI program built with server-side Javascript // (node.js) that checks on the status of a background network listener // // History: // 10-Oct-2014 Initial creation. var fs = require('fs'); var path = require('path'); var childp = require('child_process'); var querystring = require('querystring'); var datac = "data_collector_cgi"; var pidfile = "/var/tmp/" + datac + ".pid"; var xmldata = "/var/tmp/" + datac + "_data.xml"; var the_pid; var pid_status = "stopped"; try { the_pid = fs.readFileSync(pidfile, "utf8"); the_pid = the_pid.replace(/[^0-9]*$/, ''); } catch (err) { the_pid = ""; } var attrs = querystring.parse(process.env.QUERY_STRING); if (parseInt(the_pid, 10) > 0) { var pid_in_proc = "/proc/"+the_pid; if (fs.existsSync(pid_in_proc)) { if (attrs.action == "Stop") { process.kill(the_pid); } else { pid_status = "running"; } } } if (pid_status == "stopped" && attrs.action == "Start") { var child = childp.spawn("/home/guest/public_html/esercizi/wireless/jsFiles/data_collector_process", [ pidfile, xmldata ], { detached: true, stdio: [ 'ignore', 'ignore', 'ignore' ] }); the_pid = child.pid; child.unref(); pid_status = "running"; } process.stdout.write("Content-type: text/html\n\n"); process.stdout.write("STATUS: "+ pid_status + " childPID: " + the_pid + "\n");