I would like to run a python script wich is logging a nmea GPS stream from UART for a drone. And I want to see my scripts output in the terminal. The script starts just fine with the folowing setup:
/etc/systemd/system/gps_log.service
[Unit]
Description=Startup Script Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/home/pi/serial_nmealog.sh
WorkingDirectory=/home/pi
User=pi
[Install]
WantedBy=multi-user.target
/home/pi/serial_nmealog.sh:
#!/bin/sh
sleep 10
sudo python3 serial_nmealog.py
Now I want to create a new terminal with screen or tmux to see what my script ist doing. I use the following line in the serial_nmealog.sh file:
sudo screen -dmS gpslog python3 serial_nmealog.py
or
sudo tmux new -s gpslog python3 serial_nmealog.py
But the service is always stops running. When I type sudo systemctl status gps_log.service
I get:
for sudo screen -dmS gpslog python3 serial_nmealog.py
in
serial_nmealog.sh
●くろまる gps_log.service - Startup Script Service
Loaded: loaded (/etc/systemd/system/gps_log.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sun 2018年03月04日 11:42:12 UTC; 1min 37s ago
Process: 1373 ExecStart=/home/pi/serial_nmealog.sh (code=exited, status=0/SUCCESS)
Main PID: 1373 (code=exited, status=0/SUCCESS)
Mar 04 11:42:01 raspberrypi systemd[1]: Started Startup Script Service.
Mar 04 11:42:12 raspberrypi sudo[1378]: pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/screen -dmS gpslog python3 serial_nmealog.py
Mar 04 11:42:12 raspberrypi sudo[1378]: pam_unix(sudo:session): session opened for user root by (uid=0)
Mar 04 11:42:12 raspberrypi sudo[1378]: pam_unix(sudo:session): session closed for user root
for sudo tmux new -s gpslog python3 serial_nmealog.py
in serial_nmealog.sh
●くろまる gps_log.service - Startup Script Service
Loaded: loaded (/etc/systemd/system/gps_log.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sun 2018年03月04日 11:24:17 UTC; 1s ago
Process: 1227 ExecStart=/home/pi/serial_nmealog.sh (code=exited, status=0/SUCCESS)
Main PID: 1227 (code=exited, status=0/SUCCESS)
Mar 04 11:24:07 raspberrypi systemd[1]: Started Startup Script Service.
Mar 04 11:24:17 raspberrypi sudo[1241]: pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/tmux new-session -s pi -d gpslog python3 serial_nmea
Mar 04 11:24:17 raspberrypi sudo[1241]: pam_unix(sudo:session): session opened for user root by (uid=0)
Mar 04 11:24:17 raspberrypi sudo[1241]: pam_unix(sudo:session): session closed for user root
I also tried to do it with rc.local at startup. But it does not work. What am I missing and how can I make it possible to establish an new terminal session with which I can interact via ssh?
-
You can’t use a serial port twice. This means you can only have one instance of that script running at a time. Logging to stdout will make it available to journalctl where you can see the outputJohn Keates– John Keates2018年03月04日 15:31:24 +00:00Commented Mar 4, 2018 at 15:31
1 Answer 1
Modify /home/pi/serial_nmealog.py so that it logs to syslog. You can configure syslog so that serial_nmealog has its own log file, if you want. Then you can connect with ssh and tail that logfile. There is no way to "interact" with serial_nmealog.py unless it has itself made provisions for that.
BTW, I think you can safely drop the sudo from /home/pi/serial_nmealog.sh, as far as I can see, you're already root.
-
Ditto that the use of
sudo
in a boot service is at best redundant. They run with root privileges unless they explicitly set something else.goldilocks– goldilocks2018年03月04日 15:11:07 +00:00Commented Mar 4, 2018 at 15:11 -
@goldilocks thanks, I wasn't sure since I try to keep away from systemd.Gerard H. Pille– Gerard H. Pille2018年03月04日 15:12:19 +00:00Commented Mar 4, 2018 at 15:12
-
Thanks thats what I needed. I cant use some parts of the script now but at least I can see what it is doing!Hans Jürgen– Hans Jürgen2018年03月04日 16:03:44 +00:00Commented Mar 4, 2018 at 16:03
-
I also added the line
tail -f -n 1 /var/log/syslog
to .bashrc to see my what is happening when I am using ssh to log in. Thanks!Hans Jürgen– Hans Jürgen2018年03月04日 16:16:27 +00:00Commented Mar 4, 2018 at 16:16 -
You wrote serial_nmealog.py yourself?Gerard H. Pille– Gerard H. Pille2018年03月04日 16:26:41 +00:00Commented Mar 4, 2018 at 16:26