Having a ritual that you feel the need to follow through with is not inherently harmful, but there is a pretty good chance that it might be a manifestation of obsessive compulsive disorder if you get panic attacks if you’re not able to perform said ritual. Obsessive compulsive people often wash their hands for hours on end to the point where their skin starts to peel off, and they can also sometimes have intrusive thoughts that will haunt them and prevent them from being able to get a good night’s sleep.
The truth of the situation is that there is no clear consensus on what causes OCD, but you can visit here to get some more knowledge about it just in case you are worried that you yourself might have it. If you have been struggling with obsessive compulsive tendencies and you feel like they are getting worse, learning what might be causing this might help. This is because of the fact that it can allow you to contextualize what you are going through and see that it has a real world cause instead of just existing in your head.
One of the many things that can worsen your OCD symptoms is trauma, and that can include abuse of any kind as well as getting into an accident. Relying on obsessive patterns and routines becomes a bit of a coping mechanism, but the fact of the matter is that it is a decidedly unhealthy one. You can go for psychotherapy to try to work towards getting cured, or you can opt for the psychiatric route by taking medicine that is proven to reduce compulsive tendencies to a certain extent.