CLI Apps: Tips and Tricks
CLI Apps: Tips and Tricks RidgeRun documentation is currently under development. |
This wiki page is a compendium of different tips and tricks that can be used to improve the user experience of CLI applications on embedded Linux systems.
Example App
Let's use a simple Python application as an example to build upon.
mgruner: It is not clear which script is which, I recommend adding a title indicating the name of the script (please remove this box when addressed) |
import os def main(): print("These are the devices connected to the I2C-2 bus\n") os.system("sudo i2cdetect -y -r 2") if __name__ == "__main__": main()
The application would simply execute a shell command to see all the I2C devices connected to bus 2. The app can be run with:
sudo python3 demo.py
Autorun the App
To run the application automatically we will use SystemD and profile.d
. Every time a new shell is created, the scripts contained in this directory will be executed. If we add a script to run the Python app, then the app will run every time a new shell is opened.
- Install application in a known directory, for example:
sudo cp demo.py /usr/local/bin/
- Create a bash script with the command that executes the app.
cd /home/$USER sudo python3 /usr/local/bin/demo.py
mgruner: I don't understand why do we need two different scripts. Can't we run the i2cdetect directly here? (please remove this box when addressed) |
- Change the permissions on the script:
sudo chmod 755 autorun-app.sh
- Finally copy the script that runs the app into the
profile.d
directory. This will effectively run the Python demo app every time a new shell is started.
mgruner: Is this preferred over systemd scripts? (please remove this box when addressed) |
sudo cp autorun-app.sh /etc/profile.d/
- To test this, simply open a new terminal window.
Linux RunLevels
Embedded platforms are often used to run a single CLI app and only that. Having a full GUI with multiple applications and icons can be distracting and even undesired. To help with this, Linux has implemented "runlevels". Different runlevels add/remove functionality to the Linux installation, for example to remove networking capabilities or the GUI.
In this example case, the application is a CLI app that does not require a GUI nor a display manager. To simplify the end user experience, the runlevel can be changed very easily to one that just has a terminal.
- Change to multi-user runlevel (no GUI):
sudo systemctl set-default multi-user
- In case you want to go back to the default runlevel with GUI:
sudo systemctl set-default graphical
- Reboot the system for the changes to take effect.
- After logging in, the Python app should run automatically, as it has been configured with
profile.d
.
Change Kernel Logging Level
You might have noticed from the last step that when the app runs in multi-user runlevel there appear a lot of kernel log messages. This happens because in multi-user runlevel, the printk driver redirects any kernel logs to the standard output/error. This could be very annoying if you intend to use the multi-user runlevel because any kernel event could flood the terminal you are using. To help against this, the kernel logging level can be changed to suppress most of the messages.
- Check the current kernel logging level and save it in case you want to restore it later.
nvidia@nvidia-desktop:~$ sudo sysctl kernel.printk kernel.printk = 6 6 1 7 4
- Minimize kernel logging level:
sudo sysctl -w kernel.printk="3 4 1 3"
Conclusions
After running all these steps you will have an embedded platform that boots into multi-user runlevel without a GUI, with minimal kernel logging level and that runs the Python demo application automatically after logging in. The demo application was used as a simple example, but more complex user applications can be implemented.
References
- Five Ways To Run a Program On Your Raspberry Pi At Startup
- Run a Script on Startup in Linux
- What do the scripts in /etc/profile.d do?
- Understanding Linux runlevels the right way
- How to stop kernel log messages from flooding my console
RidgeRun Resources | |||||
Contact Us
|