1

I posted a thread about how to do this in batch but it turns out batch scripting isn't very popular and I barely even know it so now I'm asking for your help doing this in C++.

here's what I tried

#include <Windows.h>
using namespace std;

void openBat(char* path) {
    system(path);
}

int main() {
    for(;;) {
    openBat("C:\\Users\\Ivan\\Desktop\\folder\\run.bat");
    Sleep(1800000);
    //kill opened process
    }
    return 0;
}

I'm not sure how to kill the opened process because every time I run the bat script it will have a new ID and I can't kill by name because I need to have 4 of these open. All help is appreciated.

ed sheeran
  • 25
  • 5
  • `I posted a thread about how to do this in batch` Could you give a link to it? Also, "thread"s are usually called "question"s on Stack Overflow. – The SE I loved is dead Jul 23 '16 at 18:06
  • sorry, I deleted it, there was only like 2 replies and I couldn't understand them – ed sheeran Jul 23 '16 at 18:08
  • 2
    Ehm, that is not really reason to delete it – Přemysl Šťastný Jul 23 '16 at 18:11
  • Well if it means anything I undeleted it. http://stackoverflow.com/questions/38543991/open-file-sleep-then-close-infinite-loop-in-batch – ed sheeran Jul 23 '16 at 18:14
  • Thanks, Ed. If it really is useless, the administrators will remove it. – user4581301 Jul 23 '16 at 18:17
  • You shouldn't delete posts that may have value, just because _you_ "couldn't understand them"!! – Lightness Races in Orbit Jul 23 '16 at 18:17
  • OP: if the language doesn't matter (you'd be satisfied with a pure batch solution or a C++ solution) but the platform (Windows) does, then don't ask different questions specifying different languages; just don't specify a language at all. – Kyle Strand Jul 23 '16 at 18:40
  • Also, it's not really clear what you're trying to achieve here or why you want this. – Kyle Strand Jul 23 '16 at 18:42
  • @KyleStrand I have a python script which is buggy, every few hours it stops due to an error, now I don't know python good enough to fix the script so I figured this was the second best thing – ed sheeran Jul 23 '16 at 18:50
  • @KyleStrand No. There is not a single exception - truly infinite loops are programming mistakes, even `main` threads have to have *some* sort of exit-condition ... thats basically what *(actual)* software developers learn during job training - the `main` thread loop could wait for a keyboard exit command or probably even a combined, internal exit-flag. Algorithms which have no explicitly defined exit-state / exit-path are incomplete and erroneous by design and : *"no"*, OS interrupt signals are not what is supposed to be "clearly defined" - these are **exceptional** by definition – specializt Jul 23 '16 at 21:30
  • @specializt I'm sorry, but that sounds a heck of a lot like opinion dressed up as condescending fact. You don't necessarily need your exit condition(s) defined in such a way that the program won't simply run indefinitely *under certain conditions* (e.g. you may want httpd to run as long as the server is alive on a web server, and you'll want the server to be up as long as possible). And when you do have exit conditions, they don't necessarily need to be in your loop construct (otherwise there'd be no reason for Rust's `loop` keyword). – Kyle Strand Jul 24 '16 at 14:58
  • Also OS signals aren't exceptional "by definition"; see e.g. `SIGWINCH`. – Kyle Strand Jul 24 '16 at 14:59
  • OP, I recommend finding someone who knows Python to fix your script for you, or learning more about Python to figure it out. If you can narrow down some reasons why it might be crashing, you might even be able to post a question here to help you solve it. But as it stands, this is a poor use of batch and a ***really really really*** poor use of C++. If you really do need your Python scripts to run indefinitely and you can't fix the error, I'd recommend putting a `try/except` block in your main loop. – Kyle Strand Jul 24 '16 at 16:42
  • Also, OP, what do you mean when you say your Python script "stops"? Clearly it's not terminating, otherwise you wouldn't need to kill it....? Do you mean it becomes unresponsive but fails to exit? – Kyle Strand Jul 25 '16 at 04:28

3 Answers3

0

I think the solution can be killing the self process tree without killing process itself.

Terminate a process tree (C for Windows)

Přemysl Šťastný
  • 1,676
  • 2
  • 18
  • 39
0

When you create a process, hold onto the process handle and use the handle to terminate the when you are done. With the handle you know exactly which of possibly thousands of instances of the same process you want dead.

Note: Terminating a process may have undesirable results. You are almost always better off writing the processes in such a way that you can message them and request that they terminate themselves politely. How you would do this with a batch file... Smurfed if I know. Someone else may have a waaaaay better answer to this problem, and I'm fine with that. One day I might need that better solution.

On Windows you likely want CreateProcess and TerminateProcess.

Running a batchfile with CreateProcess is covered here: Use CreateProcess to Run a Batch File

Terminating a process launched with Create process is covered here: how to terminate a process created by CreateProcess()?

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
0

What you're doing there isn't really C++. You're basically using windows to interprete the commands you pass it like batch would do. Here is what you want to do in C++, even if it only runs on Windows.

#include <Windows.h>
#include <string>

std::wstring GetEnvString()
{
    wchar_t* env = GetEnvironmentStrings();
    std::wstring result{ env };
    FreeEnvironmentStrings(env);
    result.push_back('\0');
    return result;
}

int main()
{
    //Setup needed structures
    STARTUPINFO si{ sizeof si };
    PROCESS_INFORMATION pi;

    //Command line (read- and writeable)
    wchar_t cmd[] = L"cmd.exe /C C:\\Users\\Ivan\\Desktop\\folder\\run.bat";

    //Create process
    CreateProcess(nullptr, cmd, nullptr, nullptr, false, CREATE_UNICODE_ENVIRONMENT, 
        const_cast<wchar_t*>(GetEnvString().c_str()), nullptr, &si, &pi);

    Sleep(1800000);

    //Process Termination
    TerminateProcess(pi.hProcess, 0);   

    // Close process and thread handles. 
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
}

I'd recommend you read up on the CreateProcess function, as well as the Terminate Process one. There is also an example from Microsoft about how to use the former of the two. I hope this information can help you.

edit: Fixed stuff. Should work now. Credits to user4581301, his links were really useful.

Shaddy
  • 59
  • 5