cron

Latest

  • Terminal Tips: bash cron script to keep an app running

    by 
    TJ Luoma
    TJ Luoma
    10.21.2010

    Do you have some apps that you want to keep running all the time? If so, and if you're not afraid of the Terminal or the command line, I have a script for you. When I come home at the end of the day, Dropbox has stopped running on my iMac. I'm running the latest version, and it works fine on my MacBook Pro, but for some reason, this just keeps happening. The script has nothing to do with Dropbox itself; you could substitute any app that you always want running, such as LaunchBar, OmniFocus, 1Password, or any other app that you like. It's fairly simple: #!/bin/sh PATH=/bin:/usr/bin # Change 'Dropbox' to whatever app you want. Be sure to capitalize # it correctly and include any spaces. You do not need to add .app APPNAME="Dropbox" # if the app name _IS_ found in process list, exit ps xc|fgrep "${APPNAME}" >/dev/null && exit 0 # if the app isn't found, open it open -a "${APPNAME}" exit 0 That's it. Now, you save the file (I call mine "keep-my-app-running.sh"); I saved it to ~/bin/, but you can put it anywhere you want. Be sure to type 'chmod +x /Users/luomat/bin/keep-my-app-running.sh' (or wherever it is saved) to tell OS X it is an eXecutable file. (Thanks to Justin for reminding me about this in the comments below.) Now, we need to tell cron to run it. Some folks will tell you to use launchd, but cron works well and it's easy, so we'll use that. To do that, create a ~/.crontab file using your favorite text editor. If it already exists, just keep whatever's there, and add this line at the bottom: */5 * * * * /Users/luomat/bin/keep-my-app-running.sh Change "luomat" to whatever your login name is, and change "keep-my-app-running.sh" to whatever you named the script. This tells cron to check if your app is running every 5 minutes or so. You can change the 5 to something else if you want to change the frequency. The last step is to tell cron to load the new file you've created: crontab ~/.crontab If you want to verify that it worked, run 'crontab -l' to see if your crontab is listed properly. It may also be a good idea to run 'crontab -l' before you begin in order to make sure that there isn't anything already in there. Most likely there isn't, or if there is, you already know about it. Update: As noted in the comments, cron works fine, but launchd can be configured to relaunch Dropbox as soon as it exits. I've enclosed a picture of a Lingon screenshot below, or you can see the plist that it creates. Lingon is no longer developed, but it works fine for me under Snow Leopard. I tried to use launchd to run a script at 0, 15, 30, and 45 minutes past the hour, which I can do in cron using this: */15 * * * * /path/to/script.sh but launchd didn't keep that schedule (for example, it posted at 11:48 and 12:03). So I decided to keep using cron for that, although launchd is a much better option for the 'keep alive' purpose. %Gallery-105694%

  • Manually schedule Software Update 'the OS X way' with launchd

    by 
    Brett Terpstra
    Brett Terpstra
    01.10.2010

    In response to a Macworld article, TidBits' Chris Pepper elaborated on ways to run Software Update, Apple's means of delivering updates and patches, on your own schedule. Beginning with the fact that Software Update schedules its next update based on the time it's currently being run, setting the time for the next update is as easy as running it manually at the time you want it to be scheduled for in the future. Later, Pepper delves into the command line method of updating, using the softwareupdate tool (which we've talked about on TUAW, too) to run it from Terminal. Taking that a step further, it's suggested that you run the command from cron, a UNIX command for scheduling tasks, to automate the command-line updates. However, while it still works fine and is perfectly capable of the task, cron has technically been deprecated in OS X since Tiger. I thought I'd mention the newfangled "Mac OS X way" of handling scheduled tasks, and demonstrate a little of its flexibility. Launchd is Apple's replacement for several UNIX ways of doing things, including init, rc.d scripts and cron. It provides a uniform, XML configuration method and -- in many cases -- is more secure than the replaced methods. Launchd can trigger applications and scripts at boot time, at intervals or even when a file or the contents of a folder change. It can also make sure a daemon or an application keeps running, with the ability to respawn and throttle it. If that's just a bunch of nerd-speak to you, don't worry, this isn't going to be an overly technical post. You can read more specifics about launchd on Apple's developer site, if you want more geeky goodness.

  • Friday Favorite: MainMenu 2 keeps your Mac running smoothly

    by 
    Steve Sande
    Steve Sande
    07.17.2009

    I'm a sucker for Mac maintenance utilities.That's not to say that I run them on a regular basis like I should do, but whenever I find a new one I like to give it a try and see how it's going to work for me. Dare To Be Creative Ltd. recently released version 2.0 of MainMenu, a collection of Mac utilities that resides in your menu bar.The US$10 application takes up very little real estate in your menu bar, displaying a small rounded square icon with a plus sign in the center (you can choose other icons as well). Clicking the icon unveils the menu seen at right.Each of the clearly identified "buttons" leads to a submenu of functions designed to clean up or optimize some area of your Mac. Under the System submenu, for example, you can repair disk permissions (usually done with Disk Utility), run the daily, weekly, and monthly cron scripts for cleaning up log files, clean caches, rebuild the Launch Services database and the Spotlight Index, and update prebindings (not really necessary since OS X 10.4) and the Whatis and Locate databases. You can also create your own batch files to run a number of the tasks at the same time, restart your Wi-Fi and flush your DNS cache, perform many user-related tasks, and more. When tasks complete, you get Growl notification. MainMenu 2 is my Friday favorite because it puts a lot of maintenance mojo a click or two away; there's no need to use the CLI or dig into the Utilities folder, and yes, I am a very lazy person. What's your favorite Mac utility? Leave a comment!