So, I'm attempting to have a script play a short ogg file on shutdown/restart but I seem to be having some sort of trouble actually getting it to work. Right now, I have the script in init.d and have symbolic links in rc0.d and rc6.d that all seem to work when manually executed. The script and the links all start with "K99" because the system supposedly proceeds alphabetically through the list and K99 should make it execute at the correct time. (I've tried other things, like K00, K50 and the "S" variations because I saw some other scripts with that; but they didn't seem to change anything.)
#!/bin/sh
## play shutdown sound
pulseaudio --start
echo -e "Shutdown sound script."
/usr/bin/play /home/username/Downloads/sound_file.ogg
sleep 3
The script is pretty simple I think. The ogg file and the script are both at 755 permissions. The file is only about 3s long, so I don't think that's a problem either. Originally, it seemed as if the script wasn't working at all so I added the sleep command. It seems to take about three seconds longer to shut down now, so I think the script IS running, but I have a feeling that Ubuntu is perhaps killing the audio daemon (if there is one?) before the script goes off, but I'm not sure. I've spent a few days trying to figure this seemingly simple thing out. Any ideas?
Sorry if this ends up being trivial, I'm a bit of a novice with Ubuntu system things. (Though I'd be fine with a simple fix! :) )
UPDATE It works as it should now! After some searching about why the sound might be playing multiple times and especially on startup, I learned a bit more about the upstart system. It looks through the etc/init/ directory and runs the scripts in there on start up. I remembered that I had experimented by putting an upstart script there when the sound was still not playing at all. Apparently I had forgotten to remove it and it was executing on all run levels thus leading to the double shutdown and startup instances. Now it's gone and all is as it should be. (Although now that I've fixed it, I'm a bit sick of hearing the sound and might have to find a new one, lol.) Thanks for the help, all.
TL;DR: Put a script in init.d and symbolically link to etc/rc0.d (and etc/rc6.d if you want it on reboot too). If it plays multiple times, you've probably got a duplicate somewhere it shouldn't be.
To execute a script at shutdown
sudo chmod +x script.sh
)PulseAudio
PulseAudio is a sound server. It allows you to do advanced operations on your sound data as it passes between your application and your hardware.
Start PulseAudio at the begging of your script:
pulseaudio --start
Note: You must try/use this commands in your script because starting audio daemon and playing your ogg file is just a matter of time.