* Herath continues to torment Pakistan
* Jayawardene scores 50th half century in final test (Adds Sangakkara quotes)
COLOMBO, Aug 17 (Reuters) - Left-arm spinner Rangana Herath captured four more Pakistan wickets to take his match tally to a career best 13 as Sri Lanka edged closer to victory in Mahela Jayawardene's farewell test on Sunday.
Pakistan, chasing 271 to level the series, were struggling on 127 for seven wickets by the close of the fourth day of the second test.
Sarfraz Ahmed (38) and Wahab Riaz (2) will resume at the crease on the fifth day with Pakistan trailing by 144 runs.
Sri Lanka lead the two-test series 1-0.
Pakistan began their run chase after tea but they struggled from the outset and fast bowler Dhammika Prasad snapped up the wickets of openers Khurram Manzoor (10) and Ahmed Shehzad (8).
Herath, who took nine wickets in the first innings, continued to torment the Pakistan batsmen and had Azhar Ali (10) and out-of-form captain Misbah-ul-Haq (3) both caught at slip by Jayawardene before Younus Khan (8) was trapped lbw.
Asad Shafiq and Sarfraz Ahmed tried to rescue the innings with a 55-run stand but Herath managed to separate them when Shafiq was stumped by wicketkeeper Niroshan Dickwella for 32.
Pakistan also lost the wicket of Abdur Rehman (5) when he was given out lbw to Dilruwan Perera after failing to offer a stroke to a ball that spun in from off stump.
"We were in a little bit of trouble with the bat today because we scored only 100 runs for the loss of (the last) eight wickets but that shows how difficult the wicket was," Kumar Sangakkara told reporters.
"We showed a lot of fight to get to a lead of 270.
"Then the magic of Herath was back in the game, he's been incredible for us in the last few years. All credit to him he's worked hard and done all the hard work for us in the middle and he is reaping the rewards of that."
Earlier, Jayawardene signed off from test cricket with the bat with his fiftieth half-century as Sri Lanka were dismissed for 282 in their second innings.
Sri Lanka's last five wickets added 35 runs in the afternoon session with captain Angelo Mathews remaining unbeaten on 43 when the innings ended.
Wahab Riaz finished off the Sri Lankan innings with three wickets while two batsmen were run out.
The left-arm paceman struck twice in one over after lunch to remove Dickwella (21) and Perera for a duck - both trapped lbw - and then bowled Herath, before the batsman had scored, to finish with figures of three for 76.
Prasad did not last long after hitting a six and a four in his score of 19 as he was run out when Mathews tried to retain the strike and called him for a quick single. Last man Chanaka Welegedara was also run out without scoring.
Ajmal, who bowled unchanged throughout the day, finished with figures of three for 89 off 46 overs.
On a pitch offering bounce and sharp turn, Sri Lanka struggled against Ajmal, who took all three wickets in the morning session.
The Pakistan attack was still without paceman Junaid Khan, who did not take the field after suffering a blow on the left side of his head on the third day against Dhammika Prasad.
The visitors picked up the experienced pair of Sangakkara and Jayawardene in the first 30 minutes of play.
Sangakkara (59) had added just five to his overnight score when he pushed forward to a delivery from Ajmal that spun and bounced and took the edge to end up in the hands of Azhar Ali at silly point.
Sangakkara's dismissal ended a third-wicket stand of 107 with Jayawardene, the 19th occasion the pair had been involved in a century stand.
Jayawardene (54) completed his fifty with a swept four to fine leg off Ajmal, bringing the crowd on their feet.
On 53, he was lucky to survive a confident appeal for a caught behind off Wahab when the third umpire ruled in favour of the batsman after replays proved inconclusive on the authenticity of the catch held by wicketkeeper Ahmed.
Jayawardene, however, did not survive long as a run later he tried to hit Ajmal over midwicket but only managed to hit it in the air. Shehzad ran back from midwicket to hold onto a good catch.
The former Sri Lanka captain received a standing ovation from the crowd all the way back to the pavilion.
He ended his batting career in tests with a tally of 11,814 runs from 149 tests at an average of 49.84, making him the seventh highest run-scorer in the longest format.
"We had wanted to give Mahela a win to go out on and we hope that if we play well again tomorrow, we can do that," said Sangakkara.
"The only thing that can bother us is the rain. We're going to come back tomorrow and make sure we try and get those wickets as early as we can." (Editing by Michael Hann)