Farm animals are smart and stubborn. They rarely do what you say, unless you are standing right there, and even then they might do the opposite just to make you angry. You cannot leave them alone for one minute without them trying to tear down the place or destroy your fence and make for greener pastures. So you had better head on over to your nearest farm supply store and get the makings for an electric fence defense.
Once sure way to dissuade them from breaking out is by setting up an electric fence and shocking them into the reality that you are the boss. Now don’t go buying some puny, electrified single wire, just enough to scare off a mosquito. Otherwise, your farm animals will be laughing at you every time you step out of the house.
You are going to have to consider the type of animal you are trying to control in order to buy the appropriate voltage of fence. Now if you want to ward off short-haired cows, along with horses, and pigs, you will only need at least 700V, while long-haired cows, along with goats, and sheep will require a whopping 1200V.
Turning to an underground electric fence might be an option, but many find it to be inadequate for their purposes. It keeps your animal in, but does nothing to keep any other animals out. Also, it may shock the collared animal in parts of the ground where you did not intend it. A battery electric fence is a good option, since it does not need to be near any source of electricity. Make sure you buy an electric fence insulator for every point you require them on, so you do not have to delay your project and return to the supply store later.