Skip navigation

Running script automatically at startup at system level

1010 Views 12 Replies Latest reply: Nov 7, 2012 3:26 PM by strawberrycheesecake RSS
strawberrycheesecake Calculating status...
Currently Being Moderated
Nov 1, 2012 4:39 PM

Hi all,

 

I am a newbie with MAC. I am looking for some application that is equivalent to Window's Task Scheduler - bascially, I have a bash script and I would like it to start running at startup and be always running in the background, no matter who logs into the computer, and whoever logged in can restrat the script if needed.

 

Background info (if that helps understand what I am trying ot accomplish):

I am using Selenium (a UI automation tool) to automate some web testing, and my script will be calling the Selenium library to start a server to listen for requests, and it will launch browser and run tests when a request comes in.

 

Long story short about what I want:

I would like to automate opening up a terminal and running a bash script when my MAC starts up, and have the script always running in the background regardless of which user is currently logged in, and I would like the user to be able to stop the script and restart it if it somehow gets stuck.

 

 

I did some quick research and found out about Automator. I would like to find out/confirm if Automator is what and all I need? If not, what are my options out there?

 

Any suggestion and advice would be greatly appreciated! Thank you so much for reading!

Mac mini, OS X Mountain Lion (10.8.2)
  • Frank Caggiano Level 7 Level 7 (22,830 points)

    Automator isn;t going to do it, you need to look at launch daemon

     

    Question like this are best asked in the Mac OS X Technologies community. I'll see if a host will move  this.

  • Frank Caggiano Level 7 Level 7 (22,830 points)

    Well Automator automates tasks but says nothing about how the task will be run. You can create an app, or a folder action, or a service, etc in Automator but that woln;t address the issue of when the code runs.

     

    As you're looking to have something start running at system boot time (I assume that's what you mean by startup in your post above also as you want it available to all users this is really your only option) then you are looking to create what in Unix speak is termed a daemon, something that runs in the background and offers services.

     

    The way to start a daemon running at boot time in OS X is via the Launch Daemon. How comfortale are you with the terminal?  Start by looking at the man page for launchd (the name of the Launch Daemon).

     

    Hopefully others will chime in here, my launchd skills are limited.

     

    regards

  • twtwtw Calculating status...

    I'm not sure whether this should be a LaunchDaemon or a LaunchAgent - if it requires user interaction as you sugest it should really be the latter, so that's what I'll show how to do.

     

    Open TextEdit, make sure the open document is set to plain text (not rich text), copy in the following text, then save it as user.bash_script.autostart in the folder /Library/LaunchAgents/.  Then restart the machine or load the plist using launchctl in terminal.

     

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

    <plist version="1.0">

    <dict>

              <key>RunAtLoad</key>

              <true/>

              <key>Label</key>

              <string>user.bash_script.autostart</string>

              <key>Program</key>

              <string>/path/to/bash script</string>

              <key>KeepAlive>

              <true/>

    </dict>

    </plist>

     

    This job will start the script as a daemon so that it's running for all users.  The KeepAlive key specifies that if the daemon quits launchd should automatically restart it.  It's up to you to make sure users have the correct permissions to quit the process if it gets stuck.

     

    Note: using a LaunchAgent means that a separate instance of this process will be created for each user who logs in, and that the process will not be running if no user is logged in.  if you need to have one running instance for all users you need to use a LaunchDaemon.  However, LaunchDaemons are not supposed to have user interaction, which it seems like you want.  more details would be helpful.

  • twtwtw Level 5 Level 5 (4,580 points)

    First, pet peeve:  It's Mac, not MACMAC stands for Media Access Control, a way of identifying your computer on a low level.  Mac is short for Macintosh, a kind of apple.

     

    You're in a gray area.  LaunchDaemons can't have any GUI access themselves, but I don't think there's anything stopping them from invoking a GUI app.  however, Safari is only avaliable in user contexts, not at the root level, so if your script tries to invoke Safari when no users are logged in, I have no idea what will happen.  This really sounds more like a LaunchAgent than a LaunchDaemon to me, since (I'm assuming) you'd want to call Safari in a particular user context, not in every user context.

  • Frank Caggiano Level 7 Level 7 (22,830 points)

    I'll let twtwtw take the lead on this as I said my lauchd experience is limited.

     

    But I do have a question. You wrote that you want the script running even when no one is logged in. Why is that? Will the testing be going on even when no one is logged in? I thought from your previous post that some user interaction will be needed.

     

    The reason I ask is because another possible solution came to me. If the users who will be accessing and using this are know before hand it may be possible to just have the script started from their login items when they log in. Again this would only work if this was only needed when a user was logged in.

     

    regards

  • twtwtw Level 5 Level 5 (4,580 points)

    launchd sends its error messages to the main system log; just open console and you'll see them.  Note, however, that launchd has a 10 second throttle period (meaning it won't allow a process to launch at intervals of less than ten seconds), so f you're quitting and restarting rapid-fire that might be the problem.

     

    in activity monitor you can see if you process is running.  launchd is always on.  if you want to check on the launnchd job itself run launchctl list in terminal.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.