Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I wrote a script. When it starts, it won't stop, and it keeps getting data from the Internet. I can call it this way:

cd /User/Desktop/project/internetScanner/
python3 main.py start

But I would like to call it directly from the terminal like this, within the destination:

internetScanner start

How can I do so?

share|improve this question
2  
I wonder if the title could be changed on this.. –  user606723 Dec 30 '11 at 21:53
    
@user606723 Feel free to suggest an edit to have it changed. The first title "How to let my programme can run on shell?" wasn't very good. –  slhck Dec 31 '11 at 8:21
add comment

3 Answers

up vote 27 down vote accepted

You should probably rename your file main.py to internetScanner. Extensions on *nix are purely optional. It shouldn't matter here.

mv main.py internetScanner

Then, add the following line to this file, right at the beginning:

#!/usr/bin/env python3

This will make sure that when the shell executes the file, it will know to use python3 to interpret the content. This is known as the Shebang. Now, make the file executable:

chmod +x internetScanner

You can now run your program from within /User/Desktop/project/internetScanner/:

./internetScanner start

Your program will run in the foreground and continue running until you press Ctrl-C. If you do not want this, you can also start the program in the background, by adding an ampersand after the command:

./internetScanner start &

This will let your program run, but you can continue to use your shell. This is called job control, and there's a simple tutorial about it here.

If you now want to be able to run the program from anywhere on the system, you need to add the internetScanner directory to your PATH: What are PATH and other environment variables, and how can I set or use them?

share|improve this answer
    
+1 for nice answer! –  César Dec 30 '11 at 17:20
    
Good answer. I'd recommend adding that he might want to end the command with an ampersand (&) if he doesn't want to wait for it and/or redirect its output and error stream to a file. –  Ben Hocking Dec 31 '11 at 1:36
    
@BenHocking Thanks, I added it to the answer. –  slhck Dec 31 '11 at 8:22
    
X & doesn't detach the process from the controlling shell, it only backgrounds the process, meaning if that shell exits it'll get the TERM signal sent to it; so if you don't want that you should use detach, detach ./internetScanner start. –  Dan D. Dec 31 '11 at 12:04
1  
@DanD. … or nohup, disown or at, et cetera. How do I detach a process from Terminal, entirely? –  slhck Dec 31 '11 at 13:01
show 1 more comment

Assuming no other files in /User/Desktop/project/internetScanner/ are needed, if you want to install for a single user, link (ln -s) main.py to $HOME/bin/internetScanner. You'll probably need to mkdir $HOME/bin first.

Next time you log in, $HOME/bin will probably be added to your PATH. If you want it available for all users, copy it to /usr/local/bin.

If it must execute in /User/Desktop/project/internetScanner/, either start by importing os and calling

os.chdir('/User/Desktop/project/internetScanner/') 

or create a launcher script in $HOME/bin or /usr/local/bin that changes to /User/Desktop/project/internetScanner/ and executes the script.

share|improve this answer
add comment

Put alias internetScanner "python /User/Desktop/project/internetScanner/main.py" in .aliases file (for csh). For bash, put alias internetScanner="python /User/Desktop/project/internetScanner/main.py" in .bashrc. After that you can use internetScanner start from anywhere.

share|improve this answer
    
I've never heard of a .aliases file. Is that something standard for Bash? or do you have to explicitly source it in your .bashrc? –  Kazark Nov 10 '12 at 3:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.