Batch Stop Program A on Launch Program B, Start Program A on End Program B
I synchronize my documents using Google's sync program. However, I don't want the sync program meddling with my project files when I have certain programs open. My attempted solution is below.
This is a really clunky way to attempt to turn off program A when program B launches, then turn program A back on when program B stops running. The batch script below does this effectively, but it takes a sizable chunk of processor power (between 1 and 4% on my mobile i5 8th gen). Is there a more elegant way to do this? I'm willing to consider other tools (ie, AutoIT).
EDIT, per suggestion, I added a simple 15-second timeout at the end, which makes this program acceptably processor intensive (ie, not). Running this via task scheduler as hidden makes this a working solution.
@echo off
rem mutually exclusive program enforcer
rem initialize variables
set gDriveOn=init
set reaperOn=init
rem begin infinite loop for monitoring on states
:loopStart
tasklist /FI "IMAGENAME eq reaper.exe" 2>NUL | find /I /N "reaper.exe">NUL
if "%ERRORLEVEL%"=="0" (
set reaperOn="TRUE"
goto gDrivecheck
)
rem else if reaper not found
set reaperOn="FALSE"
:gDriveCheck
tasklist /FI "IMAGENAME eq googledrivesync.exe" 2>NUL | find /I /N "googledrivesync.exe">NUL
if "%ERRORLEVEL%"=="0" (
set gDriveOn="TRUE"
goto doStuff
)
rem else if Google Drive not found
set gDriveOn="FALSE"
:doStuff
rem turn off gdrive when reaper is on
if %reaperOn% == "TRUE" (
if %gDriveOn% == "TRUE" (
taskkill /f /t /im googledrivesync.exe
)
)
if %reaperOn% == "FALSE" (
if %gDriveOn% == "FALSE" (
start C:"Program Files"GoogleDrivegoogledrivesync.exe
)
)
rem delay 15 seconds for efficiency
timeout 15
goto :loopStart
)
rem end of infinite loop
)
batch-file batch-processing
add a comment |
I synchronize my documents using Google's sync program. However, I don't want the sync program meddling with my project files when I have certain programs open. My attempted solution is below.
This is a really clunky way to attempt to turn off program A when program B launches, then turn program A back on when program B stops running. The batch script below does this effectively, but it takes a sizable chunk of processor power (between 1 and 4% on my mobile i5 8th gen). Is there a more elegant way to do this? I'm willing to consider other tools (ie, AutoIT).
EDIT, per suggestion, I added a simple 15-second timeout at the end, which makes this program acceptably processor intensive (ie, not). Running this via task scheduler as hidden makes this a working solution.
@echo off
rem mutually exclusive program enforcer
rem initialize variables
set gDriveOn=init
set reaperOn=init
rem begin infinite loop for monitoring on states
:loopStart
tasklist /FI "IMAGENAME eq reaper.exe" 2>NUL | find /I /N "reaper.exe">NUL
if "%ERRORLEVEL%"=="0" (
set reaperOn="TRUE"
goto gDrivecheck
)
rem else if reaper not found
set reaperOn="FALSE"
:gDriveCheck
tasklist /FI "IMAGENAME eq googledrivesync.exe" 2>NUL | find /I /N "googledrivesync.exe">NUL
if "%ERRORLEVEL%"=="0" (
set gDriveOn="TRUE"
goto doStuff
)
rem else if Google Drive not found
set gDriveOn="FALSE"
:doStuff
rem turn off gdrive when reaper is on
if %reaperOn% == "TRUE" (
if %gDriveOn% == "TRUE" (
taskkill /f /t /im googledrivesync.exe
)
)
if %reaperOn% == "FALSE" (
if %gDriveOn% == "FALSE" (
start C:"Program Files"GoogleDrivegoogledrivesync.exe
)
)
rem delay 15 seconds for efficiency
timeout 15
goto :loopStart
)
rem end of infinite loop
)
batch-file batch-processing
I cannot finddoMoreStuff:
line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)
– Jack White
Nov 14 '18 at 21:08
There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
– Zediiiii
Nov 15 '18 at 22:28
You have at least one syntax error:domorestuff
needs to start with:
. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.
– Jack White
Nov 17 '18 at 8:00
I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
– Zediiiii
Nov 18 '18 at 9:13
add a comment |
I synchronize my documents using Google's sync program. However, I don't want the sync program meddling with my project files when I have certain programs open. My attempted solution is below.
This is a really clunky way to attempt to turn off program A when program B launches, then turn program A back on when program B stops running. The batch script below does this effectively, but it takes a sizable chunk of processor power (between 1 and 4% on my mobile i5 8th gen). Is there a more elegant way to do this? I'm willing to consider other tools (ie, AutoIT).
EDIT, per suggestion, I added a simple 15-second timeout at the end, which makes this program acceptably processor intensive (ie, not). Running this via task scheduler as hidden makes this a working solution.
@echo off
rem mutually exclusive program enforcer
rem initialize variables
set gDriveOn=init
set reaperOn=init
rem begin infinite loop for monitoring on states
:loopStart
tasklist /FI "IMAGENAME eq reaper.exe" 2>NUL | find /I /N "reaper.exe">NUL
if "%ERRORLEVEL%"=="0" (
set reaperOn="TRUE"
goto gDrivecheck
)
rem else if reaper not found
set reaperOn="FALSE"
:gDriveCheck
tasklist /FI "IMAGENAME eq googledrivesync.exe" 2>NUL | find /I /N "googledrivesync.exe">NUL
if "%ERRORLEVEL%"=="0" (
set gDriveOn="TRUE"
goto doStuff
)
rem else if Google Drive not found
set gDriveOn="FALSE"
:doStuff
rem turn off gdrive when reaper is on
if %reaperOn% == "TRUE" (
if %gDriveOn% == "TRUE" (
taskkill /f /t /im googledrivesync.exe
)
)
if %reaperOn% == "FALSE" (
if %gDriveOn% == "FALSE" (
start C:"Program Files"GoogleDrivegoogledrivesync.exe
)
)
rem delay 15 seconds for efficiency
timeout 15
goto :loopStart
)
rem end of infinite loop
)
batch-file batch-processing
I synchronize my documents using Google's sync program. However, I don't want the sync program meddling with my project files when I have certain programs open. My attempted solution is below.
This is a really clunky way to attempt to turn off program A when program B launches, then turn program A back on when program B stops running. The batch script below does this effectively, but it takes a sizable chunk of processor power (between 1 and 4% on my mobile i5 8th gen). Is there a more elegant way to do this? I'm willing to consider other tools (ie, AutoIT).
EDIT, per suggestion, I added a simple 15-second timeout at the end, which makes this program acceptably processor intensive (ie, not). Running this via task scheduler as hidden makes this a working solution.
@echo off
rem mutually exclusive program enforcer
rem initialize variables
set gDriveOn=init
set reaperOn=init
rem begin infinite loop for monitoring on states
:loopStart
tasklist /FI "IMAGENAME eq reaper.exe" 2>NUL | find /I /N "reaper.exe">NUL
if "%ERRORLEVEL%"=="0" (
set reaperOn="TRUE"
goto gDrivecheck
)
rem else if reaper not found
set reaperOn="FALSE"
:gDriveCheck
tasklist /FI "IMAGENAME eq googledrivesync.exe" 2>NUL | find /I /N "googledrivesync.exe">NUL
if "%ERRORLEVEL%"=="0" (
set gDriveOn="TRUE"
goto doStuff
)
rem else if Google Drive not found
set gDriveOn="FALSE"
:doStuff
rem turn off gdrive when reaper is on
if %reaperOn% == "TRUE" (
if %gDriveOn% == "TRUE" (
taskkill /f /t /im googledrivesync.exe
)
)
if %reaperOn% == "FALSE" (
if %gDriveOn% == "FALSE" (
start C:"Program Files"GoogleDrivegoogledrivesync.exe
)
)
rem delay 15 seconds for efficiency
timeout 15
goto :loopStart
)
rem end of infinite loop
)
batch-file batch-processing
batch-file batch-processing
edited Nov 18 '18 at 9:52
asked Nov 12 '18 at 0:34
Zediiiii
368313
368313
I cannot finddoMoreStuff:
line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)
– Jack White
Nov 14 '18 at 21:08
There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
– Zediiiii
Nov 15 '18 at 22:28
You have at least one syntax error:domorestuff
needs to start with:
. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.
– Jack White
Nov 17 '18 at 8:00
I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
– Zediiiii
Nov 18 '18 at 9:13
add a comment |
I cannot finddoMoreStuff:
line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)
– Jack White
Nov 14 '18 at 21:08
There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
– Zediiiii
Nov 15 '18 at 22:28
You have at least one syntax error:domorestuff
needs to start with:
. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.
– Jack White
Nov 17 '18 at 8:00
I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
– Zediiiii
Nov 18 '18 at 9:13
I cannot find
doMoreStuff:
line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)– Jack White
Nov 14 '18 at 21:08
I cannot find
doMoreStuff:
line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)– Jack White
Nov 14 '18 at 21:08
There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
– Zediiiii
Nov 15 '18 at 22:28
There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
– Zediiiii
Nov 15 '18 at 22:28
You have at least one syntax error:
domorestuff
needs to start with :
. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.– Jack White
Nov 17 '18 at 8:00
You have at least one syntax error:
domorestuff
needs to start with :
. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.– Jack White
Nov 17 '18 at 8:00
I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
– Zediiiii
Nov 18 '18 at 9:13
I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
– Zediiiii
Nov 18 '18 at 9:13
add a comment |
1 Answer
1
active
oldest
votes
Because posted source code doesn't run I won't yet attempt to answer stability part of the question. See my former comment on suggestion how to debug it yourself.
As for performance:
Batch file delays
if I didn't miss anything, this code is constantly requesting if a pair of processes exist. That is, it starts the next check right after previous one completes, again and again with no pause inbetween.
This basically instructs your computer to devote all available processing power to checking process existence. This program should theoretically be using 100% CPU, but since for some reason listing tasks under Windows is slow and inefficient, and you probably have more that one CPU core, and batch processing is slow itself it only is able to use a fraction of CPU.
You most likely do not need to check processes that frequently. This is usually done once in 1 to 10 seconds. If this is acceptable, insert a delay somewhere in the code like this:
TIMEOUT /T 5 /NOBREAK
where 5 is seconds to wait. See timeout /?
or this for more information.
AutoIT
In AutoIt you will need ProcessExists()
to check process existence and Sleep()
to insert a delay. Killing a process is accomplished with ProcessClose()
and Run()
starts one. The documentation is available here under Function Reference
Checking processes on windows is still slow and inefficient. AutoIT documentation suggests that the process is polled approximately every 250 milliseconds
which probably implies that if you try to request it any faster you will get results it saved from the last time, so it makes sense to insert a Sleep(250)
or more.
However, this mostly applies to querying process by name which requests a list of all running processes and searches yours in it. This does not need to be done if you know a process already exists - you can simply use its Process ID number (PID) to check if it stopped - which should be faster. So instead of just ProcessExists("notepad.exe")
you could do something like this:
local $pid = 0
while true
if ($pid <> 0) then $pid = ProcessExists($pid) ;Process already existed, use PID
if ($pid = 0) then $pid = ProcessExists("notepad.exe") ;Process did not exist
if ($pid <> 0) then
;do something if process exists...
endif
sleep(300)
.........
wend
This works because ProcessExists()
returns PID if process exists or 0 if not.
First if
line runs if $pid
was already known from last time we checked.
If it wasn't or if we never checked before or if it was but no longer exists then second line runs and does the check based on process name. This should be repeated for the second process. Please note this is intentionally not if-then-else
because 1st check may set $pid
to 0
.
EXE replacement
If the solutions above do not work -- for example because the reaper.exe
just crashes horribly if it happens to attempt accessing something googledrivesync.exe
is holding -- then something else should probably be done.
One option is to rename reaper.exe
to real_reaper.exe
and compile an autoit script into new reaper.exe
that kills googledrivesync
and starts real_reaper.exe
. This script needs to pass all parameters so something like
RunWait ('"real_reaper.exe" ' & $CmdLineRaw)
should do the trick. This waits for real_reaper.exe
to finish running and then you can do Run("googledrivesync.exe")
This may not work properly if the program that starts reaper.exe
uses some tricks but it should work in most cases.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254648%2fbatch-stop-program-a-on-launch-program-b-start-program-a-on-end-program-b%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because posted source code doesn't run I won't yet attempt to answer stability part of the question. See my former comment on suggestion how to debug it yourself.
As for performance:
Batch file delays
if I didn't miss anything, this code is constantly requesting if a pair of processes exist. That is, it starts the next check right after previous one completes, again and again with no pause inbetween.
This basically instructs your computer to devote all available processing power to checking process existence. This program should theoretically be using 100% CPU, but since for some reason listing tasks under Windows is slow and inefficient, and you probably have more that one CPU core, and batch processing is slow itself it only is able to use a fraction of CPU.
You most likely do not need to check processes that frequently. This is usually done once in 1 to 10 seconds. If this is acceptable, insert a delay somewhere in the code like this:
TIMEOUT /T 5 /NOBREAK
where 5 is seconds to wait. See timeout /?
or this for more information.
AutoIT
In AutoIt you will need ProcessExists()
to check process existence and Sleep()
to insert a delay. Killing a process is accomplished with ProcessClose()
and Run()
starts one. The documentation is available here under Function Reference
Checking processes on windows is still slow and inefficient. AutoIT documentation suggests that the process is polled approximately every 250 milliseconds
which probably implies that if you try to request it any faster you will get results it saved from the last time, so it makes sense to insert a Sleep(250)
or more.
However, this mostly applies to querying process by name which requests a list of all running processes and searches yours in it. This does not need to be done if you know a process already exists - you can simply use its Process ID number (PID) to check if it stopped - which should be faster. So instead of just ProcessExists("notepad.exe")
you could do something like this:
local $pid = 0
while true
if ($pid <> 0) then $pid = ProcessExists($pid) ;Process already existed, use PID
if ($pid = 0) then $pid = ProcessExists("notepad.exe") ;Process did not exist
if ($pid <> 0) then
;do something if process exists...
endif
sleep(300)
.........
wend
This works because ProcessExists()
returns PID if process exists or 0 if not.
First if
line runs if $pid
was already known from last time we checked.
If it wasn't or if we never checked before or if it was but no longer exists then second line runs and does the check based on process name. This should be repeated for the second process. Please note this is intentionally not if-then-else
because 1st check may set $pid
to 0
.
EXE replacement
If the solutions above do not work -- for example because the reaper.exe
just crashes horribly if it happens to attempt accessing something googledrivesync.exe
is holding -- then something else should probably be done.
One option is to rename reaper.exe
to real_reaper.exe
and compile an autoit script into new reaper.exe
that kills googledrivesync
and starts real_reaper.exe
. This script needs to pass all parameters so something like
RunWait ('"real_reaper.exe" ' & $CmdLineRaw)
should do the trick. This waits for real_reaper.exe
to finish running and then you can do Run("googledrivesync.exe")
This may not work properly if the program that starts reaper.exe
uses some tricks but it should work in most cases.
add a comment |
Because posted source code doesn't run I won't yet attempt to answer stability part of the question. See my former comment on suggestion how to debug it yourself.
As for performance:
Batch file delays
if I didn't miss anything, this code is constantly requesting if a pair of processes exist. That is, it starts the next check right after previous one completes, again and again with no pause inbetween.
This basically instructs your computer to devote all available processing power to checking process existence. This program should theoretically be using 100% CPU, but since for some reason listing tasks under Windows is slow and inefficient, and you probably have more that one CPU core, and batch processing is slow itself it only is able to use a fraction of CPU.
You most likely do not need to check processes that frequently. This is usually done once in 1 to 10 seconds. If this is acceptable, insert a delay somewhere in the code like this:
TIMEOUT /T 5 /NOBREAK
where 5 is seconds to wait. See timeout /?
or this for more information.
AutoIT
In AutoIt you will need ProcessExists()
to check process existence and Sleep()
to insert a delay. Killing a process is accomplished with ProcessClose()
and Run()
starts one. The documentation is available here under Function Reference
Checking processes on windows is still slow and inefficient. AutoIT documentation suggests that the process is polled approximately every 250 milliseconds
which probably implies that if you try to request it any faster you will get results it saved from the last time, so it makes sense to insert a Sleep(250)
or more.
However, this mostly applies to querying process by name which requests a list of all running processes and searches yours in it. This does not need to be done if you know a process already exists - you can simply use its Process ID number (PID) to check if it stopped - which should be faster. So instead of just ProcessExists("notepad.exe")
you could do something like this:
local $pid = 0
while true
if ($pid <> 0) then $pid = ProcessExists($pid) ;Process already existed, use PID
if ($pid = 0) then $pid = ProcessExists("notepad.exe") ;Process did not exist
if ($pid <> 0) then
;do something if process exists...
endif
sleep(300)
.........
wend
This works because ProcessExists()
returns PID if process exists or 0 if not.
First if
line runs if $pid
was already known from last time we checked.
If it wasn't or if we never checked before or if it was but no longer exists then second line runs and does the check based on process name. This should be repeated for the second process. Please note this is intentionally not if-then-else
because 1st check may set $pid
to 0
.
EXE replacement
If the solutions above do not work -- for example because the reaper.exe
just crashes horribly if it happens to attempt accessing something googledrivesync.exe
is holding -- then something else should probably be done.
One option is to rename reaper.exe
to real_reaper.exe
and compile an autoit script into new reaper.exe
that kills googledrivesync
and starts real_reaper.exe
. This script needs to pass all parameters so something like
RunWait ('"real_reaper.exe" ' & $CmdLineRaw)
should do the trick. This waits for real_reaper.exe
to finish running and then you can do Run("googledrivesync.exe")
This may not work properly if the program that starts reaper.exe
uses some tricks but it should work in most cases.
add a comment |
Because posted source code doesn't run I won't yet attempt to answer stability part of the question. See my former comment on suggestion how to debug it yourself.
As for performance:
Batch file delays
if I didn't miss anything, this code is constantly requesting if a pair of processes exist. That is, it starts the next check right after previous one completes, again and again with no pause inbetween.
This basically instructs your computer to devote all available processing power to checking process existence. This program should theoretically be using 100% CPU, but since for some reason listing tasks under Windows is slow and inefficient, and you probably have more that one CPU core, and batch processing is slow itself it only is able to use a fraction of CPU.
You most likely do not need to check processes that frequently. This is usually done once in 1 to 10 seconds. If this is acceptable, insert a delay somewhere in the code like this:
TIMEOUT /T 5 /NOBREAK
where 5 is seconds to wait. See timeout /?
or this for more information.
AutoIT
In AutoIt you will need ProcessExists()
to check process existence and Sleep()
to insert a delay. Killing a process is accomplished with ProcessClose()
and Run()
starts one. The documentation is available here under Function Reference
Checking processes on windows is still slow and inefficient. AutoIT documentation suggests that the process is polled approximately every 250 milliseconds
which probably implies that if you try to request it any faster you will get results it saved from the last time, so it makes sense to insert a Sleep(250)
or more.
However, this mostly applies to querying process by name which requests a list of all running processes and searches yours in it. This does not need to be done if you know a process already exists - you can simply use its Process ID number (PID) to check if it stopped - which should be faster. So instead of just ProcessExists("notepad.exe")
you could do something like this:
local $pid = 0
while true
if ($pid <> 0) then $pid = ProcessExists($pid) ;Process already existed, use PID
if ($pid = 0) then $pid = ProcessExists("notepad.exe") ;Process did not exist
if ($pid <> 0) then
;do something if process exists...
endif
sleep(300)
.........
wend
This works because ProcessExists()
returns PID if process exists or 0 if not.
First if
line runs if $pid
was already known from last time we checked.
If it wasn't or if we never checked before or if it was but no longer exists then second line runs and does the check based on process name. This should be repeated for the second process. Please note this is intentionally not if-then-else
because 1st check may set $pid
to 0
.
EXE replacement
If the solutions above do not work -- for example because the reaper.exe
just crashes horribly if it happens to attempt accessing something googledrivesync.exe
is holding -- then something else should probably be done.
One option is to rename reaper.exe
to real_reaper.exe
and compile an autoit script into new reaper.exe
that kills googledrivesync
and starts real_reaper.exe
. This script needs to pass all parameters so something like
RunWait ('"real_reaper.exe" ' & $CmdLineRaw)
should do the trick. This waits for real_reaper.exe
to finish running and then you can do Run("googledrivesync.exe")
This may not work properly if the program that starts reaper.exe
uses some tricks but it should work in most cases.
Because posted source code doesn't run I won't yet attempt to answer stability part of the question. See my former comment on suggestion how to debug it yourself.
As for performance:
Batch file delays
if I didn't miss anything, this code is constantly requesting if a pair of processes exist. That is, it starts the next check right after previous one completes, again and again with no pause inbetween.
This basically instructs your computer to devote all available processing power to checking process existence. This program should theoretically be using 100% CPU, but since for some reason listing tasks under Windows is slow and inefficient, and you probably have more that one CPU core, and batch processing is slow itself it only is able to use a fraction of CPU.
You most likely do not need to check processes that frequently. This is usually done once in 1 to 10 seconds. If this is acceptable, insert a delay somewhere in the code like this:
TIMEOUT /T 5 /NOBREAK
where 5 is seconds to wait. See timeout /?
or this for more information.
AutoIT
In AutoIt you will need ProcessExists()
to check process existence and Sleep()
to insert a delay. Killing a process is accomplished with ProcessClose()
and Run()
starts one. The documentation is available here under Function Reference
Checking processes on windows is still slow and inefficient. AutoIT documentation suggests that the process is polled approximately every 250 milliseconds
which probably implies that if you try to request it any faster you will get results it saved from the last time, so it makes sense to insert a Sleep(250)
or more.
However, this mostly applies to querying process by name which requests a list of all running processes and searches yours in it. This does not need to be done if you know a process already exists - you can simply use its Process ID number (PID) to check if it stopped - which should be faster. So instead of just ProcessExists("notepad.exe")
you could do something like this:
local $pid = 0
while true
if ($pid <> 0) then $pid = ProcessExists($pid) ;Process already existed, use PID
if ($pid = 0) then $pid = ProcessExists("notepad.exe") ;Process did not exist
if ($pid <> 0) then
;do something if process exists...
endif
sleep(300)
.........
wend
This works because ProcessExists()
returns PID if process exists or 0 if not.
First if
line runs if $pid
was already known from last time we checked.
If it wasn't or if we never checked before or if it was but no longer exists then second line runs and does the check based on process name. This should be repeated for the second process. Please note this is intentionally not if-then-else
because 1st check may set $pid
to 0
.
EXE replacement
If the solutions above do not work -- for example because the reaper.exe
just crashes horribly if it happens to attempt accessing something googledrivesync.exe
is holding -- then something else should probably be done.
One option is to rename reaper.exe
to real_reaper.exe
and compile an autoit script into new reaper.exe
that kills googledrivesync
and starts real_reaper.exe
. This script needs to pass all parameters so something like
RunWait ('"real_reaper.exe" ' & $CmdLineRaw)
should do the trick. This waits for real_reaper.exe
to finish running and then you can do Run("googledrivesync.exe")
This may not work properly if the program that starts reaper.exe
uses some tricks but it should work in most cases.
edited Nov 18 '18 at 13:19
answered Nov 17 '18 at 9:56
Jack White
33416
33416
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254648%2fbatch-stop-program-a-on-launch-program-b-start-program-a-on-end-program-b%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I cannot find
doMoreStuff:
line in your source code. Try opening a command prompt and running it from there: this way if there is a syntax error you will see what it is. If this does not help please edit your post and include logs of its operation (i.e. copy all text from command prompt window after running the script)– Jack White
Nov 14 '18 at 21:08
There were a couple issues with the code - both of which I fixed in the last edit - thanks. However, this takes an unacceptable amount of processing power. I'm looking for a more efficient solution.
– Zediiiii
Nov 15 '18 at 22:28
You have at least one syntax error:
domorestuff
needs to start with:
. You really need to make sure the code you posted works exactly as you described - run it and test it. If your code is no longer misbehaving, please edit the question and remove the clause about dying.– Jack White
Nov 17 '18 at 8:00
I did fix the code - but I apparently didn't post it despite my comment stating I had. Polling is a slightly better solution, and the pid truck is clever in AutoIT. I'm surprised if there isn't a low resource way to monitor if a program is running built in to Windows.
– Zediiiii
Nov 18 '18 at 9:13