The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - Newcomers (http://hintsforums.macworld.com/forumdisplay.php?f=15)
-   -   Launching script on login (http://hintsforums.macworld.com/showthread.php?t=84127)

ijustlost 01-09-2008 08:30 PM

Launching script on login
 
Ok, so I'm trying to set a script to run every time I log in..

My script looks like:

#! /bin/sh
<some unix commands>

I've named it script.command and set it as executable (chmod +x). I can run it from terminal as ./script.command

So far so good...

What I want to do is set this to run on Login. I've tried to do this through System Preferences >Accounts > Login Items and adding it as a Login Item, but on login Finder loads it in Smultron (my default text editor) instead of exectuing it as a script. How can I make it run as a script? Sounds like ti should be easy bit I've spent a long time trying to make this work!

hayne 01-09-2008 09:37 PM

I think you'll need to wrap this script in some way to make it a double-clickable application (e.g. wrap it in AppleScript) in order to run it as a Login Item.
I.e. I think Login Items need to be applications.

An alternative might be to install your script as a "login hook" - google for this or look for discussion on these forums or the main macosxhints site.

jbc 01-09-2008 10:04 PM

I've always used Launcher to run login scripts. After Launcher is installed, just use Get Info to set your shell script to "Open With" Launcher, then add the script to your Login Items. Note that this assumes your script is non-interactive, ie. it doesn't need to open a terminal window.

I'm not sure of Launcher's "Universal Binary" status at this point either (it's Carbon; can these be Universal?): I've been using it on my G4 for years with no problems whatsoever.

tw 01-09-2008 10:56 PM

if you want a shell script to launch at login, just call it from launchd. use a plist like this:

Code:

<?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>Label</key>
        <string>my.script</string>
        <key>ProgramArguments</key>
        <array>
                <string>/Users/whatever/whatever/shellscript</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
</dict>
</plist>

and put it in your launchagents folder.

chabig 01-09-2008 11:12 PM

tw has it right. launchd is built just for this purpose, and it's easy to use. Try Lingon as a graphical editor for the plist.

tw 01-09-2008 11:37 PM

Quote:

Originally Posted by chabig (Post 440966)
tw has it right. launchd is built just for this purpose, and it's easy to use. Try Lingon as a graphical editor for the plist.

incidentally, does anyone know another gui app for launchd? Lingon isn't getting updated very frequently, and I've been finding it frustrating to work with.

chabig 01-09-2008 11:42 PM

How often do you expect updates? It was last updated in November, just 2 months ago. And most of all, it works great.

tw 01-10-2008 12:00 AM

oh, I know - didn't mean to be critical. but I'm finding it very limiting. that's intentional (the author is aiming for a novice's tool).

cwtnospam 01-10-2008 12:15 AM

Are you sure your problem isn't with launchd and not Lingon? I've been trying to get a script to run at login, but it only runs at startup. If I logout and back in, it doesn't run. Lingon produces essentially the same plist as you have in post #4, so I doubt that it is the problem.

Hal Itosis 01-10-2008 12:38 AM

Quote:

Originally Posted by cwtnospam (Post 440978)
Are you sure your problem isn't with launchd and not Lingon? I've been trying to get a script to run at login, but it only runs at startup. If I logout and back in, it doesn't run. Lingon produces essentially the same plist as you have in post #4, so I doubt that it is the problem.

Let me guess... you're running Tiger --not Leopard -- right?
That is a (Tiger) launchd bug. Have you got 'OnDemand' false?

Maybe I'm wrong in this case, but that too will act as you describe.
And Leopard fixed it by giving the user their own instance of launchd.

In Tiger there was but *one* launchd proc.

-HI-

cwtnospam 01-10-2008 08:24 AM

Yes, I'm running Tiger. It's strange, because Activity Monitor shows launchd as on of my processes. Oh well, I guess I'll go back to using an Applescript run-only application at login.

Hal Itosis 01-10-2008 04:38 PM

Quote:

Originally Posted by cwtnospam (Post 441043)
Yes, I'm running Tiger. It's strange, because Activity Monitor shows launchd as on of my processes.

How does it look when Activity Monitor views all procs hierarchically?
Are there more than one launchd procs?

cwtnospam 01-10-2008 05:02 PM

Yes, there's launchd (1) whose parent is kernel_task(0) and launchd (94) whose parent is launchd (1)

P6SMSKC 01-10-2008 05:20 PM

Quote:

Originally Posted by cwtnospam (Post 440978)
... I've been trying to get a script to run at login, but it only runs at startup. If I logout and back in, it doesn't run....

One solution is to have your launchd process watch /var/log/secure.log and call your script on changes. Then have your script
Code:

tail -c numberofbytes /var/log/secure.log
grepping for some part of this...
Code:

Jan 10 13:30:43 My-Mac SecurityAgent[2086]: User Authenticated: continue login process
Jan 10 13:30:43 My-Mac com.apple.SecurityServer: authinternal authenticated user me (uid 502).


cwtnospam 01-10-2008 05:36 PM

That might work. I'd have to make the script owned by root or an admin user though, since I have a standard account for daily use.

ijustlost 01-10-2008 05:48 PM

Thanks for all the suggestions guys. I tried tws method (seems most elegant). Unfortuantly it doesn't seem to be executing - I've also tried doing the same thing using lingon, and still no luck.

My script needs to be run as root - is this the problem? I though Startup items ran as root anyway?

My script is:

Code:

#!/bin/sh
sudo ifconfig en0 ether XX:XX:XX:XX:XX:XX

where XX is some mac address. This scrtip runs fine from terminal when executed manually, but I can't get it to run automatically

tlarkin 01-10-2008 05:59 PM

Bombich has a loginwindow.app that will run scripts at log in and log out. Download it, install it, point the app to your script, give it admin access, done.

www.bombich.com

giskard22 01-10-2008 06:45 PM

Wow, I just found this thread. Took long enough for someone to finally mention loginhook functionality!!

Bombich's Loginwindow Manager will allow you to use a GUI to set both loginhooks and logouthooks -- scripts that the loginwindow process will run, as root, before the user's GUI session actually starts.

Now, that may or may not be what you want to do. If you want a script to run after the login -- after the Desktop appears -- the launchd method is great. Alternatively, you can use Platypus to generate a double-clickable app from any shell script and put it in your login items.

hayne 01-10-2008 07:40 PM

Quote:

Originally Posted by giskard22 (Post 441208)
Wow, I just found this thread. Took long enough for someone to finally mention loginhook functionality!!

You mean post #2 ?
:)

giskard22 01-10-2008 08:00 PM

D'oh! LOL!

cwtnospam 01-10-2008 10:39 PM

Login Hook, launchd, it just doesn't seem to want to work. :confused:

I'm trying to automatically start Folding@home on login. So far, the only way I've been able to do it is using an Applescript.app and login items. I need a way to execute this line in a Terminal window:

/path/to/fah5 -local

If I try to execute it from a shell script, it doesn't run. If that shell script is a login hook, it hangs the system! From Applescript I can tell the Terminal to open a new window and execute fah5 in it, but I'd prefer to get a login hook or launchd to work. Any ideas?

- Oh, and this is in Tiger.

chabig 01-10-2008 11:36 PM

I used Lingon to create this LaunchAgent:

Code:

<?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>Label</key>
        <string>RunFolding</string>
        <key>ProgramArguments</key>
        <array>
                <string>/path/to/fah5</string>
                <string>-local</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
</dict>
</plist>


cwtnospam 01-11-2008 12:11 AM

Close! But it doesn't get the -local portion correct. This gets logged repeatedly in the Console:

Quote:

[05:04:43] Configuring Folding@Home...

User Name? Team Number? Ask before fetching/sending work (no/yes) [no]? Use proxy (yes/no) [no]? Allow receipt of work assignments and return of work results greater than
5MB in size (such work units may have large memory demands) (no/yes) [no]? Change advanced options (yes/no) [no]? [05:04:43] Could not write MyFolding.html.
Maybe if I move the app to the same folder as the data...

Yes, moving the app seems to work, but since it's no longer running in a Terminal window, I can't be sure. Anyone know of a way to redirect the output of a running process? It appears to be writing these files:

/usr/lib/dyld
/usr/lib/libSystem.B.dylib
/dev/null
/dev/console

I'm assuming /dev/null would normally be the Terminal window.

tlarkin 01-11-2008 01:48 AM

Isn't folding@home an application, or is it strictly a background process?

So, maybe use loginwindow manager with this simple shell script, of course this will be at log in not start up, so you would have to log in.

Code:

#!/bin/bash

open -a "folding@home.app"


P6SMSKC 01-11-2008 05:26 AM

Quote:

Originally Posted by cwtnospam (Post 441278)
Anyone know of a way to redirect the output of a running process?

If your still launching the script with launchd you can direct the standard output and standard error paths. The pertinent part of the plist for changing standard out looks like this.
Code:

<key>StandardOutPath</key>
        <string>/Library/Logs/myscript/myscript.stdout</string>

I use lingon so I'm not sure that's the only required key to redirect output, but I think so. Lingon rocks btw.

cwtnospam 01-11-2008 08:59 AM

Quote:

Originally Posted by tlarkin (Post 441285)
Isn't folding@home an application, or is it strictly a background process?

Fah is a small (256K) Unix executable that calls FahCore_78.exe which is (2.6 MB), so I'm not sure what's going on there.

Quote:

Originally Posted by P6SMSKC (Post 441308)
The pertinent part of the plist for changing standard out looks like this.
Code:

<key>StandardOutPath</key>
        <string>/Library/Logs/myscript/myscript.stdout</string>


That did it!

Hal Itosis 01-11-2008 07:06 PM

Quote:

Originally Posted by tlarkin (Post 441285)
Isn't folding@home an application, or is it strictly a background process?

So, maybe use loginwindow manager with this simple shell script, of course this will be at log in not start up, so you would have to log in.

Code:

#!/bin/bash

open -a "folding@home.app"


I'm just adding some general info, not suggesting a solution here.
To fire up an "app" via launchd plist, here is another way (I use):
Code:


        <key>ProgramArguments</key>
        <array>
                <string>/Applications/FastScripts.app/Contents/MacOS/FastScripts</string>
        </array>


cwtnospam 01-12-2008 09:13 AM

Well, I thought it was working. :(
For some reason, FAH is failing in the middle of work units:
I noticed this at the top of the output file: Ignoring argument: -local

when I run it from the Terminal, I get: Using local directory for work files

Here's a sample error:
Code:

[18:39:32] Writing local files
[18:39:32] Writing local files
[18:39:32] Completed 0 out of 5000000 steps  (0)
[20:28:44] Writing local files
[20:28:45] Completed 50000 out of 5000000 steps  (1)
[22:09:22] Writing local files
[22:09:22] Completed 100000 out of 5000000 steps  (2)
[23:53:27] Writing local files
[23:53:27] Completed 150000 out of 5000000 steps  (3)
[23:53:27] Size of work/wudata_03.xtc not what saved.
[23:53:27]
[23:53:27] Folding@home Core Shutdown: FILE_IO_ERROR
[23:53:28] CoreStatus = 75 (117)
[23:53:28] Error opening or reading from a file.
[23:53:28] Deleting current work unit & continuing...
[23:53:28] - Preparing to get new work unit...

I'm running it in the Terminal now to see if that still works, but it's only at step 17 out of 100.

P6SMSKC 01-12-2008 05:52 PM

Quote:

Originally Posted by cwtnospam (Post 441610)
I noticed this at the top of the output file: Ignoring argument: -local

when I run it from the Terminal, I get: Using local directory for work files

What type of agent / daemon are you calling fah from...i.e. where is the plist. Processes started from /Library/LaunchAgents or /Library/LaunchDaemons will be run as root and have no HOME defined (I think) and lack permissions to be in your home anyway. I've had this trouble before with processes launched by anacron.

Try writing a script that will launch FAH for you. and then just calling that script from launchd. Have the script export HOME as your users home directory. I know there are provisions for defining the home directory within a launchd plist, but I've not used them so I don't know if this will help.

OTOH if you have the plist in ~/Library/LaunchAgents I've got no idea. Does FAH have any way to pass more info to it on launch (--path-to-workspace="")?


p.s. do any of you have trouble hitting tab to auto-complete only to realize your in a text editor?...I do this all the time.

cwtnospam 01-12-2008 06:54 PM

The plist is in ~/Library/LaunchAgents. I tried to do this through a script, but I don't know of another way to run it from a Terminal window without using osacript, in which case I might as well call it from login items.

tw 01-13-2008 03:27 PM

Quote:

Originally Posted by cwtnospam (Post 441701)
The plist is in ~/Library/LaunchAgents. I tried to do this through a script, but I don't know of another way to run it from a Terminal window without using osacript, in which case I might as well call it from login items.

try saving it in /Library/LaunchAgents rather than ~/Library/LaunchAgents - that will run it as root and avoid any permissions problems. you might need to explicitly set the working directory using the 'WorkingDirectory' key - it's not clear from the posts so far precisely where the files it's working with live.

cwtnospam 01-13-2008 04:06 PM

Won't that cause it to run for each user that logs in? If I give it explicit paths in that case, they couldn't be in a user's home, but if they weren't, then I could end up with multiple instances working on the same data.

Maybe I'll try it between snow shoveling sessions tomorrow. :eek:

tw 01-13-2008 04:58 PM

well, the other alternative is to launch it from the root folder, but use the UserName key to make sure it always runs as a particular user (I'm assuming they have that key in Tiger, incidentally - I only have easy access to the Leopard man page). that might mean that you'd want create a separate user dedicated to doing whatever it is this app does...

I checked out the folding@home website, incidentally. you know, on the FAQ page they imply you can just add folding@home to your login items, and it will run just fine. see it here... did you try that way, and if so what was the problem?

cwtnospam 01-13-2008 05:05 PM

That's the graphical application. I'm using the command line console in this case so that it runs in the background while I use the system. I also run the screen saver, so that gives me two instances at a time. Since I've only got a dual processor system, that's all I want running.

cwtnospam 01-13-2008 05:11 PM

Here's the Applescript I was using to start it. I saved it as a run only app and added it to login items, and it works well, except that I can't do anything until after the gui scripting completes. A login hook or launchd wouldn't need the gui portion and it would be quicker too.

Code:

tell application "Terminal"
        activate
        tell application "System Events"
                key down command
                keystroke "n"
                key up command
        end tell
        set pth to "/Users/username/Documents/fah5 -local"
        do script pth in window 1
end tell


tw 01-13-2008 05:21 PM

well, the Console version has the same instructions (here). my sense is, though, that this opens and runs the command in terminal. let me try downloading it myself and see what happens...

cwtnospam 01-13-2008 05:44 PM

Quote:

Originally Posted by tw (Post 441887)
well, the Console version has the same instructions (here).

So it does! Not only that, but it works, too!

tw 01-13-2008 05:57 PM

Quote:

Originally Posted by cwtnospam (Post 441890)
So it does! Not only that, but it works, too!

lol - well that makes things a trifle easier. :D

I actually wouldn't mind doing this myself, but man is it a CPU hog. does that settle down after a while?

cwtnospam 01-13-2008 06:00 PM

It's low priority, so it gets out of the way easily, but it will use as much of the cpu as is available. That's what I like about it. I can leave it running all the time, and it won't hog the processor when I need it. When I leave the computer, I start the screen saver, which keeps the other processor busy. That way my system is doing some good while I'm not using it.

tw 01-13-2008 06:11 PM

cool. I will run it then. :)


All times are GMT -5. The time now is 10:16 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.