3

I am trying to create an Alias to launch mystepper6.py and moveit.py and sudo ps ax by placing the following alias' in sudo nano ~/.bashrc (Note: I am using Python 2 for this script.)

reboot='sudo reboot'
ax='sudo ps ax'
runstepper='python home/pi/mystepper6.py'
moveit='sudo python home/pi/moveit.py'

The alias reboot works fine but none of the others work at all. All I get is "bash: runstepper: command not found".

I am doing this because I am trying to control my webcam on my Raspberry Pi 2 using my iPhone with the iFreeRDP app. I use remote desktop connection from my Windows 10 laptop. The problem with this app and some other similar apps, is that the period and space keys do not function (it is a know reported issue). This makes typing full commands impossible.

Incidentally, I tried to use the VNC Viewer iPhone app and got my Raspberry Pi 2 hijacked when I loaded the required software onto the RPi2 requiring me to get a new SD card. Fortunately, I just cloned my SD card a few hours prior. Long story, but I am very weary about using VNC Viewer now.

Please help me with my alias' so I can either type one word with no spaces or periods or create a desktop short cut that I can double click so I can use it as a workaround for the deficiencies of these otherwise good apps. I am not sure the Ctrl + C works on the app keyboards either, so a short cut for that would be good as well.

2
  • 1
    the fact that your reboot='sudo reboot' works is quite unexpected. Commented Mar 18, 2016 at 21:14
  • There's no way any of those "aliases" work. Something else is making reboot work. Commented Mar 18, 2016 at 22:02

1 Answer 1

3

to create aliases in your shell, you shall use the alias shell directive:

alias reboot='sudo reboot'
alias ax='sudo ps ax'

To run ps ax you do not need to sudo first. If you're running a standard kernel, any user can see the list of all processes without special privileges.

For the two python aliases:

alias runstepper='python home/pi/mystepper6.py'
alias moveit='sudo python home/pi/moveit.py'
                          ^-- missing / here

do not forget about the first / in the path, or whenever you launch the aliased command, you'll have python look up for the script relatively to the current directory. i.e. if you're in /home/pi, it will look it up into /home/pi/home/pi/movestepper6.py and tell you the script does not exists. So the proper command should be:

alias runstepper='python /home/pi/mystepper6.py'
alias moveit='sudo python /home/pi/moveit.py'

Though as a suggestion to you, instead of making aliases to run python scripts, I'd make them into a proper python package. Considering that within both codes your entry points are a function called main(), i.e., both scripts end with:

if __name__ == "__main__":
    main()

you should create a directory for your project:

cd /home/pi
# create a directory for your python project:
mkdir motion_control
# create a directory to place your scripts within:
mkdir motion_control/motion_control
# adding an empty __init__.py file makes that directory a python package
touch motion_control/motion_control/__init__.py
nano motion_control/setup.py

and now you just have to add this within the setup.py file:

from setuptools import setup

setup(name='motion_control',
      version='0.1',
      description="Python library to operate stuff that move on my rasppi",
      long_description='explain how to use the tools installed by this package',
      classifiers=[],
      keywords='raspberrypi motion control',
      author='YOU',
      author_email='YOUR@EMAIL',
      url='ANY URL YOU THINK IS RELEVANT',
      license='MIT', # or any license you think is relevant
      packages=['motion_control'],
      zip_safe=False,
      install_requires=[
          # add here any tool that you need to install via pip 
          # to have this package working
          'setuptools',
      ],
      entry_points="""
      # -*- Entry points: -*-
      [console_scripts]
      runstepper = motion_control.mystepper6:main
      moveit = motion_control.moveit:main
      """,
)

the entry_points part is very important, as it's telling python where to look for the first function to execute to have the script run. For example:

      moveit = motion_control.moveit:main

means "look for the main() function within the moveit module in the motion_control package". So adapt accordingly! As a note: don't make that main() function take any parameter, but rather do the argument parsing within it (if you parses arguments).

and finally, to install it, all you need to do is:

cd motion_control
sudo python setup.py install

and you'll have runstepper and moveit installed in the same directory as your python executable.

HTH

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for all of your work. I will give this a try and get back to you for sure. Thanks!
Will these alias' work from different computers and after the power is turned on and off? The reason I ask is that the alias 'ax' worked great when accessing the RPi from my laptop, but gave me the same error message when I used used the iFreeRDP from my iPhone 6; just got the "bash: ax: command not found" error.
Also, when I log off from my laptop and log back on and then into the RPi, the 'ax' alias is no longer recognised (get the same bash error). Is there a way to put it into the RPi so it is there when ever or where ever I need it?
You need to add those lines to your /home/pi/.bashrc (and on other computers, whatever username it is you're using)
It almost works... The runmystepper has a "IOError: [Errno13] Permission denied: '/home/pi/vert.txt". The mystepper6.py runs two steppers and saves the last position in a .txt file so when power is turned back on, they read the data from the .txt files to find home. The two files are vert.txt and horz.txt and located in the same place mystepper6.py is located. Also, how would I make an alias for kill -s kill 465 (may not be 465 next time). I have to look the process up to kill from ps ax but the problem is the space between kill and 465 needs to be handled correctly.
|

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.