Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Last active August 29, 2015 14:06
Show Gist options
  • Save Noitidart/16cd799f6bbfbcdea12f to your computer and use it in GitHub Desktop.
Save Noitidart/16cd799f6bbfbcdea12f to your computer and use it in GitHub Desktop.
_ff-addon-snippet-X11_bash - Trying to run bash with ps on X11 system to get list of running process IDs and names.
var thisWin = Services.ww.activeWindow;
var env = Cc['@mozilla.org/process/environment;1'].getService(Ci.nsIEnvironment);
var pathFor = {
'ps': 0,
'bash': 0
};
var fileFor = {}; //populatdd based on pathsFor
var paths = env.get('PATH').split(':');
var len = paths.length;
for (var p in pathsFor) {
for (var i = 0; i < len; i++) {
try {
var fullyQualified = new FileUtils.File(OS.Path.join(paths[i], p));
console.log('search for:', p, 'fullyQualified:', fullyQualified.path)
if (fullyQualified.exists()) {
fileFor[p] = fullyQualified;
pathFor[p] = fullyQualified.path;
break;
}
} catch (e) {
// keep checking PATH if we run into NS_ERROR_FILE_UNRECOGNIZED_PATH
}
}
}
if (!fileFor.bash || !fileFor.ps) {
console.error("Error: a task list executable not found on filesystem");
thisWin.alert('Error: a task list executable not found on filesystem');
throw new Error("Error: a task list executable not found on filesystem");
}
var procFinned = {
observe: function (aSubject, aTopic, aData) {
thisWin.alert('ps completed');
}
};
var process = Cc['@mozilla.org/process/util;1'].createInstance(Ci.nsIProcess);
var pathOutput = OS.Path.join(OS.Constants.Path.desktopDir, 'newt.txt');
var args = ['-c', pathFor.ps.replace(/\W/g, '\\$&') + ' aux > ' + pathOutput.replace(/\W/g, '\\$&')];
process.init(fileFor.bash);
process.runAsync(args, args.length, procFinned);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment