Muting system volume upon logout using launch agent I'm attempting to create a script for muting the system volume which gets triggered upon logging out or shutting down. This is to avoid hearing the startup chime really loudly the next time I start the machine.

Using the instructions provided in this post, I have created a RunAtLoad launch agent that calls the script shown below:

#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

IFS=$'\n\t'

onLogout ()
{
    osascript -e "set volume output volume 0"
    exit
}

trap onLogout SIGINT SIGHUP SIGTERM

while true; do
    say starting
    sleep 86400 &amp;
    wait $!
done


The idea being that the script is called and made to sleep until it receives one of the trapped signals, at which point the onLogout function is called in order to mute the volume.

Currently it's not working as intended, the volume remains unchanged when I log out and log back in. However, I've found that if I manually unload the launch agent that launches the script (using a launchctl unload call), then it works perfectly. The system volume is muted immediately.

Initially I thought perhaps the trapped signals weren't being received by the script when I performed a logout. But after adding some debugging code to the onLogout function as shown below, I have eliminated this as a possible cause. 

onLogout ()
{
    printf "Before mute\n" &gt;&gt; ~/Desktop/log.txt
    osascript -e "set volume output volume 0"
    printf "After mute\n" &gt;&gt; ~/Desktop/log.txt
    exit
}


When I performed a logout, and logged back in, there was a "log.txt" file on the desktop with both of the Before mute and After mute debugging lines present.

And I don't believe the reason is that the osascript call for performing the muting is somehow failing when a logout is carried out. Due to the set -o errexit line in the script, if any part of the script fails, then it should halt execution and exit immediately. This means that if the muting operation is failing, I shouldn't be seeing the After muting line in my log file. Indeed, when I tested this by replacing the osascript line with something I knew was guaranteed to fail, the log contained only the Before muting line as expected.

I'm at a loss to explain what's going on. Clearly the onLogout function is being called correctly as explained above, but for some reason the osascript call seems to only run when I manually unload using launchctl. When I log out manually it just skips over it somehow.

Any help would be greatly appreciated.
In Terminal, execute the following command to disable the chime completely until reenabled:

sudo nvram SystemAudioVolume=%80

If this setting is not retained, try:

sudo nvram SystemAudioVolume=%01

sudo nvram SystemAudioVolume=%00

or

sudo nvram SystemAudioVolume="  "

To reenable the chime:

sudo nvram -d SystemAudioVolume

Info gleaned from:

https://business.tutsplus.com/tutorials/how-to-silence-the-startup-chime-on-a-mac--cms-21212

https://www.howtogeek.com/260693/how-to-disable-the-boot-sound-or-startup-chime-on-a-mac/