Sunday, March 13, 2016

command line - Autostart Python script in Terminal Lubuntu


I'm using Lubuntu with the ODROID C0. There is a Python script which needs to be autostarted. I navigated to



[start menu]>preferences>Default applications for LXSession



, then navigated to the autostart tab and added xterm -e "python /path/to/script.py". The xterm autostart works but reads the error



xterm: Can't execvp "python: No such file or directory



Any ideas about how I can properly configure the autostart of this Python script?


Thanks.



Use xterm -e python /path/to/script.py, without the quotes, instead.


Your " quotes aren't behaving the way you want. xterm -e is trying to run "python with /path/to/script.py" as its argument, instead of python with /path/to/script as its argument. In this case you can simply omit the quotes.


In a shell script or when you run a shell interactively, quotes are treated specially and quote removal is performed. But this does not happen with commands you specify in LXSession configurationAutostartManual autostarted applications.


Fortunately, even though xterm -e in Ubuntu will perform word splitting on one argument to treat it as a command, it also accepts multiple arguments. That's why -e has to be the last option--everything else is interpreted as being part of the command. For example, if you run xterm -e nano foo, that opens foo in nano in an XTerm window. As man xterm says:


   -e program [ arguments ... ]
This option specifies the program (and its command line
arguments) to be run in the xterm window. It also sets the
window title and icon name to be the basename of the program
being executed if neither -T nor -n are given on the command
line. This must be the last option on the command line.

Assuming you're not trying to run a script whose path has spaces in its name, all you have to do is remove the quotes:


xterm -e python /path/to/script.py

Two other caveats come to mind:



  • When the script finishes, python quits, and then the XTerm window will quit too. If you don't want this then you can add code to the Python script that waits for user interaction, or you can write a wrapper shell script (or any kind of wrapper script) that runs python /path/to/script.py and then waits for user interaction.

  • XTerm works fine in Lubuntu but the default GUI terminal emulator is LXTerminal. In case you would prefer to use that, you can use lxterminal instead of xterm. The lxterminal command accepts -e and treats it the same way as xterm, so this would look like lxterminal -e python /path/to/script/py. As with XTerm, LXTerminal will quit when the command is finished running.


No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...