Results 1 to 10 of 10
  1. #1


    Member Since
    Sep 08, 2017
    Posts
    5
    MacOS shutdown hook
    Trying to find a way to run a shutdown hook on mac from a command-line background process.

    Tried using NSWorkspaceWillPowerOffNotification, which was never called. Also tried kIOMessageSystemWillPowerOff event and that did not run either.

    Anyone has any idea?

  2. #2

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    9,752
    Specs:
    27" i7 iMac, 15" Macbook Pro TB, 13" Macbook Air, iPhone 6S, iPod Nano 7th Gen
    You haven't provided full context of where you are looking to execute the shutdown, but like all Unix system the command below will shutdown the Mac..

    Code:
    sudo shutdown -h now
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  3. #3


    Member Since
    Sep 08, 2017
    Posts
    5
    I am running this on mac, but the idea is not to shut down but
    Quote Originally Posted by Raz0rEdge View Post
    You haven't provided full context of where you are looking to execute the shutdown, but like all Unix system the command below will shutdown the Mac..

    Code:
    sudo shutdown -h now
    I am running this on mac, but the idea is not to shut down but to be able to run logic upon shutdown

  4. #4

    pigoo3's Avatar
    Member Since
    May 20, 2008
    Location
    U.S.
    Posts
    38,540
    Specs:
    2011 13" MBP 2.3ghz, 8gig ram, OS 10.8.5
    Quote Originally Posted by YanivH View Post
    I am running this on mac, but the idea is not to shut down but to be able to run logic upon shutdown
    This is a bit unclear (at least to me). Are you saying that you want your Mac computer to do this series of events:

    First = Start the shutdown program.
    Second = run Logic
    Third = Shut down computer

    If this is not correct...please clarify.

    - Nick
    - Too many "beachballs", read this: Beachballs
    - Computer seems slower than it used to? Read this for some slow computer tips: Speedup
    - Almost full hard drive? Some solutions. Out of Space
    - Apple Battery Info. Battery

  5. #5


    Member Since
    Sep 08, 2017
    Posts
    5
    That is correct.
    To be more presise, I want to log each shut down time to a log file

  6. #6

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    9,752
    Specs:
    27" i7 iMac, 15" Macbook Pro TB, 13" Macbook Air, iPhone 6S, iPod Nano 7th Gen
    OK, I see what you are trying..you said background process, if that is a shell script, one possibility is to trap the script being killed and then run a function where you logic resides. However, this is not a deterministic way of handling the case, i.e., the system is killing all running scripts and expects that to happen quickly and you can't too long or delay the shutdown, so it's possible that the computer will go down before you finish your "logic"..

    Checkout this handy script to see how it's done..
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  7. #7


    Member Since
    Sep 08, 2017
    Posts
    5
    Quote Originally Posted by YanivH View Post
    Trying to find a way to run a shutdown hook on mac from a command-line background process.

    Tried using NSWorkspaceWillPowerOffNotification, which was never called. Also tried kIOMessageSystemWillPowerOff event and that did not run either.

    Anyone has any idea?
    Quote Originally Posted by Raz0rEdge View Post
    OK, I see what you are trying..you said background process, if that is a shell script, one possibility is to trap the script being killed and then run a function where you logic resides. However, this is not a deterministic way of handling the case, i.e., the system is killing all running scripts and expects that to happen quickly and you can't too long or delay the shutdown, so it's possible that the computer will go down before you finish your "logic"..

    Checkout this handy script to see how it's done..
    Yeah I tried that..
    The only down fall to this is the need to run sudo..
    Is there are a way to run this on command line?
    launchctl load will do the trick?

  8. #8

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    9,752
    Specs:
    27" i7 iMac, 15" Macbook Pro TB, 13" Macbook Air, iPhone 6S, iPod Nano 7th Gen
    You can modify the script to have the startup() function just to the wait since you want the script to hang around. With that, you only need the sudo for Launchctl once to add it to to the list of scripts/functions/applications that will be loaded for each user automatically. When the script is killed, it will not need nor get sudo access, you will want to use a log file location that is accessible to that user.
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  9. #9


    Member Since
    Sep 08, 2017
    Posts
    5
    Quote Originally Posted by Raz0rEdge View Post
    You can modify the script to have the startup() function just to the wait since you want the script to hang around. With that, you only need the sudo for Launchctl once to add it to to the list of scripts/functions/applications that will be loaded for each user automatically. When the script is killed, it will not need nor get sudo access, you will want to use a log file location that is accessible to that user.
    But I will still to run one sudo

    Not sure I also understood what to do with the startup(), according to what you said..

  10. #10

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    9,752
    Specs:
    27" i7 iMac, 15" Macbook Pro TB, 13" Macbook Air, iPhone 6S, iPod Nano 7th Gen
    Yes, you will need Sudo to add to the list of things to run on user login..not sure there's a way around that..
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. iMac shutdown causes emergency shutdown of UPS
    By Lastmboy in forum Other Hardware and Peripherals
    Replies: 6
    Last Post: 09-17-2012, 09:33 PM
  2. TV hook up help
    By monkeytrot in forum Apple Notebooks
    Replies: 1
    Last Post: 01-27-2012, 08:14 AM
  3. Sudden Shutdown without warning, previous shutdown code: -2
    By scottttocsscott in forum Apple Notebooks
    Replies: 3
    Last Post: 04-24-2010, 04:48 AM
  4. hook up to HD tv?
    By jacintosh in forum Switcher Hangout
    Replies: 9
    Last Post: 02-07-2008, 04:07 PM
  5. Closing Hook
    By sinoeth in forum Apple Desktops
    Replies: 1
    Last Post: 02-12-2006, 11:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •