Issue with shutdown command in script (MacOS High Sierra)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with shutdown command in script (MacOS High Sierra)
# 8  
Old 03-14-2019
Quote:
Originally Posted by Neo
You don't need to sudo, just run it from the admin account on the Mac and move on to other tasks.
Quote:

LMHmedchem wrote:

Some of my issue was how to go about running from the admin account. The account I am in is already administrator , but I get permission denied when I try to run shutdown from a script. To authorize shutdown, I can run the script as, sudo scriptname , or I can su and then call the script.The default root account is disabled on many Mac installations, so you are not supposed to be able to change the user to root without activating the root account first. Please let me know if I am not correct about any of this.
No, you are not really correct. I have 4 Macs which I work on extensively, and I do not use MacOS as you describe. When I login as "Neo", which is a user account with MacOS Admin rights, I do not use my "Neo" account to run scripts which need to run as root. I simply do this:

Code:
sudo -i

...then I am into a terminal window and if I need a cron file to run (for example) with superuser privs, I install a root crontab. I don't create a "Neo" crontab and then write a bunch of "goofy" (sorry, but that's the first word that comes to mind, it is not directed at you or your work) sudo wrappers around a "Neo crontab script" to force the "Neo" account do accomplish what I can easily do running the script as root in the root account.

As a side note, I think it was also you who, in a earlier post (LMHmedchem, but I might be mistaken as I've busy coding sorry and don't have time to go back and review your older posts, my bad) , was working "as an exercise" to figure out how to close the MacOS terminal window in a script when the shell exits, and I read (in reply to that post) a lot of people replying to help you "in your exercise" which was confusing to me because this is simply a " 10 second preferences" selection in the MacOS terminal config dialog box.

You seemingly are asking questions, "as exercises" to solve problems that do not exist and that's OK "as exercises" and I encourage you to do so in these forums and I encourage everyone to reply who has time to reply; but honestly, you seem to have the luxury of an abundance of free time on your hands if you need to create problems in order to solve them. That's OK too. However, let me be clear since you asked me:

I write a lot of small task-related scripts on MacOS and I work in a quite robust development environment on the Mac and my "main development box" is a 12 core Mac Pro, and if I need to run a script to do "root things", like a crontab script which needs root privs , I run this script as root, configured as the root user from the root account. Boom. Task done. Move on to the next task. I never run scripts as "Neo" wrapped in a lot of "sudo" code to accomplish tasks as the superuser which I can do as the root user directly. Sorry, but why would I waste my time doing that? I have a lot to do and never enough time to do it and if I worked 12 hours a day everyday, I would still not have enough time to so all the things I need to get done in development, so I need to get things done quickly and not turn 30 second tasks into days of exploring MacOS. Hey, that's just me and more power to you if you have a lot of free time to do these "turn a one minute routine task into days of Q&A" experience.

The same is true for the earlier post about "how to close a MacOS terminal window when the shell exits"... I have shells exiting in MacOS terminal windows all the time, and if I wanted or needed those terminal windows to close when the shell exits, I would configure the terminal preference to "close window on shell exit..."

MacOS is great and I would not develop code on any other platform. If you or anyone has the luxury of a lot of time to play around with MacOS and do things in a more "difficult" way than the "10 second routine way" then by all means, go for it; but for me (and you asked me directly), when I read your posts my "inner thoughts" are "wow, this guy has a lot more free time than I do because he spends days on tasks which I can do one minute" and you seem to enjoy it; so go for it!

I used to have free time like that many years ago. I'm kinda envious to be honest Smilie
This User Gave Thanks to Neo For This Post:
# 9  
Old 03-14-2019
Shutdown is a system-wide action, and requires root privileges in some UNIX systems (macOS included).

This means, if you want to have shutdown as an option in a script, you will need to give the script root privileges, sooner or later.

Assuming the script filename is "backupscript", you can run it like this:

Code:
sudo ./backupscript

If you place it in /usr/local/bin, you can run it like this:

Code:
 sudo backupscript

The following version of your script, is POSIX shell compliant, and it works with Bash too.

Code:
#!/bin/sh

# POSIX shell compliant.

# read input to shutdown after script, or not.
echo " "
printf "do you want to shutdown after the backup is finished? (y/n):  "
read -r shutdown_response


# ...
# backup stuff
# ...


# shutdown if asked to.
if [ "$shutdown_response" = "y" ] || [ "$shutdown_response" = "Y" ]
then

  shutdown -h now


else
  echo " "
  echo "exiting script"

  exit 0

fi

Now, if you want to double click on a script on your Desktop, you can do the following:

1. Place backupscript in /usr/local/bin

2. Create a script on your Desktop, named something like "runbackupscript", that contains:

Code:
#!/bin/sh
# POSIX shell compliant.

sudo backupscript

exit 0


Last edited by johnprogrammer; 03-14-2019 at 03:35 PM..
This User Gave Thanks to johnprogrammer For This Post:
# 10  
Old 03-15-2019
Quote:
Originally Posted by Neo
No, you are not really correct. I have 4 Macs which I work on extensively, and I do not use MacOS as you describe. When I login as "Neo", which is a user account with MacOS Admin rights, I do not use my "Neo" account to run scripts which need to run as root. I simply do this:
Code:
sudo -i

These scripts have always been run by double clicking on a launcher, shortcut, or alias. Command line arguments like sudo -i scriptname can generally be executed from a launcher or shortcut, but I don't know how to do that with an alias on MacOS.For myself, I can certainly run such a script from the terminal, but there are others who use this particular computer who wouldn't be very comfortable doing that. For that matter, I'm not sure how comfortable I would be having them fire up a terminal and start running commands under sudo. I have tried to make such scripts interactive so they just have to double click on the script and enter the information requested. What I was looking for was to be able to open the script by double click, enter the password, and then have the rest of the script run as root (shutdown was the first issue I ran into but there were others requiring root). I just wasn't sure how to do that. I suggested having a launcher script that would call the backup script as root, thus prompting for the password to execute the call. That didn't seem to generate any support, so I worked through my goofy code for calling a script function as root (I don't disagree with your assessment though it didn't really take very long). In his last post, johnprogrammer also suggested a launcher script, so maybe that is a good option to to this for users who aren't very good with a terminal.

Quote:
Originally Posted by Neo
...then I am into a terminal window and if I need a cron file to run (for example) with superuser privs, I install a root crontab. I don't create a "Neo" crontab and then write a bunch of "goofy" (sorry, but that's the first word that comes to mind, it is not directed at you or your work) sudo wrappers around a "Neo crontab script" to force the "Neo" account do accomplish what I can easily do running the script as root in the root account.
This is another important issue in that I also have such scripts that are scheduled to run unattended. In MacOS, I use launchd to do that by,

-> create an appropriate plist for the task
-> copy the task.plist file to /Library/LaunchDaemons
-> change owner with sudo chown root:wheel task.plist
-> change permissions with sudo chmod 600 task.plist
-> add the task to the launchd queue sudo launchctl load /Library/LaunchDaemons/task.plist

Is this the launchd equivalent of a root crontab, meaning does the called script run as root in this scenario? Does the called script file also have to be owned by root or be in a specific location? Does the plist file need to have root specified as the user?

Quote:
Originally Posted by Neo
As a side note, I think it was also you who, in a earlier post (LMHmedchem, but I might be mistaken as I've busy coding sorry and don't have time to go back and review your older posts, my bad) , was working "as an exercise" to figure out how to close the MacOS terminal window in a script when the shell exits, and I read (in reply to that post) a lot of people replying to help you "in your exercise" which was confusing to me because this is simply a " 10 second preferences" selection in the MacOS terminal config dialog box.
Well the simple answer there is that I didn't know that (about the terminal preferences). There are many tasks that are simple, or at least straightforward, after you know what to do. I have never seen a terminal that didn't close on the exit command and I have never seen that option in the terminal preferences before. None of the other posters in that thread seemed to know that off the top of their head either, thought I found it eventually. Most of the help I received in that thread was for the issue of the script not running in terminal on double click. This was also a minor issue but others seemed interested in why it wasn't happening when there was a proper shebang in place and the script was permissioned as executable.

Quote:
Originally Posted by Neo
If you or anyone has the luxury of a lot of time to play around with MacOS and do things in a more "difficult" way than the "10 second routine way" then by all means, go for it; but for me (and you asked me directly), when I read your posts my "inner thoughts" are "wow, this guy has a lot more free time than I do because he spends days on tasks which I can do one minute" and you seem to enjoy it; so go for it!
This is all well and good if you happen to know the 10 second solution. If you don't, then you have to learn it like everyone else. Just because it takes me a long time to do some things doesn't mean that I prefer it that way.

I would like to be clear again about how much I appreciate the effort made by the member of this group. There are a great many things that I can do now that were well out of reach not that long ago. Those acquired skills have been very beneficial to my research even if they are not directly in line with what I do most days. I am afraid that I am not able to make much of a contribution here myself in that there are not many questions I can provide a competent answer to. I do give advice on many other forums where I have greater expertise and experience since I think it is important for users to contribute where they can do so effectively.

LMHmedchem
# 11  
Old 03-15-2019
Some idea that flashed through my mind: If there are several (admin?) tasks that you'd like to offer to several users that have to be done in terminal: why not create a (restricted) shell with a small menu script offering the tasks? You could centralize common code into functions, run certain code snippets with elevated privileges, and so on...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Change Name of Bluetooth Device from Command Line in macOS

Mac Version 10.15.2 (macOS Catalina) Does anyone know how to change the name of a connected bluetooth device from the command line on macOS? I am having trouble with various bluetooth devices which I cannot get the "rename" option in the GUI to "save" properly and so I cannot rename a few... (0 Replies)
Discussion started by: Neo
0 Replies

2. Shell Programming and Scripting

Terminal running bash/rsync script does not close with exit (MacOS High SIerra)

Hello, I am running a bash script to do an rsync back on a computer running MacOS High Sierra. This is the script I am using, #!/bin/bash # main backup location, trailing slash included backup_loc="/Volumes/Archive_Volume/00_macos_backup/" # generic backup function function backup {... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

3. Shell Programming and Scripting

Issue with pwd for script run by double click on script (MacOS High SIerra)

Hello, I have the following script that just archives and clears some log files. #!/bin/bash # script: archive_logs_and_clear # add date to logfile names and copy archive directory # clear logs # change to script directory cd ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

4. UNIX for Beginners Questions & Answers

Linux SuSE SLES 8 error..unable to issue shutdown command

the only way we can power off is if we actually press power button on server. Running on HP DL-G4. from root, when we issue command it just returns to root prompt. (1 Reply)
Discussion started by: amexboy
1 Replies

5. OS X (Apple)

If you run macOS High Sierra version 10.13.1, be sure to install today's update.

Some hackers found a security hole in macOS High Sierra and tweeted it to the world before telling Apple about the problem. You can see the details from PC Magazine's daily news here: Apple Releases Fix for MacOS High Sierra 'Root' Bug. The original story this morning was published before a patch... (6 Replies)
Discussion started by: Don Cragun
6 Replies

6. UNIX for Beginners Questions & Answers

Capture power button press on MacOs High Sierra?

Hello everyone! I'm developing a MacOs Application in python and I'm having some issues trying to find information related to the power button pressed event. I know that in Ubuntu 14.04 you can find information about it on the acpi folders, but I realized that here in Mac that process is... (0 Replies)
Discussion started by: xedge
0 Replies

7. Shell Programming and Scripting

Exit script when shutdown or reboot command is given

This is probably a simple question, but I'm new with writing scripts for Linux (IPFire in this case) and Google wasn't helpful with this. When creating a script, what is the best and/or proper way to have it exit automatically if the reboot or shutdown command is given? If that's even... (2 Replies)
Discussion started by: bartgrefte
2 Replies

8. Shell Programming and Scripting

what would a script include to find CPU's %system time high and user time high?

Hi , I am trying to :wall: my head while scripting ..I am really new to this stuff , never did it before :( . how to find cpu's system high time and user time high in a script?? thanks , help would be appreciated ! :) (9 Replies)
Discussion started by: sushwey
9 Replies

9. High Performance Computing

High Performance Linpack Compiling Issue

I'm trying to compile Linpack on a Ubuntu cluster. I'm running MPI. I've modified the following values to fit my system TOPdir MPdir LAlib CC LINKER. When compiling I get the following error: (the error is at the end, the other errors in between are because I've ran the script several times so... (0 Replies)
Discussion started by: JPJPJPJP
0 Replies

10. UNIX for Dummies Questions & Answers

Script to force Oracle database shutdown when shutdown immediate does not work

I have Oracle 9i R2 on AIX 5.2. My Database is running in shared server mode (MTS). Sometimes when I shutdown the database it shutsdown cleanly in 4-5 mints and sometimes it takes good 15-20 minutes and then I get some ora-600 errors and only way to shutdown is by opening another session and... (7 Replies)
Discussion started by: aixhp
7 Replies
Login or Register to Ask a Question