Using the the sudo command in a shell script

Lately it seems I have become bogged down with all the routine maintenance that is required for my system. It seems I am spending a considerable amount of time keeping things tidy. One of the things I wanted to update was the process of keeping macports updated. I am tired having to issue a command in terminal and wait several hours for the program to finish building, so I decided to create a simple shell script to hopefully automate the process.

File: macportsUpdate.sh


# This a simple bash script outlining the procedures to get macports on OS X
# to auto update weekly via the ~/Library/LaunchAgents/net.ipatch.macportsUpdate.plist
# The first step is to update the port files
echo "Step 1: Update port tree"
sudo port selfupdate
# The second step is to upgrade the outdated
echo "Step 2: Upgrade outdated"
sudo port upgrade outdated
# The third step is to remove inactive ports
echo "Step 3: Remove inactive ports"
sudo port uninstall inactive
# The fourth step is to clean the vile
echo "Step 4: Clean the vile"
sudo port clean --all vile


My question is how to get the script to run through the four steps without having me in front of the computer to enter a password after the several hours it takes to run the first step because terminal/shell prompts me for a password because it takes several minutes/hours to run the first step so the sudo password is required to run steps 2 -4.

PS sorry for my lack of formatting/markup, I thought this forum used UBB code for markup but apparently I am mistaken. Well, I found the formatting thread, here 😀

Macbook pro, Mac OS X (10.6.5), Santa Rosa

Posted on Mar 23, 2011 6:48 PM

Reply
9 replies

Mar 23, 2011 8:04 PM in response to chris.jones1


#!/usr/bin/env bash
# This a simple bash script outlining the procedures to get macports on OS X
# to auto update weekly via the ~/Library/LaunchAgents/net.ipatch.macportsUpdate.plist

# The first step is to update the port files
echo "Step 1: Update port tree"
port selfupdate

# The second step is to upgrade the outdated
echo "Step 2: Upgrade outdated"
port upgrade outdated

# The third step is to remove inactive ports
echo "Step 3: Remove inactive ports"
port uninstall inactive

# The fourth step is to clean the vile
echo "Step 4: Clean the vile"
port clean --all vile

Now invoke your script using sudo, as in:

sudo ./macportsUpdate.sh

This way you authentiate once, and from that point forward everything is run a 'root'

If you do not want to remember to prefix the script with sudo, then you could do the following:

#!/usr/bin/env bash
myPortUpdate()
{
# This a simple bash script outlining the procedures to get macports on OS X
# to auto update weekly via the ~/Library/LaunchAgents/net.ipatch.macportsUpdate.plist

# The first step is to update the port files
echo "Step 1: Update port tree"
port selfupdate

# The second step is to upgrade the outdated
echo "Step 2: Upgrade outdated"
port upgrade outdated

# The third step is to remove inactive ports
echo "Step 3: Remove inactive ports"
port uninstall inactive

# The fourth step is to clean the vile
echo "Step 4: Clean the vile"
port clean --all vile
}
# Now do all of the above with a single sudo command
sudo myPortUpdate

Now you can invoke the script and it will just prompt once for your admin password

macportsUpdate.sh


Message was edited by: BobHarris

Mar 24, 2011 1:28 PM in response to chris.jones1

chris.jones1 wrote:
Lately it seems I have become bogged down with all the routine maintenance that is required for my system. It seems I am spending a considerable amount of time keeping things tidy. One of the things I wanted to update was the process of keeping macports updated.


I thought MacPorts was supposed to be time-saver, not a time-waster.

I have never performed any routine maintenance on my system. It shouldn't be required at all.

Mar 24, 2011 4:09 PM in response to etresoft

etresoft wrote:
chris.jones1 wrote:
Lately it seems I have become bogged down with all the routine maintenance that is required for my system. It seems I am spending a considerable amount of time keeping things tidy. One of the things I wanted to update was the process of keeping macports updated.


I thought MacPorts was supposed to be time-saver, not a time-waster.

I have never performed any routine maintenance on my system. It shouldn't be required at all.


It's still a time saver, but it becomes a time waster when I have to upgrade several packages e.g. Java and GCC and it starts compiling from source. I would much rather macports perform these operations in the background, e.g. when I am sleeping 😀

Mar 24, 2011 5:28 PM in response to chris.jones1

chris.jones1 wrote:
It's still a time saver, but it becomes a time waster when I have to upgrade several packages e.g. Java and GCC and it starts compiling from source. I would much rather macports perform these operations in the background, e.g. when I am sleeping 😀


I'm not familiar with MacPorts so I can't say what its capabilities might be. I can tell you that if you weren't using MacPorts, you could easily create a launchd task that would re-build all of your software every night. You could write one script to build everything and then another script that installed using the sudo command. You can edit your sudoers file such that the install script can be run as root without requiring a password. Then, if your build succeeds, it will kick off the installer and maybe fire off an e-mail to you for good measure. That is all fairly easy with any OS. I've even done that in Windows. I don't know if MacPorts can do it. You will have to dig into the documentation and see.

Mar 26, 2011 5:39 PM in response to chris.jones1

So I solved the problem. At first I wanted to run the script via a cron job, but then soon realized Mac OS X, root, and cron jobs don't play well together. I also discovered the /etc/peridoic directory and the /etc/periodic/weekly directory. I placed my macportsUpdate.sh in the directory mentioned above, and I made sure the script was executable. I also made sure the script has proper owner and group permissions. Once I had all this setup I ran the command: sudo periodic weekly and all the commands were executed in the script. I also removed the sudo in front of the four commands, and everything seemed to work properly. The reason being that these scripts are run using a launchd process. I hope this helps if anyone was trying to automate a task of their own.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Using the the sudo command in a shell script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.