Extract running of cmd in background to function