Is there any way to keep my minecraft server from going through the day/night cycle when no one's online?

Solution 1:

You should be able to use command blocks to automatically detect whether players are online or not, then appropriately set the doDaylightCycle rule appropriately. Try placing the following setup in the server spawn chunks:

enter image description here

(Piston is a sticky one.)

The general idea is that the left command checks for online players a few times every second and the comparator next to it will only output a signal if there is at least one player online. Once everyone logs off, the piston retracts and turns off the day/night cycle. As soon as someone logs in again, the comparator will turn on again and toggle the day/night cycle back on.

Solution 2:

As an alternative to @SpectralFlame's solution that works with a setblock/fill clock (for reduced lag), you can use the following two commands (in this order):

gamerule doDaylightCycle false
execute @p ~ ~ ~ gamerule doDaylightCycle true

The second command will fail when there is no player to execute on, meaning the result of the first command won't be immediately overridden.