0

Is there a way to prevent shutdown on macOS while a particular bash script is running without changing the actual shutdown command?

This question has some linux based solutions that would probably work by aliasing the shutdown command, but I am just wary of doing it this way. Call it a lack of knowledge combined with a desire to not alter more than I need to.

This question asks essentially what I am asking but the accepted (and only) answer pertains only to sleep.

Ideally a solution will provide some way to prevent shutdown from within the bash script that is running. If this is not possible, the answer that requires the fewest changes elsewhere will be the best one.

ben
  • 113
  • 6
  • 1
    It's ugly and horrendously bad practice, but you could look here and do something similar such as renaming the `/sbin/shutdown` command so it can't run... https://stackoverflow.com/a/24202568/2836621 – Mark Setchell Apr 05 '19 at 07:01
  • @MarkSetchell yeah this seems to be a sure way to do it, though the point you made about updates is one concern. Hoping there is a more elegant/robust way to do it but that may end up being the only solution. – ben Apr 05 '19 at 16:40

1 Answers1

2

The command you probably are looking for is this:

sudo systemsetup -setcomputersleep Never

And since we are on this topic, you can get the actual state using this command:

sudo systemsetup -getcomputersleep

I hope those help you.

EDIT: I just found this excellent and more complete article: How to customise the power settings from Terminal on the Mac.

accdias
  • 5,160
  • 3
  • 19
  • 31
  • Didn't know about pmset and that will probably be helpful in the future, but these approaches all seem geared towards preventing the computer from sleeping. I am trying to prevent/inhibit *shutdown*. Is there a pmset option I am missing? – ben Apr 05 '19 at 02:15
  • Sorry. I misread your post and I was under the impression you wanted to prevent the computer going into sleep mode. – accdias Apr 05 '19 at 02:29
  • No worries, I'll count knowing the existence of pmset a plus for sure. – ben Apr 05 '19 at 16:33