12 Replies Latest reply: Nov 7, 2012 3:26 PM by strawberrycheesecake
strawberrycheesecake Level 1 Level 1 (0 points)

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)
  • 1. Re: Running script automatically at startup at system level
    Frank Caggiano Level 7 Level 7 (23,820 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.

  • 2. Re: Running script automatically at startup at system level
    strawberrycheesecake Level 1 Level 1 (0 points)

    Thank you Frank! Seems like my question is already moved ! I am going to look up launch daemon. Out of curisosity,  which part of what I would like to do cannot be accomplished by using Automator?

  • 3. Re: Running script automatically at startup at system level
    Frank Caggiano Level 7 Level 7 (23,820 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

  • 4. Re: Running script automatically at startup at system level
    twtwtw Level 5 Level 5 (4,690 points)

    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.

  • 5. Re: Running script automatically at startup at system level
    strawberrycheesecake Level 1 Level 1 (0 points)

    Thank you Frank and twtwtw! I started looking into how to use launchd last night but haven't tried it out yet because I don't have access to the MAC all the time. I think I will need Launch Daemon instead of Launch Agent because I would like my script to be running in the background all the time, once the MAC is powered on, whether there is anyone logged in.

     

    I do wonder if I will end up having to use Launch Agent instead because my script is bascially launching a server listening for requests and firing off Safari once it receives a request to start running a test against Safari. I am pretty new to Safari also (I only know the basic, at the user-level). I am not sure if Safari is user-profile based, and that I will have to have someone logged in in order to open up Safari.

     

    I am planning to try out Launch Daemon either later today or the beginning of next week.

  • 6. Re: Running script automatically at startup at system level
    twtwtw Level 5 Level 5 (4,690 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.

  • 7. Re: Running script automatically at startup at system level
    strawberrycheesecake Level 1 Level 1 (0 points)

    Thanks again twtwtw! I am sorry I kept using captial letters for Mac (because of it being an enum in my code.. -_- I am definitely getting old..).

     

    I haven't decided if my test will call Safari in a particular user context. I will try out Launch Daemon and Launch Agent.

  • 8. Re: Running script automatically at startup at system level
    Frank Caggiano Level 7 Level 7 (23,820 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

  • 9. Re: Running script automatically at startup at system level
    strawberrycheesecake Level 1 Level 1 (0 points)

    Thank you so much Frank! Yeah, I would like my testing to be going on even when no one is logged in. The Mac is going to be a shared machine. It's major use is for running automated website tests, but in the future, we may use it for soemthing else, we don't know yet. We don't need to see the tests running visually, the results are good enough for now.

     

    I really appreicate your another possible solution because we are experiementing now. We may end up going into that direction. It's good to know more and have more ideas.

  • 10. Re: Running script automatically at startup at system level
    strawberrycheesecake Level 1 Level 1 (0 points)

    I am trying out launchd now. I set my LaunchDaemon to run at startup (i.e. I use "<true/> for the key "RunAtLoad") but it only started at startup once (the first time out of the 6, 7 times I tried). I wonder if anyone knows where the log file for launchd is? I cannot see any launchd file under /var/log. I also wonder how I can tell if it's running in Activity Monitor? Thanks!

  • 11. Re: Running script automatically at startup at system level
    twtwtw Level 5 Level 5 (4,690 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.

  • 12. Re: Running script automatically at startup at system level
    strawberrycheesecake Level 1 Level 1 (0 points)

    Thanks a lot twtwtw! launchctl is helpful telling me all the launchd jobs that are running.  I added key "KeepAlive" and it resolved the problem of my launchd job not running after the machine restarted (I only used RunAtLoad at first because I was starting up a server which kept listening and was not supposed to end, but there must be something I am missing).