Issue with shutdown command in script (MacOS High Sierra)


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
Old 1 Week Ago
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:
LMHmedchem (1 Week Ago)
# 9  
Old 1 Week Ago
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; 1 Week Ago at 02:35 PM..
This User Gave Thanks to johnprogrammer For This Post:
LMHmedchem (1 Week Ago)
# 10  
Old 1 Week Ago
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 1 Week Ago
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 Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Terminal running bash/rsync script does not close with exit (MacOS High SIerra) LMHmedchem Shell Programming and Scripting 12 2 Weeks Ago 07:09 AM
Issue with pwd for script run by double click on script (MacOS High SIerra) LMHmedchem Shell Programming and Scripting 4 2 Weeks Ago 05:17 AM
Linux SuSE SLES 8 error..unable to issue shutdown command amexboy UNIX for Beginners Questions & Answers 1 4 Weeks Ago 08:48 AM
If you run macOS High Sierra version 10.13.1, be sure to install today's update. Don Cragun OS X (Apple) 6 12-01-2017 01:05 PM
Capture power button press on MacOs High Sierra? xedge UNIX for Beginners Questions & Answers 0 10-12-2017 05:04 PM
Samba High Availability issue using luci (HA Manager) on CentOS sunnysthakur Red Hat 1 04-05-2016 05:45 AM
sed command issue in script oberoi1403 Shell Programming and Scripting 6 03-10-2015 01:05 AM
Exit script when shutdown or reboot command is given bartgrefte Shell Programming and Scripting 2 12-23-2013 05:58 AM
what would a script include to find CPU's %system time high and user time high? sushwey Shell Programming and Scripting 9 04-26-2012 11:44 AM
High Performance Linpack Compiling Issue JPJPJPJP High Performance Computing 0 12-31-2011 08:12 AM
cronjob Command for Shutdown babinlonston Red Hat 7 12-30-2011 04:26 AM
Shutdown command x_adm AIX 3 06-17-2010 12:24 PM
Problems using shutdown -u command mckorn99 OS X (Apple) 1 04-05-2010 10:11 AM
Script to force Oracle database shutdown when shutdown immediate does not work aixhp UNIX for Dummies Questions & Answers 7 03-11-2010 01:04 PM
shutdown command manoj.solaris AIX 3 06-25-2009 04:38 AM