Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Doctor Q

Administrator
Original poster
Staff member
Sep 19, 2002
40,248
8,796
Los Angeles
Suppose I create a shell script that does something useful. For example, it might make a backup copy of some important files. (The purpose of the script doesn't matter for my question.)

My question: In what ways I can invoke this shell script on demand? I know a number of methods, but I'd like to know if there are more.

--- Part 1: As a regular user ---

I can invoke my shell script as follows:

(1) Open a Terminal window and type the name of my shell script (assuming a suitable PATH and execute permission on the shell script).

To use my shell script from the Finder by double-clicking something, I have these choices:

(2) Name the shell script "something.command". Then, when I double click it, the Finder launches a Terminal window and executes the script.

(3) Write an AppleScript that uses a "do shell script" command to run the shell script, and save the AppleScript as a double-clickable application.

(4) Do the equivalent in another scripting language.

(5) Use a real programming language to write a program that invokes my shell script (or, I suppose, the script from #3 or #4).

Are there other ways?

--- Part 2: As superuser ---

Now let's add a complication. Suppose my shell script needs superuser (often called "root") privileges in order to do whatever it does. Now I have a slightly different set of chocies:

(6) In Terminal, I can use the "sudo" command (typing "sudo" in front of the shell script name) to run my shell script.

(7) In Terminal, I can use the "su" command to become superuser, and then run my shell script by typing its name.

(8) I can log into the Finder as "root", so that I will already be superuser. Will all of methods #1 thru #5 then work without further prompts?

(9) I can get the program named Skeleton Key and drag the file from any of methods #2 thru #5 into that application. It will prompt me for my root password and then execute the shell script. Is that correct? Are there other programs like Skeleton Key?

Are there other ways to invoke my shell script when it needs superuser privileges?
 
Get "chron" to do it for you. Set it as a chron task and have it execute however often you want it to. (I think this will work)
 
My question was only about "on demand" use of a shell script, but if we're going to include "automatic" ways too then yes, chron is one of them. I didn't check, but I assume that the "at" and "batch" commands can be used too.

Another "automatic" way is to make one of these executables a startup item (either in /Users/myname/Library/StartupItems to be executed when I log in or in /System/Library/StartupItems to be executed at system boot time). The startup items used at system boot time are always run as superuser.
 
cron

Originally posted by OutThere761
Get "chron" to do it for you. Set it as a chron task and have it execute however often you want it to. (I think this will work)
It's "cron", not "chron".
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.