2

Pfiew that was a long question...

So, during bootup, I am starting on the background a python script (it gets called in the launcher).

sh /home/pi/launcher.sh&

When I ssh to the rpi I can see the python running "top -d 1". That python script runs forever and it outputs some print() messages which of course I can't see when I ssh because the app is in the background. To overcome this, I have to pkill python and then run the script "my-self" on my shell...

To put it in the background again I can either kill the app and restart the rpi or run it myself with the & in the end and then simply exit the shell...

The question is, is there a way to grab that proc from the background, without killing it (we can stop/pause it), and then after I finish looking at it and the messages it prints, send it back to the background and close my shell?

Edited: To put this in perspective, we are talking about industrial environment(s) with 20+ devices per customer. The python script is made by me and in those 4k lines of code I have various print() messages. Ofc those by them selves don't spamm the screen, but, in one certain setup of those devices there is serial communications between my devices and the big machine they "sit" on. That machine is sending serial data every 1-2 seconds, which I print() and also based on those data I do stuff. Normally I don't "care" that much about that serial data, but if there is a problem I need to "see" them "live". There were alot of suggestions and thank you for that. Since I am afraid to write to much stuff on the sd card over and over again, I could always use the "ramdrives" I have in my fstab and create the files there. I mainly use those "ramdrives" because my image is readonly and some "stuff" like samba need to be able to write somewhere.

Edit2: I am currently exploring "import logging" and writing that file to the aforementioned ramdrives. Tried changing the stdout but failed miserably. I believe because I have many threads running and also it doesn't seem like a good practice based on the comments. f = open('output.txt','w') sys.stdout = f

1
  • sounds like a job for tmux Commented Mar 11, 2023 at 20:33

3 Answers 3

1

The way I did something similar is to script starting the process within screen.

screen can be attached and detached inside your ssh connection, to access the program running.

SESSION_NAME=name
NL=$(echo -ne '\015')
screen -S "$SESSION_NAME" -d -m -U -A
screen -S "$SESSION_NAME" -X screen 1
screen -S "$SESSION_NAME" -p 1 -X stuff "sh /home/pi/launcher.sh$NL"

Then from ssh session:

screen -xRR

to connect.

tmux is newer than screen, but I haven't setup scripting with tmux.


Another option might be to make the program log to a file, then you can review the file to check the output.


Another thing that can be done is to use reptyr to take ownership of a running process. However this is rather hacky - using ptrace. And it leaves the command attached to your ssh connection so will get SIGHUP when you disconnect. Best only used as an emergency tool to connect a process to a screen or tmux session.

3
  • Thank you mate for the reply. Yeah I have logs enabled for "important" stuff. It's just that on the serial channel I am getting a string every 1 or 2 seconds so writing every couple of seconds is not really an option... I already commit=120 in fstab... now that I mentioned it, in theory I could.. but no matter what I would have to change all those print() commands to debugPrint() or something... I will try the screen thingy and will let you know :)
    – papatrexas
    Commented Feb 24, 2023 at 14:17
  • Yeap, that works.. Thanks a lot. I marked it up but I will wait for (if there are) any more answers because I am wondering if there is a way to do it without having to install screen (offline RPIs) and change the way they already load the python script.
    – papatrexas
    Commented Feb 24, 2023 at 14:30
  • There are hacky ways to take control existing processes - see serverfault.com/a/272526/85 - but they also need new programs installed. Commented Feb 24, 2023 at 16:14
0

Use tail -f

Change your startup script to

sh /home/pi/launcher.sh > /home/pi/launcher.log &

Then when you log in, just enter

tail -f /home/pi/launcher.log

The tail command show the last few lines of the file. The -f switch causes tail to not stop at the end of the file, but to wait for more data.

4
  • I don't know if I am scared for nothing, but... these devices are printing 5 lines every 1~2 seconds. I am really worried that if I put those in a txt file I will be really impacting the life of the sd card. I am using industrial grade sd cards, I am writing to the card every 2 minutes (through the fstab options), but still, I am really concerned. Are my fears justified?
    – papatrexas
    Commented Mar 8, 2023 at 15:49
  • I can't reassure you about the life of the SD card. Why not raise another question? (Not here, as it's not Pi-specific - maybe on Superuser.) I take an image backup of my SD cards, so I have something to go back to, should the card fail.
    – Peter bill
    Commented Mar 9, 2023 at 16:19
  • It seems to me that you have other problems, like how do you read so many lines so quickly. Do you have access to the Python script? If so, you may be able to limit the messages to just the important lines. Do you know about stdout and stderr, and how they separate error and info messages?
    – Peter bill
    Commented Mar 9, 2023 at 16:27
  • Please check my edited "Question" that explains the environment. It was to big to put it as a reply to the comment.
    – papatrexas
    Commented Mar 10, 2023 at 15:59
0

Use an environment variable

As you control the source code, you have more options. I would consider an environment variable, declared like:

export my_debug=0

In the Python code, for output you only rarely need, only execute the print() if $my_debug is not zero. Remember to retrieve the variable regularly, say every time round the 'main loop'.

Then, when you need the extended output, you can simply set $my_debug to one. Of course, this must be done in the same shell as started the process, so you would need @DouglasLeeder's answer too.

The benefit is your output file is much smaller, not containing data that will never be read.

I'm not a Python programmer, but have done this many times in C code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.