3

How can I create functions and software for my Linux server? Let me explain this in a bit more detail.

So for my Linux server which I access with my SSH client, I have made a few Python scripts that work fine, but what I really want to do is have these Python scripts active all the time, such that I can execute functions I've created in the script (such as "def time(): ...") just by typing "time" in to the command line rather than starting up a script with ./script-name.py and then type "time". Do I need to install my Python files in to the system in some way?

I struggled searching Google because I didn't fully understand what to search, and results that came up weren't really related to my request. I did find the cmd Python module and learned how to create cmd interpreters, however, in order for me to access the commands I defined in the cmd interpreter, I had to first start the script, which as I explained above, not what I am looking for.

How can I make script-level Python functions callable from the Linux command line?

3
  • 1
    Lookup supervisord also, this is very vague. Commented Jan 14, 2016 at 0:00
  • You could just add functions to your .bashrc or /etc/profile like function time () { python /my/dir/my-script.py } Commented Jan 14, 2016 at 0:01
  • (time is a very bad name because it shadows the command time and to get it back you'd have to type \time instead.) Commented Jan 14, 2016 at 0:02

2 Answers 2

7

If you're using Python, you'll still need to fire up the interpreter, but you can make that happen automatically.

Start by making your script executable. Run this command in the shell:

chmod +x script-name.py
ls -l script-name.py

The output of ls should look something like this (note the xs in the left-hand column):

-rwxr-xr-x  1 me me     4 Jan 14 11:02 script-name.py

Now add an interpreter directive line at the top of your script file - this tells the shell to run Python to interpret your script:

#!/usr/bin/python

Then add code at the end of the file that calls your function:

if __name__ == '__main__':
    time()

The if statement checks to see if this is the file that is being executed. It means you can still import your module from another Python file without the time() function being automatically called, if you want.

Finally, you need to put your script in the executable path.

mkdir -p $HOME/bin
mv script-name.py $HOME/bin/
export PATH=$HOME/bin:$PATH

Now you should be able to run script-name.py, and you'll see the output of the time() function. You can rename your file to whatever you like; you can even remove the .py extension.

Additional things you can do:

  • Use the argparse module to add command line arguments, e.g. so you can run script-name.py time to execute the time() function
  • Put the script in a system-wide path, like /usr/local/bin, or
  • Add the export PATH=$HOME/bin:$PATH line to your .bashrc so that it happens by default when you log in
Sign up to request clarification or add additional context in comments.

Comments

2

The answer above is by far more complete and more informative than mine. I just wanted to offer a quick and dirty alternative.

echo "alias time='<your script> time'" > ~/.bashrc
bash

Like I said, quick and dirty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.