Coder Social home page Coder Social logo

macosx-script-boot-shutdown's Introduction

macosx-script-boot-shutdown

This configuration is useful when you need to execute a shell script at Mac OS X boot or shutdown.

There are two files:

  • boot-shutdown-script.plist
  • boot-shutdown.sh: bash shell script

# Install

You should customize the following placeholders inside the file boot-shutdown-script.plist:

  • BOOT.SHUTDOWN.SERVICE : If you don't know, leave it as is
  • SCRIPT_PATH : the path where the boot-shutdown.sh script is stored.
  • LOG_PATH : the path where the logs are stored. If you don't care about logs, use: /tmp

Then copy the file boot-shutdown-script.plist into /Library/LaunchDaemons/

sudo cp boot-shutdown-script.plist  /Library/LaunchDaemons/

When you want start the virtual machine type the following command:

sudo launchctl load -w /Library/LaunchDaemons/boot-shutdown-script.plist

If you want stop the script (and the you should type the following command:

sudo launchctl unload -w /Library/LaunchDaemons/boot-shutdown-script.plist

The script will be started machine at every host boot. Set into boot-shutdown-script.plist the field RunAtLoad to false if you don't want that the script starts automatically every time your mac os x boots. When RunAtLoad is set to false, after the load you must start and stop the script using the following command:

sudo launchctl start BOOT.SHUTDOWN.SERVICE
sudo launchctl stop BOOT.SHUTDOWN.SERVICE

Useful links

macosx-script-boot-shutdown's People

Contributors

freedev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

macosx-script-boot-shutdown's Issues

Script does not execute until user interactive session ends

I realize this script is 8 years old and probably unmaintained but thought I would leave a comment via this issue anyway for anyone else that has a similar issue.

I am trying to issue "change inputs" commands via DDC using a commandline utility on shutdown. Because the SIGTERM causes the user interactive session to end immediately, the DDC commands can't be sent to the "active" monitors and my script fails.

I tested the same script manually and using "shutdown -h now" and the process works correctly. Not really a bug in the script - just something to be aware of. You can't run a user interactive shutdown script either (I tried to just plug in a "Wait for Keypress" step in the bash script. It never happens in the user interactive space and hangs until the SIGTERM timeout happens.

Alternative was to use Automator to create a Service with the DDC commands as well as a "shutdown" command and then map a new unique keypress to both switch and shutdown. Working for me now.

regular CPU spike

Hi,

thanks a lot for the script. I use it :)

Buuut I noticed (and hunted down) regular CPU spikes caused by fseventd (and mdstore).
Turns out it was the "endless loop" in your script using tail on /dev/null that caused them.

I replaced the tail... and the wait $! with sleep 36500d and thats much better.
Granted: If I won't reboot my laptop in 100 years I have a problem. But I can live with that ;)

Voilà. I thought, maybe you wanna know :)

Thanks again anyways!!

PS: I picked the solution from here:
https://superuser.com/questions/681954/is-there-a-command-in-linux-which-waits-till-it-will-be-terminated
I was sure that sleep will be efficient.... maybe read is the same... did not try this one.

Works great with a little change in boot-shutdownscript.sh

Hi by me at first the shutdown commands did not execute startup well. Im working with a macbookpro mid 2010 . High Sierra 10.13.6 (which is max os installable on my mac).

I just changed (think even corrected) the trap command from

trap shutdown SIGTERM
trap shutdown SIGKILL
TO
trap shutdown: SIGTERM
trap shutdown: SIGKILL

The ":" was I think just forgotten but it's needed for a function launch.

Doesn't work in High Sierra

I can't run a shutdown command in the script. I tried:

echo "testing123" > /tmp/test.txt

to no avail. Does this script still work in High Sierra or Mojave?

Priorities with .zshrc

My script is related to the function defined in .zshrc.
So I want to know which comes first.
If the script comes first before system read .zshrc, then my scripts of course will false.
If the script comes later before system clean function in .zshrc , then my scripts of course will false.

I prepare to try it and post the results later.

An automation setup example

Just sharing example script to automate shutdown setup without original files modification (working with copies), might be appended to readme and be helpful

#!/bin/bash

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
EXECUTABLE_COMMAND="killall Simulator"

#setup paths

REPO_DIR_NAME="macosx-script-boot-shutdown"
REPO_PLIST_NAME="boot-shutdown-script.plist"
REPO_SCRIPT_NAME="boot-shutdown.sh"

REPO_WORKING_DIRECTORY="$SCRIPT_DIR/$REPO_DIR_NAME"
REPO_LOG_PATH=$SCRIPT_DIR

REPO_CUSTOM_PLIST_PATH="$SCRIPT_DIR/$REPO_PLIST_NAME"
REPO_CUSTOM_SCRIPT_PATH="$SCRIPT_DIR/$REPO_SCRIPT_NAME"

#create plist & script copy

cp "$REPO_WORKING_DIRECTORY/$REPO_PLIST_NAME" $REPO_CUSTOM_PLIST_PATH
cp "$REPO_WORKING_DIRECTORY/$REPO_SCRIPT_NAME" $REPO_CUSTOM_SCRIPT_PATH

#replace vars in custom plist

sed -i '' "s#SCRIPT_PATH#$REPO_WORKING_DIRECTORY#g" "$REPO_CUSTOM_PLIST_PATH"
sed -i '' "s#LOG_PATH#$REPO_LOG_PATH#g" "$REPO_CUSTOM_PLIST_PATH"
sed -i '' "s#.*AT SHUTDOWN.*#$EXECUTABLE_COMMAND#g" "$REPO_CUSTOM_SCRIPT_PATH"

#setup launch daemon

sudo cp $REPO_PLIST_NAME  /Library/LaunchDaemons/
sudo launchctl load -w "/Library/LaunchDaemons/$REPO_PLIST_NAME"

Error: Unable to commit changes to network database

Dear Sir,

I've been using your scripts successfully and I find it extremely useful for my use. I thank you for your job very much. However, I'd like raise what seem to be a small issue written in the log file:

Wed Jan 6 11:11:49 GMT 2016   root  Received a signal to shutdown
** Error: Unable to commit changes to network database.
** Error: Unable to commit changes to network database.
** Error: Unable to commit changes to network database.
** Error: Unable to commit changes to network database.
** Error: Unable to commit changes to network database.
** Error: Unable to commit changes to network database.

Although the script seems to perform my desired action well at shutdown time, above statements appear in the log file.

What do you think?

Thank you for your help.

Faxopita

Not starting at boot

Hi there.

I'm receiving this message in system.log when I boot.

Oct 15 19:33:19 Turing com.apple.xpc.launchd[1] (me.stefanotranquillini.shutdown[541]): Service could not initialize: 18G95: xpcproxy + 11313 [1534][4B485AA1-F3FF-36A4-86B9-6109F5CDE94F]: 0xd
Oct 15 19:33:19 Turing com.apple.xpc.launchd[1] (me.stefanotranquillini.shutdown[541]): Service exited with abnormal code: 78

the plist me.stefanotranquillini.shutdown
is the following


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
   <dict>
   <key>Label</key>
   <string>me.stefanotranquillini.shutdown</string>

   <key>ProgramArguments</key>
   <array>
     <string>/Users/stefano/.shutdown</string>
   </array>

   <key>RunAtLoad</key>
   <true/>

   <key>StandardOutPath</key>
   <string>/var/log/boot-shutdown.log</string>

   <key>StandardErrorPath</key>
   <string>/var/log/boot-shutdown.err</string>

   </dict>
</plist>

at shutdown, it works. the script runs in the shell works.
my script actually calls a python script, can be that?

PS: is it possible to trigger a script at reboot?

Script seems to be killed before shutdown function finishes

Thanks for putting up this repo.

Trying to use it to control my VMware VM during boot up/shutdown. Boot works, shutdown doesn't.

My goal it to suspend the VM during the shutdown or reboot process.

function shutdown()
{
  echo `date` " " `whoami` " Received a signal to shutdown"
  /Applications/VMware\ Fusion.app/Contents/Library/vmrun -T fusion suspend "/Volumes/VirtualMachines/Production/Ubuntu 64-bit Server 18.04.vmwarevm/Ubuntu 64-bit Server 18.04.vmx"
  echo "VM suspend command finished. Exit code: $?"
  exit 0
}

Nothing too complicated here. Doesn't really change your function. From the .plist, I'm logging to /var/log/vmware.log. In that, here is what the code above produces:

Thu Jun 7 15:42:01 PDT 2018 root Received a signal to shutdown
Killed: 9

If I copy & paste the vmrun command and use sudo to run it, the VM is properly suspended and documented in the machine's log.

I tried to follow you link to the Apple documentation but they've changed it so the link was no good. From my read of the man page on shutdown, the process still sends out a SIGTERM and SIGKILL, both of which you script traps. So, I'm not sure what else could have changed.

Or, am I missing something simple?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.