Products > Computers
Creating a background process on RPi / Linux
John B:
I've been having a lot more difficulty with this than I thought would be the case. Basically I would like a python script to start when rebooting the RPi. More specifically I need a few bash commands to run, ie running the python script in a virtual environment. The script also has stdin/stdout/stderr functionality, so I've been manually running it within a TTY.
The first idea is using crontab to create a startup event where it will run a few bash commands on boot. However I can't seem to figure whether it's possible to run a command in one TTY or desktop, and have that command run in another TTY as if it had been run in that TTY. I can manage to print stdout from one TTY to another one, but that doesn't give the same stdin/stderr functions and doesn't take exclusive control over the TTY process. So to clarify, is there a way to run a bash command in another TTY (assuming things like it's already logged in)?
The second idea is to create a systemd process, however I will run into a similar issue, though I think I can attach a TTY to a systemd process.
abeyer:
I'm a little confused as what you're trying to do, as what you're saying sounds like the exact opposite of normal background process behaviour on linux: normally part of your daemonizing steps if you do that yourself is to explicitly detach yourself from a tty if you have one. (the NOTTY ioctl)
The systemd setup is probably the right direction... but again, expecting a tty is not the normal approach, but rather something that is intended to run the in background should handle whatever it's given as stdin/stout/stderr and let systemd handle that.
abeyer:
also, as to your specific question (even though I suspect it's not the right approach) you might look at https://github.com/nelhage/reptyr
Nominal Animal:
If you want to run an interactive service that you can "connect" to, install the screen package, so you can use screen to create a new session and run your script in detached mode, with something like
setpriv --reuid user --init-groups --reset-env screen -d -m -S sessionname python3 /path/to/script.py
in your init script. This command starts the script as a separate process as user user, and returns immediately.
Afterwards, user user can run
screen -r sessionname
to reconnect to the script, from any terminal; even from an SSH connection. To detach from the script, press Ctrl+A then Ctrl+D.
screen and other similar utilities work by creating pseudoterminals when needed. These are character device pairs, a slave (which acts basically indistinguishably from a TTY device), and a master (that controls the input and output to the slave). In your particular case, Linux pts support, also known as UNIX 98 pseudoterminal support, will be used and the character devices reside under /dev/pts/.
You can also run a script or program using a specific console terminal using openvt as root:
openvt -c 8 -- python3 /path/to/script.py
or
openvt -c 8 -- setpriv --reuid user --init-groups --reset-env python3 /path/to/script.py
This starts the Python script connected to /dev/tty8 as root (first) or user user (second), but without changing the ownership of the tty device. It will fail if the tty is already in use. These too will start the script in a separate process, and return immediately, without waiting for the script to complete.
You can switch the active terminal using e.g. Ctrl+Alt+F8, or running chvt 8, but only from the console terminal, and not when connecting using e.g. SSH.
This kind of stuff is normally done only when you replace the entire init system, though; with say passwordless login shells in a couple of TTYs, but one or more primary ones providing some kind of a text-based full-screen interface (TUI), so that the user can switch between them using Ctrl+Alt+Fn or Ctrl+Alt+◀ and Ctrl+Alt+▶, but only from the local console terminal, not over SSH etc.
John B:
Thanks, I guess describing it as a background process is a little confusing. Put another way, when I start the RPi, I'll manually enter bash ./startscript.sh into tty1. The sh script then switches to the virtual environment and starts the python script with parameters. I would just like to automate that so it happens on reboot.
The script is a serial/i2c/mqtt bridge, and is supposed to run all the time, weeks or months at a time. I think of it in terms of background processes, as for a while RPi has had a bug where going into screen power saving mode could crash the display server, and therefore kill any processes in that desktop environment. The ttys seemed to be unaffected by that crashing. It may be fixed now with RPi moving to a wayland based system, but separating the script from the usual desktop environment seems like a good idea.
I will give openvt another try. I think I tried that but maybe I was unsuccessful in trying to reference an existing terminal
Navigation
[0] Message Index
[#] Next page
Go to full version