Then, my below command sometimes gives my desired output (all the gene tables corresponding to that protein ID), but now it doesn't give any output at all.
Did you sign up for NCBI_API_KEY? Use it when you do these queries.
NCBI is a public resource so if you are doing a large number of queries put a pause between query blocks. Even with NCBI API Key you are allowed a certain number of queries per unit time.
It is possible that some ID's may not work for various reasons (e.g. they could be deprecated). Entrezdirect does not have a good error handling mechanism built-in so you will need to deal with the errors after the search.