Automated Shell Scripts using Launchd

in #interactive7 years ago

Using Terminal commands to navigate and control your computer can be very useful. Running those commands together in a shell script can be even more useful, but executing it manually in my opinion seems to defeat the purpose. In my case I'm running OSX 10.11 which includes Launchd. It loads on boot and allows you to specify programs to execute, in my case a simple shell script.

There are inifinite practical applications for using this technique, in my experience I've found it useful for:

  • Getting diagnostics from a remote computer.
  • Making certain applications run on boot for easy installation startup.
  • Checking status of pjlink or other hardware.

In the following steps I'll discuss how to create a simple shell script and a launchd plist that will automate that shell script based on a specified time. I won't however get too in detail about the actual terminal commands, but if you're a terminal beginner I highly recommend running through the Code Academy tutorials.

Create Shell Script

  1. Open up Terminal and enter the following command to create a new file.

    touch testingShell

  2. Now enter the following command to open a built-in Terminal text-editor.

    nano testingShell

  3. You now should be viewing the nano text editor. This is where you will write all the lines of commands you want to execute in one single shell script file. Here is a simple example that when executed will write the date and your ip address into a text file. Write the following and once finished hit Ctrl + X to save.

`#!/bin/sh`

 `date >> ~/Desktop/testOutput.txt`

 `ifconfig en0 >> ~/Desktop/testOutput.txt`
  1. Now that you've created and saved the shell script which contains some terminal commands, you are ready to run the following command that will give that file the ability to execute.

    chmod u+x testingShell

  2. Finally, run this command to execute your shell script. Once you do you will notice that the testingShell script will create a text file called testOutput.txt (contained in the same directory as your shell script) with the date and time you ran that script, as well as the ip address of your computer.

`./testingShell`

Create Launchd Plist

  1. Just like we did with the shell script above, we now want to create a launchd plist which will give us the ability to execute our shell script routinely at a specified date and time. Go ahead and run this first command.

touch ~/Library/LaunchAgents/com.yourusername.testLaunch.plist

  1. You will notice that the last command created a file with the extension .plist in the LaunchAgents directory of your computer. It is crucial that you keep your plist in that folder for the computer to know to execute it. You will also notice that the file you just created has no content so run the next command.

nano ~/Library/LaunchAgents/com.yourusername.testLaunch.plist

  1. Once again we will use nano editor to insert content into our plist. I would recommend using another text editor like Sublime Text when doing this in the future, but for our purposes nano will do just fine. Now copy and paste this linked text into you com.yourusername.testLaunch.plist file. Once again, when finished hit Ctrl + X to save your file modification.

  2. Lastly, run the next three lines consecutively everytime you modify and save your plist file, otherwise it will fail to update.

launchctl unload com.yourusername.testLaunch.plist

launchctl load com.yourusername.testLaunch.plist

launchctl start com.yourusername.testLaunch.plist

  1. If all goes well you should see that at the specified time in the plist a text file with the date and time, as well as your computer's ip address will pop-up on your desktop! If you run into issues a helpful command is launchctl list Your plist should be listed and have a PID number. Usually a status means something went wrong but it will give you a number to search out what may be the problem.
Sort:  

Congratulations @nightshining! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You made your First Vote

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.21
TRX 0.26
JST 0.039
BTC 98927.45
ETH 3456.69
USDT 1.00
SBD 3.19