简体   繁体   中英

Is there a way to have the default terminal window execute my python program from within shell script?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM