Hello I'm trying to make a linux ubuntu arm64 service in python. My problem is quite simple. I called the service 'bob'
My code for the service is:
import time
for i in range(0,99999999):
print("hello", i)
time.sleep(5)
systemctl restart bob
systemctl status bob
What i want to see is the information about the active service and the service saying hello [number] every 5 seconds but instead it shows nothing but the information about the active service 'working correctly'.
When i try it without the sleep function:
import time
for i in range(0,99999999):
print("hello", i)
It works perfectly fine. Both status and 'hello 41234'. But with it, it doesn't. No errors show up anywhere and the cpu seems to be used by the service but no output.
EDIT: Make sure to not spam your linux server ith a command that prints information too much. Every message is contained in at least 2 places in system logs that are cleaned every so often (from days to weeks depending on system). Doing so will cause the logs to be really big. And because they are autocleaned so rarely, you're left with less disk space until next clean. in my case just turning the program on a few times for a few seconds resulted in 100% disk space filled.
CodePudding user response:
You should use sys.stdout.flush()
to flush the buffer and print the things to the terminal.
E.g,
import sys
import time
for i in range(10):
print(i, end =' ')
sys.stdout.flush()
time.sleep(1)
Without sys.stdout.flush()
, it will wait until the 10th second and at that time, it will print everything to the terminal.
More details here