1

How can I doc a specific terminal window on my Mac ( OS X 10.9.2) that upon clicking executes my python program from within shell script?

In my job.sh file I have this:

#!/bin/bash
python python_script.py

This is what I have in my python_script.py file:

import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()

Basically I don't want to have to open up my terminal window, type ./job.sh and run the command, I want my terminal window to run ./job.sh by default just by clicking it.

Timoor Kurdi
  • 65
  • 1
  • 6

2 Answers2

0

You can just put the command in your .profile or .bash_profile or .bashrc file:

echo 'python python_script.py' >> ~/.profile
# or
# echo '/path/to/job.sh' >> ~/.profile

When you open up a new terminal, the .profile file will be sourced and your job will be run.


Note: Command to be put into the file is python python_script.py or /path/to/job.sh
Jahid
  • 21,542
  • 10
  • 90
  • 108
  • So, I went to my .bashrc file, it had this inside of it. [ -n "$PS1" ] && source ~/.bash_profile I closed out any existing terminal windows and commented out the above line for now and added what you suggested: echo 'python python_script.py' >> ~/.bashrc However it still does not open and execute my python program upon clicking a brand new terminal window. – Timoor Kurdi May 15 '16 at 19:30
  • @TimoorKurdi : I meant to run those codes in terminal. It will add the needed commands, i.e run `echo 'python python_script.py' >> ~/.profile` and it will add `python python_script.py` in the end of the file. – Jahid May 15 '16 at 19:34
  • My mistake! I ran it in the terminal and this is what my .bashrc file looks like now. [ -n "$PS1" ] && source ~/.bash_profile python python_script.py Closed out the terminal and reopened but still a no go :( – Timoor Kurdi May 15 '16 at 19:43
  • @TimoorKurdi : what about .profile file. It's probably the file for Mac OS – Jahid May 15 '16 at 19:47
  • No luck unfortunately. This is what the .profile file looks like after running the command. PATH=$PATH:$HOME/.rvm/bin python python_script.py Could it be something in the .bash_profile file that could be interfering with it? I have a bunch of stuff in there that was added a while back by somebody else. This is in the .bash_profile and not the .profile file. I ran the 'python python_script.py' >> ~/.bash_profile just to be sure and it gave me this error. python: can't open file 'python_script.py': [Errno 2] No such file or directory – Timoor Kurdi May 15 '16 at 19:56
0

Figured it out! It was the .bash_profile file in my case(OS X) that needed the

'python python_script.py' >> ~/.bash_profile

command executed as my .profile is overlooked due to the order in which bash profile files are read. For anyone who has no idea about this(like me until today), definitely checkout http://hayne.net/MacDev/Notes/unixFAQ.html#shellStartup

The accepted answer for this question led me there, https://apple.stackexchange.com/questions/12993/why-doesnt-bashrc-run-automatically

After that, it was simply a matter of being in the right directory to execute the script.

Also be careful and make sure to properly exit out of your server(ctrl-c) when your done, as I closed out the workspace by accident before and it led to process being bound to the default port and kept giving me an error which was solved by this. socket.error: [Errno 48] Address already in use

Community
  • 1
  • 1
Timoor Kurdi
  • 65
  • 1
  • 6