Getting javascript to run a process if it's not already running
Asked Answered
A

1

4

I want to have Javascript run say cmd.exe if there is already not one running.

I was hoping there is a way to have javascript look at running processes and then if the name is in the list dont run. but if it's not run the process.

Appetitive answered 26/7, 2011 at 16:31 Comment(8)
You'd probably want to look into JScript.net.Blinking
Or look into Rhino, ringoJS, nodejs or any other server side js platform. Although for windows, JScript.NET would be bestMarko
As well as the WSH object model: msdn.microsoft.com/en-us/library/a74hyyw0(v=vs.85).aspxDifferent
Every hacker in the world would love to see this work as well. JavaScript knows nothing of the shell. You'd have to use ActiveX or some other horrible thing to get OS level access - and the user would have to explicitely allow it. Also, it would obviously not be cross platform.Superorganic
@Superorganic what makes you think he wants to do this from a browser? That would be stupidMarko
@Superorganic - you're assuming he means JS from the browser. You might be right, but I assumed he meant something like WSH...Different
I assume when someone wants to do something in WSH, they write in VBS. My bad!Superorganic
I don't know why people feel like it is acceptable to moralize at another developer - if you don't know about something or don't like it, leave it alone. If you're going to assume anything at all, assume that the OP needs to know the answer to the question they asked. We don't have nearly enough background to decide if the application of the information they seek is sensible or not, and frankly I fail to see how it is our purview to play moderator or gatekeeper of IT knowledge based on merit. Answer the question or don't.Goatsbeard
G
8

Javascript is not the best route for scripting OS-level process control. If javascript were able to have such direct access to your operating system, it would be an extreme security risk to ever browse the internet.

Internet Explorer does have a mechanism to script Windows from javascript, but you would have to adjust your security settings to allow this to occur. Other browsers do not even offer the possibility.

This code will execute notepad.exe in Internet Explorer, after choosing to "Allow blocked content" from the security warning:

var shell = new ActiveXObject('WScript.Shell');
shell .Run("notepad.exe");

docs: http://msdn.microsoft.com/en-us/library/aew9yb99%28v=vs.85%29.aspx

So, we can use this method to both list active processes and to start one if it is appropriate:

function startUniqueProcess(process_name, process_path) {
    // create a shell object and exec handle
    var shell = new ActiveXObject('WScript.Shell');
    var handle = shell.Exec("tasklist.exe");

    // loop through the output of tasklist.exe
    while (!handle.StdOut.AtEndOfStream) {
        // grab a line of text
        var p = handle.StdOut.ReadLine();
        // split on space
        p = p.split(' ');
        // check for split lines longer than 2
        if (p.length < 2)
            continue;
        // match process_name to this item
        if (p[0] == process_name) {
            // clean up and return false, process already running
            shell = null;
            handle = null;
            return false;
        } // end :: if matching process name
    } // end :: while

    // clean up
    handle = null;

    // process not found, start it
    return shell.Exec(process_path);
}


// example use
var result = startUniqueProcess('notepad.exe', 'notepad.exe');
if (result === false)
    alert('did not start, already open');
else
    alert('should be open');

Keep in mind, this is proof of a concept - in practice I would not suggest you ever, ever, ever do this. It is browser-specific, dangerous, exploitable, and generally bad practice. Web languages are for web applications, javascript is not intended to be a OS scripting language despite what Microsoft might want to tell you. :)

Goatsbeard answered 26/7, 2011 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.