Automated scripting and running headless

I am developing with a Raspberry Pi 3 and an Arduino Due. These two are connected via a USB cable. On the Pi I am running PlatformIO version 3.4.0.

Via a terminal window I can get the Pi to flash the Due, no issues there. PlatformIO works as expected.

Now I trying to run the Pi headless and use Python scripts called through cron scheduling. Within these scripts I have system calls to execute functions (such as platformio run commands). I’ve tested the Python scripting and I do not have any syntax errors, however, the scripts fail when they try to execute the platformio system command. I am guessing these is some issue with user/permissions (as the daemon scripts are run as user “root”), but I am not sure.

Also, I’ve had similar functionality working previous and the issue may be with a recent update/re-install of PlatformIO on the Pi.

Any suggestions or comments would be wonderful! Thanks!

See ubuntu - How to specify in crontab by what user to run script? - Stack Overflow

At this point, I don’t think the issue has to do with permissions as I previously thought.

I use Flask to manage my web content on a local server and to provide Python backend functions. When I try to run any Platformio commands via Flask I get the following message in my Apache error log:

[Tue Jul 25 21:01:21.435938 2017] [mpm_prefork:notice] [pid 2060] AH00169: caught SIGTERM, shutting down
[Tue Jul 25 21:01:23.658197 2017] [wsgi:warn] [pid 2367] mod_wsgi: Compiled for Python/2.7.8.
[Tue Jul 25 21:01:23.658316 2017] [wsgi:warn] [pid 2367] mod_wsgi: Runtime using Python/2.7.9.
[Tue Jul 25 21:01:23.670082 2017] [mpm_prefork:notice] [pid 2367] AH00163: Apache/2.4.10 (Raspbian) mod_wsgi/4.3.0 Python/2.7.9 configured -- resuming normal operations
[Tue Jul 25 21:01:23.670216 2017] [core:notice] [pid 2367] AH00094: Command line: '/usr/sbin/apache2'
Error: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/platformio/__main__.py", line 104, in main
    cli(None, None, None)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 700, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 680, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1022, in invoke
    cmd_name, cmd, args = self.resolve_command(ctx, args)
  File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1061, in resolve_command
    cmd = self.get_command(ctx, cmd_name)
  File "/usr/local/lib/python2.7/dist-packages/platformio/__main__.py", line 43, in get_command
    ["cli"])
  File "/usr/local/lib/python2.7/dist-packages/platformio/commands/run.py", line 24, in <module>
    from platformio.commands.device import device_monitor as cmd_device_monitor
  File "/usr/local/lib/python2.7/dist-packages/platformio/commands/device.py", line 20, in <module>
    from serial.tools import miniterm
  File "/usr/lib/python2.7/dist-packages/serial/tools/miniterm.py", line 128, in <module>
    console.setup()
  File "/usr/lib/python2.7/dist-packages/serial/tools/miniterm.py", line 109, in setup
    self.old = termios.tcgetattr(self.fd)
error: (25, 'Inappropriate ioctl for device')

============================================================

An unexpected error occurred. Further steps:

* Verify that you have the latest version of PlatformIO using
  `pip install -U platformio` command

* Try to find answer in FAQ Troubleshooting section
  http://docs.platformio.org/page/faq.html

* Report this problem to the developers
  https://github.com/platformio/platformio-core/issues

============================================================

Any thoughts?

EDIT: I should also mention that in the Flask code I am using the Subprocess package to run the Platformio commands. If I manually run these lines in a Python terminal they execute as expected.

After a bit more debugging I can confirm the issue is with the termios.tcgetattr(self.fd) call in miniterm.py. If you look into the code, I can say that the self.fd = sys.stdin.fileno() call works properly so the issue does seem to be with the tcgetattr function.

The point is that this issue does not have to do with Platformio code. I’d welcome any feedback as I work through the issue (which seems to do with the above function call from my Flask back end), but I can now appreciate that the Platformio community board is not the most appropriate place for this question.

As I mentioned in my original post, I was able to run Platformio headless earlier in my development. Actually, the reason I moved to Platformio was because the Arduino command line function required an X11 server (at least at the time, ~ 1 year ago).

I downgraded my Platformio install to version 3.1.0 and my issues are resolved.

I am not sure if this would be considered a bug in newer releases as maybe Platformio is not designed to be used headless (or maybe it’s still user error).

Could you finally describe your issue?

I believe the issue is that Platformio v3.4.0 commands must be executed from an interactive terminal. One reason for this is that all(?) commands call the miniterm.py tool from the Pyserial package.

As such, it is not possible to use Platformio with headless processes (e.g. scheduled services, web back-end).

My solution was to downgrade to v3.1.0 which can be run without an interactive terminal.

Could you explain in details? Can’t understand why you downgraded PIO Core.

Hm… Please use PySerial API directly pySerial API — pySerial 3.0 documentation

I managed to run it via unbuffer (unbuffer pio run ...). See Pretend to be a tty in bash for any command - Stack Overflow