Execute a .command file on logout?

I used the defaults tool, and com.apple.loginwindow LogoutHook thing:


sudo defaults write com.apple.loginwindow LogoutHook /Users/admin/Desktop/ifconfdown_mutevol.command


And if I do:


sudo defaults read com.apple.loginwindow LogoutHook


it does show the path:

"/Users/admin/Desktop/ifconfdown_mutevol.command"


but the commands contained the file ifconfdown_mutevol.command ARE NOT executed:

"sudo ifconfig en0 down && sleep 1 && osascript -e "set Volume 0" && sleep 1 && osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &

exit"


What's wrong? I know I'd use automator, but I'd prefer to do it manually as I want to learn about the structure of OS X more.

MacBook Air, OS X Yosemite (10.10.5)

Posted on Oct 30, 2015 12:32 PM

Reply
21 replies

Nov 2, 2015 8:44 PM in response to rccharles

How are you invoking this code?

-- apple icon > logoff

-- apple icon > shutdown


What do you expect your logoff code to do?


The only thing that I can figure it should do is set volume to mute. What happening wrong with mute? Maybe muting speaker is only defined for current user and gets rest to something for the next user


Does volume come back on at next boot?


I've look at your logoff script and have made some comments.



#! /bin/bash  

# running in root 

dateTime=$(date | tr ":" "-")  
echo "${0} Started at ${dateTime}" | cat >>runningStartLog.txt  

# running in root so we do not need sudo 
# for set Volume 0
# mute volume.  I assume you want next boot to start with mute volume. 
#    To be effective, you may have to issue some firmware command. 
# for tell application "Terminal"  
# This runs at logoff.  Terminal may be already closed.  What difference does it make anyway?
#    Terminal will eventually be gone.
ifconfig en0 down && sleep 1 \
  && osascript -e "set Volume 0" \
  && sleep 1 \
  && osascript -e 'tell application "Terminal" to close (every window whose namecontains".command")' & 
   
echo "${0} background scheduled " | cat >>runningStartLog.txt  
top -l 1 >>runningStartLog.txt  
ps aux >>runningStartLog.txt  
# see if adding sleep here does anything.  
sleep 10  
exit

Nov 8, 2015 5:51 PM in response to rccharles

I've repeatedly asked how my commands would look like if I want them to be run with a .sh extension.


And the commands aren't being executed when I add the file .command to the loogouthook thing.


This is what is currently in my .command file:

shutdown -h now

sudo nvram SystemAudioVolume=%80

 sudo ifconfig en0 down



I also don't know what arguments for shutdown command I should give in order for it to tell all programs to close, and then if some program prompts a window to confirm whether I want to close it ("Transmission" for example), it should wait 15 sec and close everything forcefully and then shutdown.


It's so frustrating that there's no help on apple forums...

Nov 8, 2015 7:28 PM in response to Vizakenjack

I've repeatedly asked how my commands would look like if I want them to be run with a .sh extension.


I post your complete script above. It doesn't make any difference what the extension is. The type of script is determined by the line:

#! /bin/bash


The ordering of commands doesn't make any sense.


# Shutdown now, means what it says. Shutdown happens immediately.

#you should not put any commands after the shutdown.

shutdown -h now


The commands should be in this order:

  1. sudo nvram SystemAudioVolume=%80
  2. sudo ifconfig en0 down
  3. shutdown -h now



I also don't know what arguments for shutdown command I should give in order for it to tell all programs to close, and then if some program prompts a window to confirm whether I want to close it ("Transmission" for example), it should wait 15 sec and close everything forcefully and then shutdown.


This is a new question. It is the first time you asked it.

Nov 9, 2015 2:20 PM in response to rccharles

It would be best to start a new post. Start with a post of what you wish to happen. Followed by what you what steps you have taken. Finally, what has failed to happen. anything to do with unix commands post here:

Mac OS X Technologies


I do not see a problem with shutdown.


man shutdown

"SIGTERM TO SIGKILL INTERVAL

Upon shutdown, all running processes are sent a SIGTERM followed by a SIGKILL. The SIGKILL will follow

the SIGTERM by an intentionally indeterminate period of time. Programs are expected to take only

enough time to flush all dirty data and exit. Developers are encouraged to file a bug with the OS ven-

dor, should they encounter an issue with this functionality."



I opened up three terminal windows. I set one to sleep 500. I then did a shutdown -h now. In about 15 seconds, my screen went black.


User uploaded file

Nov 9, 2015 7:31 PM in response to rccharles

This is what I want:

When I shutdown (apple menu -> shut down ...), I want the following commands to be executed automatically:

sudo nvram SystemAudioVolume=%80
sudo ifconfig en0 down
shutdown -h now

So that when I boot up, and login into my OS X profile, my ifconfig will be down on default.


I tried adding adding .command file containing the commands above to the loogout thing, but it didn't work. 0 effect. I don't know what's wrong.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Execute a .command file on logout?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.