macosx-script-boot-shutdown icon indicating copy to clipboard operation
macosx-script-boot-shutdown copied to clipboard

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

Open christophecvr opened this issue 2 years ago • 13 comments

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.

christophecvr avatar Jan 08 '24 11:01 christophecvr

Sorry ignore the above with the : . (Looked it was the solution but that was not the cause). I well had to make a change on my Mac (macos high sierra last version).

When a change the start shutdwon script to :

#!/bin/bash

#
# Author: Vincenzo D'Amore [email protected]
# 20/11/2014
#

trap shutdown 9 15

function shutdown()
{
  echo `date` " " `whoami` " Received a signal to shutdown"

  /Users/christophe/scripts/WisLibreofficeHistory.sh

  exit 0
}

function startup()
{
  echo `date` " " `whoami` " Starting..."

  # INSERT HERE THE COMMAND YOU WANT EXECUTE AT STARTUP

  tail -f /dev/null &
  wait $!
}

startup:

It works great however According trap command macos manual

https://ss64.com/mac/trap.html

The trap command does not work for SIGKILL (9) but by me I guess (did not test it yet) it works on the 15 SIGTERM .

christophecvr avatar Jan 09 '24 09:01 christophecvr

Hi @christophecvr, if you can please do your test and let me know if everything works well.

freedev avatar Jan 09 '24 10:01 freedev

Hello,

Yes just tested and indeed on the SIGKILL signal (9) the trap command does not work. On SIGTERM (15) it does work perfect. For just reboot and shutdown. So indeed my script now is:

#!/bin/bash

#
# Author: Vincenzo D'Amore [email protected]
# 20/11/2014
#

trap shutdown 15

function shutdown()
{
  echo `date` " " `whoami` " Received a signal to shutdown"

  /Users/christophe/scripts/WisLibreofficeHistory.sh

  exit 0
}

function startup()
{
  echo `date` " " `whoami` " Starting..."

  # INSERT HERE THE COMMAND YOU WANT EXECUTE AT STARTUP

  tail -f /dev/null &
  wait $!
}
startup:

comment: I Think You could also use

trap shutdown SIGTERM

When time permits I will try it out.

christophecvr avatar Jan 09 '24 11:01 christophecvr

As extra info I also activated the log function and used the /private/var/log made a map BOOTSHUTDOWN/

There both log and error are located. and can be seen it works perfect. with 15 SIGTERM.

Tue Jan 9 12:17:12 CET 2024   root  Received a signal to shutdown
Removed /Users/christophe/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/org.libreoffice.script.sfl2
Tue Jan 9 12:17:30 CET 2024   root  Starting...

tes

christophecvr avatar Jan 09 '24 11:01 christophecvr

Hi @freedev , now all test completed. By me it just works perfect. Macbookpro mid 2010 with 16 gb ram I7 double core(4 threads) processor updated HD ssd samsung 1 TB . Maybe very old but still perfect working was hardware from the days Steve Jobs was still with us. Unfortunately max macos is High Sierra 10.13.6 . On the boot-shutdown-script.plist nothing needed to be changed Appart from location stuff like you mentioned. Installation and launching like you explained. (which is also the mac recommended way). The SIGKILLL indeed does not work for trap as far I understand SIGKILL will by macos always follow ON SIGTERM but bassically SIGTERM is issued to stop running deamons properly while SIGKILL will kill hard whiteout a proper way. Something like that ?? interesting topic about it : https://apple.stackexchange.com/questions/361574/is-there-a-way-to-prevent-sigkill-after-sigterm-for-a-daemon-agent-during-shutdo For the boot-shutdown.sh

Here my current perfect working version:

#!/bin/bash

#
# Author: Vincenzo D'Amore [email protected]
# 20/11/2014
#

trap shutdown SIGTERM

function shutdown()
{
  echo `date` " " `whoami` " Received a signal to shutdown"

  /Users/christophe/scripts/WisLibreofficeHistory.sh

  exit 0
}

function startup()
{
  echo `date` " " `whoami` " Starting..."

  # INSERT HERE THE COMMAND YOU WANT EXECUTE AT STARTUP

  tail -f /dev/null &
  wait $!
}

startup;

Logs:

Tue Jan 9 16:57:18 CET 2024   root  Starting...
Tue Jan 9 16:59:37 CET 2024   root  Received a signal to shutdown
2024-01-09 16:59:38 root Removed /Users/christophe/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/org.libreoffice.script.sfl2
Tue Jan 9 16:59:54 CET 2024   root  Starting...

So it works with SIGTERM or 15 may also be used. Thanks for this script it is very useful.

christophecvr avatar Jan 09 '24 16:01 christophecvr

Hallo @freedev I also created script files for macos who will do the same but then at user login and logout. So then it's limited to specific user. What is interesting for mac pc's who have several users. see:

mac-script-login-logout

christophecvr avatar Jan 13 '24 16:01 christophecvr

Hi @christophecvr, if I compare the original source with the only change I see is that you have removed the trap shutdown SIGKILL line. And, for what I understand it should be useless... so it is not clear what was wrong.

freedev avatar Jan 17 '24 08:01 freedev

Hi @freedev by me it did not worked at all in original version. There were 2 trap commands. on 2 lines.

I think that was the reason why it did not worked on my macos high sierra (possible it well worked on older osx)

trap shutdown SIGTERM
trap shutdown SIGKILL

By removing the SIGKILL line it does work well (but I well relocated the trap above the functions line do not now if that is needed but in a couple of program examples on other stack excange sites they do it always like that). But it works perfect. By me with the boot-shutdown like I published at the end.

christophecvr avatar Jan 17 '24 11:01 christophecvr

Ok, thanks for sharing it. Honestly I don't even remenber what version of osx was available when I wrote the script (and published the project). But at that time, for sure, it was working well :) I have adjusted the script boot-shutdown.sh by removing the trap shutdown SIGKILL. Right now I have Sonoma on my mac. This is perfect to give a try to this lastest version.

freedev avatar Jan 17 '24 12:01 freedev

@freedev Yes I'm limited to High Sierra with my mac. You can try it on a higher MacOs . I now made the script to work user based as a LaunchAgent at login logout. The logout gives a SIGTERM so it works perfect. The advantage is that If there are several users on a same pc They can use it just for the user. Also the commands are with user rights. No need for sudo during install and or launching .

christophecvr avatar Jan 17 '24 13:01 christophecvr

Hi @freedev If You have time can you once test the macos-script-login-logout in you're mac. Would be nice if I know that this work still on higher MacOs versions then High Sierra 10.13.6 . It's on my github.com. It does contain a full automated install.sh script which can Install remove and or reinstall. It can run as source script or bash subshell script. macos-script-login-logout

The master is just the basic script. The experimental just contains a extra that is for me personall to be rid of the libreoffice history on the LibreOffice dock picto.

christophecvr avatar Jan 17 '24 17:01 christophecvr

Hi @freedev I just double checked . At this time the boot shut down like it is here. Well it worked well now (must have done something wrong myself by the first install to the script) But its was a long time I worked with bash shell and especially on mac I worked before always on linux. I well did the test with the SIGKILL only and that indeed does not work. So actually it never worked according the manuals. trap command does not work on SIGKILL . The reason is I think pretty logic since when SIGKILL comes the pc is hard killing every remaining process right away normally when SIGKILL comes all processes should have been finished. SIGTERM comes actually at user logout which off course is the first thing which happens when you reboot or shutdown. So the second line: trap shutdown SIGKILL #may just be removed as it is not needed.

Logging of the process One with the both commands in. OK One with only SIGTERM in OK The last only SIGKILL NOT OK You can see that shutdown is not done at reboot.

Sat Jan 20 11:11:30 CET 2024   root  Starting...
Sat Jan 20 11:16:57 CET 2024   root  Received a signal to shutdown
2024-01-20 11:16:57  Removed /Users/christophe/Library/Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/org.libreoffice.script.sfl2
Sat Jan 20 11:17:15 CET 2024   root  Starting...
Sat Jan 20 11:22:18 CET 2024   root  Received a signal to shutdown
2024-01-20 11:22:18  No LibreOffice History file found
Sat Jan 20 11:24:55 CET 2024   root  Starting...
Sat Jan 20 11:27:58 CET 2024   root  Starting...

I'm now will try to make a good automated install file for this script also for boot shutdown like I did for the my login-logout-script . But bootshutdown is a system process not user . See if I can make one which needs to be run with sudo . Anyway this is a system process ideal for cleanup in system locations or whatever. While login logout is only for user processes. I will make this file in a forked git repo.

christophecvr avatar Jan 20 '24 10:01 christophecvr

Hi @freedev I just made a Automated (user install script for the macosx-script-boot-shutdown) . You can find it here :

github.com/christophecvr/macosx-script-boot-shutdown

christophecvr avatar Jan 23 '24 12:01 christophecvr