You can create a process as per below description:
Use "Query single row" component in workbench and execute below mentioned query:
select status from tb_process_instance where long_lived_invocation_id = '{$ /process_data/@InstanceID $}';
(where "InstanceID" is a string type input variable for this process).
The above query would give you status of the Process instance you would provide as input.
This status would be in number:
1 Running
3 Completed
You can create conditional routes in your process based on the status number you get.
If you need to do this programmatically, you can invoke this process as a web service.
Regarding your other query, there are several ways an operation can be stalled and based on those scenarios different queries are required to get the status from the database.
So, you need to figure out which stall scenario you want to target specifically, else adminui would be better option.