Create a service to reboot Core Audio

Discussion in 'Mac Programming' started by the Western zoo, Jan 19, 2014.

  1. the Western zoo macrumors 6502

    the Western zoo

    Joined:
    May 16, 2006
    Location:
    Aarhus C, Denmark
    #1
    I know very little about coding, and next to nothing about using automator, so I am a little wary about just trying stuff on my own...

    I would like to make a service that will run a Terminal command to reboot Core Audio - sometimes Airplay messes up audio. I have found the Terminal command by Googling, and also I have an idea about how I should set it up in Automator, but I would like to get some input from people in the know.

    From what I have gathered, I should start a new service in Automator, choose run shell command, and paste the following:

    sudo kill `ps -ax | grep 'coreaudiod' | grep 'sbin' |awk '{print $1}'`

    I do not understand that command, but have found it by googling... If someone could explain what the individual parts mean I would appreciate it.

    Usually when I run this command in terminal I have to input my password afterward, is there a way around this when setting up this service?
     
  2. subsonix, Jan 20, 2014
    Last edited: Jan 20, 2014

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2

    Each program running on the system has a process id, the kill command expects a process id (pid for short) as an argument.

    Above, "kill" is followed by a whole string of commands that are surrounded by back tick quotes ` what these do, is to replace the quoted part with it's result, which in this case is the pid. (the purpose of this is to find the pid for the process named 'coraudiod').

    You can test that by running it in isolation. ps -ax lists running processes, this is then piped to a grep filter which only shows rows with 'coreaudiod'. A second filter is then used because grep itself will show up. The reason for this is that the argument to grep is 'coreaudiod'. And finally, only the first column is printed by specifying $1 in awk.

    All of this is unnecessary though, because there is a "killall" command that takes the process name as argument as opposed to the pid, so it can be replaced by:

    Code:
    sudo killall coreaudiod
    

    'coreaudiod' is owned by root, so you need to be super user to kill it.


    There may be better ways to fix the Airplay issue than killing the system wide audio daemon.
     
  3. the Western zoo thread starter macrumors 6502

    the Western zoo

    Joined:
    May 16, 2006
    Location:
    Aarhus C, Denmark
  4. 10smom macrumors regular

    10smom

    Joined:
    Mar 26, 2008
    Location:
    USA
    #4
    How did you create in automator

    Can you please explaing the process for creating this service in automator? Thanks for taking the time.
     
  5. snorkelman macrumors 6502a

    snorkelman

    Joined:
    Oct 25, 2010
    #5

    [​IMG]

    launch automator, choose service from the options

    [​IMG]

    select 'utilities'

    drag 'run applescript' into the main window

    set 'service receives' to 'no input'

    in the run applescript dialog enter the following text:

    Code:
    on run
    	
    	do shell script "sudo killall coreaudiod" user name "John Doe" password "abcd" with administrator privileges
    	
    end run
    replace John Doe with user account name and replace abcd with the users password

    alternately you could use

    Code:
    on run
    	
    	do shell script "sudo killall coreaudiod" with administrator privileges
    	
    end run
    
    
    though in that case you'll be prompted for password every time you call the service
     
  6. 10smom macrumors regular

    10smom

    Joined:
    Mar 26, 2008
    Location:
    USA
    #6
    Thank you so much for taking the time.

     

Share This Page