Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect if CTest already running, to avoid unwanted concurrent CTest/CDash runs
function(ctest_once_only)
# check if CTest already running, error if so
if(WIN32)
find_program(tasklist NAMES tasklist REQUIRED)
execute_process(COMMAND ${tasklist} /fi "Imagename eq ctest.exe" /fo csv
TIMEOUT 10
RESULT_VARIABLE ret
OUTPUT_VARIABLE out
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT ret EQUAL 0)
message(FATAL_ERROR "Could not check if CTest already running")
endif()
string(REGEX MATCHALL "(ctest.exe)" mat "${out}")
list(LENGTH mat L)
if(NOT L EQUAL 1)
message(FATAL_ERROR "CTest already running ${L} times, not starting again.
${mat}")
endif()
else()
find_program(pgrep NAMES pgrep REQUIRED)
execute_process(COMMAND ${pgrep} ctest
TIMEOUT 10
RESULT_VARIABLE ret
OUTPUT_VARIABLE out
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(ret EQUAL 1)
message(VERBOSE "No CTest found running, proceeding.")
return()
elseif(NOT ret EQUAL 0)
message(FATAL_ERROR "Could not check if CTest already running")
endif()
string(REGEX MATCHALL "([0-9]+)" mat "${out}")
list(LENGTH mat L)
if(NOT L EQUAL 1)
message(FATAL_ERROR "CTest already running ${L} times, not starting again.
${mat}")
endif()
endif()
endfunction(ctest_once_only)
@scivision
Copy link
Author

scivision commented Jul 15, 2022

Uses regex to count number of times ctest is running. We expect only one instance.

This is used in CTest CDash mode to avoid unexpectedly long-running scripts to "pile up" on subsequent unwanted concurrent CTest runs, eventually filling swap memory and crashing the computer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment