Bus #459 departs from Logan's Terminal C with service ending at the Salem commuter rail station where you could pick up the train for a 30-minute ride to Gloucester. I know that you asked about getting to Gloucester via commuter rail from North Station, but you could also consider taking a bus directly from Logan to a commuter rail station and on to Gloucester.