Run Shell Script

Discussion in 'Mac Programming' started by ruhi, Aug 17, 2009.

  1. ruhi macrumors member

    Joined:
    Jun 17, 2009
    #1
    Hello all,

    I have made a .sh Shell Script file which contains the script that will execute my application.

    I have tested the same on automator its working fine. Now how should i run it using terminal??

    When i am running it using automator it is executing my application but after 10 sec my application doesn't respond.

    What is need to be done?

    Thanks,
    Ruhi.
     
  2. fredthefool macrumors newbie

    Joined:
    Jun 4, 2008
    #2
    Hi Ruhi,
    it would be helpful, if you could spread up some more details, e.g. the code of your script or the App it should start.
    One first approach could be to check the environment of your script, the search path, or just the correct shebang. There are many possible traps ...

    Did your shell-script work regularly in automator or does your App just start and hangs then? The shell command to start an (Mac) App, which is actually a bundle, would be
    Code:
     open -a /Path/to/Your.app
    Did you notice about the article
    http://developer.apple.com/document...atorConcepts/Articles/ShellScriptActions.html
     
  3. pcwiz macrumors member

    Joined:
    May 28, 2008
    #3
    fredthefool's open command is the best way to do this using shell.

    If you want to run a shell script using the Terminal just type in:

    Code:
    sh <path to shell script>
    or

    Code:
    sudo sh <path to shell script>
    If you need administrator privileges
     
  4. ruhi thread starter macrumors member

    Joined:
    Jun 17, 2009
    #4
    Run Shell Script

    Hello,

    Thanks for your response.

    To clearly explain i have a cocoa application. Now i want to set it to system startup item not a login item.

    After research i found that i need to put a script in /Library/Startupitem/myscriptFolder/ and a plist with it.

    So i am trying to make that script.

    so that when it run on startup it will launch my Application. So how to write a shell script which will launch my myexample.app.

    Is that the right approach i am following?

    Can you help me with this script also do it need root privilages to put the script in startupitem folder?

    I really need to do this.

    Thanks,
    Ruhi.
     
  5. fredthefool macrumors newbie

    Joined:
    Jun 4, 2008
    #5
    There are several ways to start a program at boot time, the 'best' depends on the 'character' of the app: Is it more of a real deamon or is it an interactive GUI'ed Application every user should be provided with? For StartupItems being 'deprecated' ( as i read the offical apple documentation ) you maybe could use just the recommended launchd.

    For launchd you need a .plist, which is an XML file, incorporating the name and the path to your app. You could use this example as a template:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
     <plist version="1.0">
     <dict>
       <key>Label</key>
       <string>YourAppsName</string>
       <key>Program</key>
       <string>/usr/local/sbin/Your.app</string>
       <key>ProgramArguments</key>
       <array>
         <string>/usr/local/sbin/Your.app</string>
       </array>
       <key>RunAtLoad</key>
       <true/>
     </true/>
     </dict>
     </plist>
    
    Place this file under /Library/LaunchAgents/com.yourname.YourApp.plist using sudo (you will need administrator privileges) and assure that it's being owned by root:wheel (which should be the default moving it with 'sudo')

    More details, especially about the PropertyList's options, are under

    http://en.wikipedia.org/wiki/Launchd

    Next, use the launchctl command in terminal to load it:

    Code:
    sudo launchctl load /Library/LaunchAgents/com.yourname.YourApp.plist
    In case of your app needing some special arguments it could become necessary to wrap the program start in a shell script. Then you have to replace the path to Your.app in the .plist file with the path to your start script.


    If you prefer the StartupItems way i recommend the article

    http://developer.apple.com/document...tartupItems.html#//apple_ref/doc/uid/20002132


    There are some tools out there which will help you to set up a 'launch at boot time app'. Two of them which will give you a nice GUI, you'll find under

    http://www.codepoetry.net/products/launchdeditor

    or

    http://sourceforge.net/projects/lingon/files/
     
  6. fredthefool macrumors newbie

    Joined:
    Jun 4, 2008
    #6
    Or maybe it's easier to set up the App as login item for all of the user accounts via system preferences. (if you have an administrator account)
     
  7. pcwiz macrumors member

    Joined:
    May 28, 2008
    #7

Share This Page