The Terminal.app application is actually a "terminal emulator," which allows you to use a "command-line interpreter" that uses the "bash" shell program to interact with your computer. There are other terminal emulators and there are other shell programs that you can use—I often use the iTerm application as my emulator, for example, although I always use the "bash" shell, which takes the commands I enter— ls, cd, etc.—and executes them.
Just as you can write Java or Python programs, you can also write shell programs—usually called shell scripts—that do all sorts of things. In some cases, those scripts can be written using a single line of code, with statements separated by semicolons (;).
Here's a fun, single-line shell script you can run (Apple OS X only)
For reference, here is a more extensive bash program that does the same thing you just did in a series of commands.
The cron utility is a daemon, a small program that runs without user intervention. This cron program is launched every minute that your computer is in operation, at which time it checks to see if there are, in turn, any programs that it is supposed to run. As a user, you can write small programs that you wish to run automatically on a given date or time, and create an entry in the cron table, or crontab interface.
Let's see how easy it is to do that.
I like to start my day reading the Penny Arcade comic. I can open by browser and type in the URL, and I might even make a bookmark in my browser for that page: https://www.penny-arcade.com/comic
Conveniently, I can also launch that webpage on my Apple OS X machine using the open command in the Terminal:
In Ubuntu (Debian Linux), one can launch the default terminal using the x-www-browser command:
Go ahead and try it!
Once we've confirmed that the command works, let's write a shell script that will run that line for us. This shell script, when executed, will issue the Terminal instruction that we just tried out above.
We're going to call the script penny_arcade_launcher.sh, and we're going to save it in an OS X directory called /usr/local/bin/, as shown below.
Create the following shell script using a text editor.
Here's what it looked like when I created that shell script using nano:
Confirm that your script works by opening up a Terminal and using the bash shell to run the script:
You should see the page launch from that script!
Now that we have a script, we can set up a cron job that will launch that script at a pre-specified time. If I want to read the comic when I start my day at 8am, I'm going to set up cron to run that script at 7:30 in the morning, Monday through Friday.
This launches your default text editor so that you can create an entry in your cron table. If you haven't yet set a text editor for your shell, you may be presented with something like this:
Once you've selected an editor, you can add a line to the crontab similar to the one below, which uses bash to run the script at 7:30 every day of the week, Monday through Friday:
The instruction to start the launcher using bash is the last entry. The five fields in front of that instruction indicate when the command should be run.
From the Wikipedia entry on cron:
The asterisk * indicates that the job should be run during every time period indicated. So, our launcher will run at 7:30 every day of the month, every month of the year, Monday through Friday.
In OS X, cron is now deprecated in favor of using launchd. Although cron is still supported, you may wish to consider creating a launchd plist instead.
See below for more information on setting that up.
cron has been deprecated on many systems in favor of a more powerful and versatile (and more complex) system called launchd for "launch daemon." This launch daemon is responsible for managing the running of files on your system in the same way that cron is, but configuring it takes a bit more work.
We're assuming you already have your penny_arcade_launcher.sh shell script set up and stored in /usr/local/bin/.
Here's an example that you can start from. You should call this file something like com.crashwhite.launchPennyArcade.plist.
For this launchd file to work, it needs to be placed in the appropriate directory. In OS X, user files are placed in ~/Library/LaunchAgents. (There are other places the file may run as well, but we're going to stick with this for now.)
You now need to tell launchd that you'd like this agent to be included with all the other things that launchd runs from time to time.
In the Terminal:
If you don't get any error messages, then your agent is being monitored and should be run at the time and day you've indicated.
Just as the script above took a screenshot every 30 seconds, you can write a script that takes a picture using the webcam every 30 seconds as well.
To do this you'll need to install ffmpeg onto your computer.
Then, write and run the following script: