Sponsored Content
Top Forums Shell Programming and Scripting Issue with shutdown command in script (MacOS High Sierra) Post 303032204 by LMHmedchem on Wednesday 13th of March 2019 05:38:51 PM
Old 03-13-2019
Thank you for taking the time to put together this informative post. I'm sure that others will read it as well.

Quote:
Originally Posted by bakunin
First off, i can only emphasize what Neo already said. This is the best solution from a practical POV. But since you may want to understand your system better i will try to explain what happened anyway:
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.
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.

One thing I was trying to accomplish was to have this all run without having to launch a terminal, navigate to the script location, and run the script. I can do that, but there are others who may use this machine who cannot. I wanted to set it up so that an Alias to the script could be double clicked on, the script launched in Terminal, and the user prompted for their password.

Quote:
Originally Posted by bakunin
Now, everybody can - in principle - use the command su to switch the user. What su does is to open a subshell (you are correct in your assumption that it does) with the new rights and you are that new user as long as you do not leave this shell again. Notice that there are two (three) ways to run su:

Code:
su newuser
su - newuser
su - newuser -c command

Let us first talk about the difference between the first and the second: without the dash ("-") in between the environment of your own user is preserved. Variables like "PATH" and everything else you have set remains even though the user credentials change. With the dash, though, your environment is completely overwritten by the environment of the new user, effectively it is like doing a login as the new user - complete with executing the profile and whatever else is done during a normal login. In both cases, once you leave the subshell this is reverted back because all the changes are made within that subshell.

The last variant (which i have written only once but in fact it can be done in the with-dash- or the without-dash-fashion) does the same but within the resulting subshell only the specified command is executed and the subshell is left immediately afterwards. This way you can do single commands*) as a different user.
Apparently there is an issue in MacOS when you just try su. If you just do su you get an error even though you have entered your administrator password. If you do su root and enter your password, everything works. This is odd since the root account is supposedly not active.

Quote:
Originally Posted by bakunin
Finally, how should you do the backup: you should run the whole backup process as user root because this way you avoid the possibility that your user may not be allowed to access some data and hence cannot backup them.
This is an important point, especially in that the version of rsync that ship with MacOS is fairly old and seems to have some issues with metadata. If you run rsync with -E (preserve file execution permissions) and don't run as root, you get a whole long list of errors in your log file. If you are backing up your entire profile directory, there are allot of hidden system files there that will be skipped if you don't run as root.

Quote:
Originally Posted by bakunin
When you do it like this you can shutdown at the end or not without any additional password. Furthermore, instead of asking interactively, i would use an option to select an optional shutdown at the end:
This again goes back to not wanting to have to call the script from a Terminal. There are always choices to be made and so I guess there is no perfect solution.

This is one method I found that works. This is a little script that adds a username to /etc/sudoers and sets the user to be able to run sudo rsync without a password. You double click on the script, or script alias, enter the username, and enter your password. The proper entry is added to the sudoers list.
Code:
#!/bin/bash

# check if user exists in /etc/sudoers
# if not, add user and recheck
function add_user_and_check () {
   # check if the user entry already exists
   if sudo grep -q $user_name" ALL= NOPASSWD:/usr/bin/rsync" /etc/sudoers; then
      echo $user_name "already has the correct privileges"
   else
   # add the user
   sudo bash -c "echo \"$user_name ALL= NOPASSWD:/usr/bin/rsync\" >> /etc/sudoers"
      # recheck
      if sudo grep -q $user_name" ALL= NOPASSWD:/usr/bin/rsync" /etc/sudoers; then
         echo "success: the user was given rsync sudo privileges
      else
          echo "failure: the user was NOT given rsync sudo privileges
      fi
   fi
}

# get the username to add
echo "enter the username you want to add"
echo ""
read user_name

# get the password and call the function with sudo
echo "enter the administrator password"
echo ""
# call the function in a subshell as root
sudo su root -c "$(declare -f add_user_and_check)"; add_user_and_check
echo ""
echo "key \"Enter\" to exit script"
read exit_now
sleep 1
exit

My understanding is that because the function is declared and then called as root to run in a subshell, the instructions in the function can all run under root privileges. It is necessary to call the function instructions like grep and echo with sudo or you still get permission denied errors. My guess it that you could run the backup script as root using the same method, namely having all of the backup code in a function that is declared and called in the same way. You would start the script by double clicking on the script and then enter your password. The script function would then run as root. I don't think your function call other functions using this method, which is a drawback if true.

I opted for now to add the user to the sudoers list with $user_name ALL= NOPASSWD:/usr/bin/rsync so that rsync can run as root without a password. This has worked in tests so far in my tests.

LMHmedchem
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 07:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy