Introduction
In spanning tree, path with the lowest cost towards root bridge it’s preferred. Switch adds cost of its incoming port and adds it to BPDU. Cost can be configured per interface, so we can influence path to the root bridge.
Setup
All interfaces are 1gbit which calculates path cost as 4 (2 for 10gbit and 19 for 100mbit). Switch adds cost of its interface that BPDU arrives.

We can amend cost to root bridge per-interface:
S2(config-if)#int eth0/0 S2(config-if)#spanning-tree cost 1000
Switch will generate TC trap and go through blocking, listening and forwarding stage:
*Jan 26 20:30:48.187: STP: VLAN0001 new root port Et0/1, cost 200 *Jan 26 20:30:48.187: STP: VLAN0001 sent Topology Change Notice on Et0/1 *Jan 26 20:30:48.187: STP[1]: Generating TC trap for port Ethernet0/0 *Jan 26 20:30:48.187: STP: VLAN0001 Et0/0 -> blocking