freedev / macosx-script-boot-shutdown

This project is useful to execute a shell script during Mac OS X boot or shutdown.
Apache License 2.0
78 stars 23 forks source link

Script seems to be killed before shutdown function finishes #3

Closed tim-rohrer closed 6 years ago

tim-rohrer commented 6 years ago

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!

freedev commented 6 years ago

May be the user you're using to run the script hasn't the privileges to shut down the vm.

freedev commented 6 years ago

If you haven't done already have a look at this http://www.launchd.info/

tim-rohrer commented 6 years ago

Thanks.

The script process is owned by 0/root.

In /Library/LaunchDaemons, the plist:

<?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>net.itstechnical.VirtualServersStart</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Library/Scripts/VirtualServersStart.sh</string>
    </array>
    <key>KeepAlive</key>
    <false/>
    <key>RunAtLoad</key>
    <true/> 
    <key>StandardOutPath</key>
    <string>/var/log/itn_vmware.log</string>
    <key>StandardErrorPath</key>
    <string>/var/log/itn_vmware.log</string>
    <key>ExitTimeOut</key>
    <integer>600</integer>
</dict>
</plist>

I read where others have said the only reliable way to do a shutdown like this is to wrap the server's shutdown and reboot commands so we can control the process timing.

But, I'm assuming you have it reliably working?

freedev commented 6 years ago

I have tried to do the same thing with virtualbox and as far as I remember if the owner user wasn't my user I was unable to run the script correctly.

<key>UserName</key>
<string>user</string>
<key>GroupName</key>
<string>group</string>
tim-rohrer commented 6 years ago

Thanks for the taking the time to work with me on this.

The script, the plist and the VM processes had all been owned by root. The VM owner user is tim so I've added the keys you suggested to the plist. Unfortunately, I'm not searching for a solution to this error:

Could not find user/group associated with service: 0: Undefined error: 0 myuser/mygroup

I've tried both text and UID/GID versions of myuser and my group.

Not giving up yet.

====== UPDATE: I think I got it! And yes, it appears your memory is correct. Perhaps a note in your README is warranted? Without you bringing up the ownership issue, I don't think I would have ever figured it out.

And regarding the errors, turns out I had permission issues with the logs in /var/log/, and when I moved them to myuser/Library/Log, I was able to put it all together.

Thank you!

freedev commented 6 years ago

Wonderful. I'll update the README adding this suggestion for posterity. :) So can I close the issue?

tim-rohrer commented 6 years ago

Closed. Thank you again.

I'm still seeing my VMs die from time-to-time vmx | W115: Caught signal 15 but I'm pretty sure this is related to me logging into the host server, even though I don't bring up VMware, nor am I logged in to the account the owns the running VMs. Kind of strange, but something I need to figure out for reliability reasons :-)