I think I know your issue here. Commands are not always loaded into the game. They only load in if a player is near them.
However, there is a way around this. You will need to set a ticking area at the command block.
A ticking area basically sets a group of chunks to always be loaded even if nobody is around.
Use the command /tickingarea