There are many forms of Amr bil Ma’ruf (i.e. Commanding Righteousness):
(1) If one knows that by speaking to a person (who is sinning) about what he is doing, there is a greater likelihood that he will refrain from it and take heed, then Amr bil Ma’ruf is Waajib (compulsory). It is not permissible for him to abstain from cautioning such a person.
(2) If one knows that the ones committing the sinful act will level accusations against him and become vulgar towards him, it is better to avoid it.
(3) If he knows that they will beat him and he will not be able to bear this beating, or there will be strife and conflict because of this and a fight or clash will ensue amongst them due to this, then (in this case too), it is better to desist.
(4) If he knows that by cautioning them they will hit him, but he will be able to bear this and be patient in this regard, then he should forbid them from doing the wrong deed.
(5) If this person is a Mujaahid and he knows that they will not listen to him, and nor will they hit him or be vulgar towards him, then he has the choice (of either forbidding them or not), and it is better that he should command what is right.
— — —
Bahar-e-Shari'at, Vol. 16, Page 615
Fatawa Alamgiri, Vol. 5, Page 352-353