How can I make a program start up automatically in OSX? - macos

I have a little program that I want to make open automatically when my mac is started up.
Because this program accepts command line arguments, its not as simple as just going to System Prefs/Accounts/Login items and adding it there...
From google, I read that I can create a .profile file in my user's home folder, and that will execute whatever I put in it... So I have a .profile page in ~ like this:
-rw-r--r--# 1 matt staff 27 27 Sep 13:36 .profile
That contains this...
/Applications/mousefix 3.5
But it doesn't execute on startup! If I enter "/Applications/mousefix 3.5" manually into the terminal, it does work.
Any ideas?

From here and into the future, look into launchd for what you want to do. All other methods have been deprecated or are now unsupported. This is probably a bit more heavy-weight than what you want, though.
It could also be a problem with your version of the bash shell not correctly executing your .profile. Try putting the command into .bashrc in your home directory, and see if that helps.

You can use Lingon to help construct a plist file for launchd.

The most general way of launching things on startup on MacOS is using launchd. You can create a plist file to tell it to launch your program on startup, which can include arguments.

You can use Applescript which can run terminal commands, then have that applescript launched at startup.

The .profile and .bash_profile only come into play when you open a new shell (ie. opening Terminal or entering through SSH). Also, I believe if bash detects .bash_profile it won't look for .profile
If you want it start upon login, I would look at the other suggestions about launchd

You could always write a wrapper script that runs it with the arguments you want

Thanks all. The launchd solution is pretty cool, yes its heavyweight for such a simple thing, but its good to know, and as a developer I'm happy to tinker about :)

Related

Command on terminal startup

What I am trying to do is have my current ASCII ART appear on start up as well as have ls run so I can see my directories. I assume this is going to be a script that I will have to do so if anyone can point me in the right direction to get started that would be awesome. Thank you!
You'll most likely want to add whatever commands display your ASCII art and ls to the end of the ~/.bash_profile file. See here.
In other words, open the /Users/$USER/.bash_profile file in a text editor (or create it if it doesn't exist) and put ls at the end of the file on its own line.
This startup shell file is also called .profile or .bashrc on other linux/unix systems. More information on this behavior is available here. The scripting language for the file is bash.

Messed up my LIBRARY_PATH for bash, using OSX

I am quite the novice and I messed something up awhile back. I do not know what I did, but now whenever I start up a new terminal I constantly have to export LIBRARY_PATH=/opt/local/lib. This is the default path for OSX I thought, and I think I might have voided it somehow.
It has just become so annoying to type that every time I want to run something like a gsl library, where are the default paths for bash on osx set? Thanks for any help you can offer.
You can add it to ~/.bash_profile
ยง Invocation
When bash is invoked as an interactive login shell, or as a non-interactive shell
with the --login option, it first reads and executes commands from the file
/etc/profile, if that file exists. After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and
executes commands from the first one that exists and is readable.

Cygwin automatic script launch

Im trying to automatically run a script using Cygwin via CMD. I basically created a BAT file that goes to the directory and executes an .SH file. SH files are accosiated with Cygwin, and I tried something like "cygwin update.sh" in the command line. But all it really does is open Cygwin. I want Cygwin to automatically run the script file. Is there any easy way to do this, I've been trying to find but can't. Thank you!
You'll want to call the shell script with a particular shell, e.g. bash.
When having Cygwin open, call which bash to figure out where the binary is located. Cygwin also comes with tools that can convert paths between Cygwin and Win32 form, which is pretty helpful in cases like yours.
There is one other thing that may work, depending on your setup. There is an environment variable named PATHEXT which declares file extensions that are deemed "executable" by CMD. This can be used to your advantage, if Windows is configured so that the shell's "open" verb executes the correct shell for the file extension .sh (in your case).
Good luck.
From Cygwin Terminal, read man mintty. Try something like the following from a Windows Command Prompt:
c:\cygwin\bin\mintty --hold always --exec /cygdrive/c/path/to/bash/script.sh
I also found this!
http://rothmanshore.com/2011/01/26/kick-off-a-cygwin-script-from-a-windows-bat-file-with-a-different-working-directory/
I didn't quite understand it at first, but then it worked as I wanted it. Just if anyone knows, is there a way to make the script run without the CMD window open?? Thanks

Running a script automatically when logging in via ssh

I would like to know if there is a (simple) solution to the following issue:
When I log in with ssh to a specific host, I would like to automatically execute a (bash)script on that host. This way I could -for example- load my aliases on that host.
Definitively the bashrc script is not executed; The ssh configuration files do not seem to help in this issue either.
Any suggestions?
Thanks in advance!
BTW: The host is running on Gentoo
If .bashrc isn't being run, try .profile, which has a similar function. Different shells use different startup scripts at different times, so knowing when to run things is useful.
On many systems where you have a choice of which shell to use, you are put through ~/.profile only. This way there is no need to find out (and no probably wrong guessing) which shell you're running in and which profile to actually load (.bashrc, .cshrc, .kshrc etc.) and which ones to avoid loading.
The easiest solution in your case would be to create a link (a symbolic one if you prefer visibility) to your favourite shell's startup script as in ln -s ~/.bashrc ~/.profile. If you don't intend to ever using anything other than bash, you're set.

Start script on login

I have written a ruby daemon and I would like for it to run when I log in. It is normally run by going to the command line and calling ruby my_ruby_script.rb. How can I start my daemon on login? (Running 10.6 Snow Leopard).
There's an option to add applications etc that need to start at login, you could try writing a shell script or an apple script thing that launches terminal and runs ruby my_ruby_script.rb, or possibly even just add my_ruby_script.rb to this list after adding a #!/bin/env ruby line to the top of that file. http://support.apple.com/kb/HT2602?viewlocale=en_US gives precise instructions as to how to add an application to be started at login.
If you need to use AppleScript to actually start a terminal application (I believe this is not the case, but I am not in front of my mac now and hence can't test), just create an applescript file with something like
do shell script "ruby <path>/my_ruby_script.rb"
Hope this helps
As Panda said, add:
#!/bin/env ruby
to the begining of the file, and then you could add a reference to your file inside ~/.bashrc or ~/.profile or even /etc/profile , depending on your needs.
Check this out: https://stackoverflow.com/questions/3484429/profile-and-bashrc-doesnt-work-on-my-mac/3484472#3484472

Resources