LarsiParsii
Posts: 7
Joined: Tue Dec 29, 2020 3:11 pm

Have two background processes print to terminal

Tue May 11, 2021 10:36 am

I have an RPi running two scripts as daemons, that start on boot. The two scripts run without too many hiccups, but it would be helpful to see what they print, similar to what they do when I run them from the terminal. How can I output their prints to a terminal window, even though they are running in the background?

Thanks in advance! :)

User avatar
neilgl
Posts: 3244
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Have two background processes print to terminal

Tue May 11, 2021 10:46 am

When you start the script, direct its output to a file, like this

Code: Select all

python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1
Then look in neo.log later on.

LarsiParsii
Posts: 7
Joined: Tue Dec 29, 2020 3:11 pm

Re: Have two background processes print to terminal

Tue May 11, 2021 1:14 pm

neilgl wrote:
Tue May 11, 2021 10:46 am
When you start the script, direct its output to a file, like this

Code: Select all

python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1
Then look in neo.log later on.
Thanks for your quick reply!
It seems to work when i run it from the terminal, but when I try to run it as a daemon, no log file is created. Here's the service file, with your solution, if you're interested:

Code: Select all

[Unit]
Description=My service
After=network.target

[Service]
ExecStart=/usr/bin/python3 -u /home/pi/ProsjektPower.py >> /home/pi/Prosjekt/power.log 2>&1
WorkingDirectory=/home/pi/Prosjekt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

bls
Posts: 1450
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: Have two background processes print to terminal

Tue May 11, 2021 1:25 pm

LarsiParsii wrote:
Tue May 11, 2021 1:14 pm
neilgl wrote:
Tue May 11, 2021 10:46 am
When you start the script, direct its output to a file, like this

Code: Select all

python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1
Then look in neo.log later on.
Thanks for your quick reply!
It seems to work when i run it from the terminal, but when I try to run it as a daemon, no log file is created. Here's the service file, with your solution, if you're interested:

Code: Select all

[Unit]
Description=My service
After=network.target

[Service]
ExecStart=/usr/bin/python3 -u /home/pi/ProsjektPower.py >> /home/pi/Prosjekt/power.log 2>&1
WorkingDirectory=/home/pi/Prosjekt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target
Change StandardOutput and StandardError to be the files you want, rather than using redirection on the ExecStart line, which you should remove.
Pi tools:
Quickly and easily build customized-just-for-you SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

LarsiParsii
Posts: 7
Joined: Tue Dec 29, 2020 3:11 pm

Re: Have two background processes print to terminal

Tue May 11, 2021 5:56 pm

I've done as you said, but no files are created.

This is my current service file (I hope I did it correctly):

Code: Select all

[Unit]
Description=My service
After=network.target

[Service]
ExecStart=/usr/bin/python3 -u /home/pi/Prosjekt/Power.py
WorkingDirectory=/home/pi/Prosjekt
StandardOutput=/home/pi/Prosjekt/power.log
StandardError=/home/pi/Prosjekt/power_error.log
Restart=always
User=pi

[Install]
WantedBy=multi-user.target
And this is the output of the "sudo systemctl status power.service" command, if it is to any help:

Code: Select all

* power.service - My service
   Loaded: loaded (/etc/systemd/system/power.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-05-11 19:53:45 CEST; 2s ago
 Main PID: 1073 (python3)
    Tasks: 1 (limit: 2062)
   CGroup: /system.slice/power.service
           `-1073 /usr/bin/python3 -u /home/pi/Prosjekt/Power.py

May 11 19:53:45 prosjektpi systemd[1]: Started My service.

bls
Posts: 1450
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: Have two background processes print to terminal

Tue May 11, 2021 6:17 pm

What's in the output from sudo journalctl -b | grep power and/or is there anything you can find in sudo journalctl -xe'?
Pi tools:
Quickly and easily build customized-just-for-you SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

LarsiParsii
Posts: 7
Joined: Tue Dec 29, 2020 3:11 pm

Re: Have two background processes print to terminal

Tue May 11, 2021 9:47 pm

sudo journalctl -b | grep power gave me this. most entries dating to when I last messed around with the power.service file (editing it and reloading it):

Code: Select all

May 11 19:56:19 prosjektpi kernel: bcm2835-power bcm2835-power: Broadcom BCM2835 power domains driver
May 11 19:56:19 prosjektpi systemd[1]: /etc/systemd/system/power.service:7: Failed to parse output specifier, ignoring: /home/pi/Prosjekt/power.log
May 11 19:56:19 prosjektpi systemd[1]: /etc/systemd/system/power.service:8: Failed to parse output specifier, ignoring: /home/pi/Prosjekt/power_error.log
May 11 19:56:24 prosjektpi kernel: brcmfmac: brcmf_cfg80211_set_power_mgmt: power save enabled
May 11 19:56:29 prosjektpi systemd[1]: power.service: Main process exited, code=exited, status=1/FAILURE
May 11 19:56:29 prosjektpi systemd[1]: power.service: Failed with result 'exit-code'.
May 11 19:56:29 prosjektpi systemd[1]: power.service: Service RestartSec=100ms expired, scheduling restart.
May 11 19:56:29 prosjektpi systemd[1]: power.service: Scheduled restart job, restart counter is at 1.
May 11 19:56:29 prosjektpi bthelper[548]: Changing power off succeeded
May 11 19:56:30 prosjektpi bthelper[548]: Changing power on succeeded
May 11 19:56:32 prosjektpi systemd[1]: power.service: Main process exited, code=exited, status=1/FAILURE
May 11 19:56:32 prosjektpi systemd[1]: power.service: Failed with result 'exit-code'.
May 11 19:56:32 prosjektpi systemd[1]: power.service: Service RestartSec=100ms expired, scheduling restart.
May 11 19:56:32 prosjektpi systemd[1]: power.service: Scheduled restart job, restart counter is at 2.
sudo journalctl -xe gave me an output similar to what I expected to print, so that's really useful. However, the prints from my other script (Power.py) does not produce any logs (although it still runs normally).

bls
Posts: 1450
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: Have two background processes print to terminal

Tue May 11, 2021 9:57 pm

LarsiParsii wrote:
Tue May 11, 2021 9:47 pm

Code: Select all

May 11 19:56:19 prosjektpi systemd[1]: /etc/systemd/system/power.service:7: Failed to parse output specifier, ignoring: /home/pi/Prosjekt/power.log
May 11 19:56:19 prosjektpi systemd[1]: /etc/systemd/system/power.service:8: Failed to parse output specifier, ignoring: /home/pi/Prosjekt/power_error.log
Problem seems pretty obvious...

Does the above-named directory exist? If not, create it. Do the files exist? Are they owned or writeable by User=pi?
Pi tools:
Quickly and easily build customized-just-for-you SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

User avatar
jojopi
Posts: 3466
Joined: Tue Oct 11, 2011 8:38 pm

Re: Have two background processes print to terminal

Tue May 11, 2021 10:10 pm

StandardOutput=inherit did not mean to write to a file named "inherit", so we might expect to need some additional syntax when we do want to provide a literal file name.

man systemd.exec says:

Code: Select all

       StandardOutput=
           Controls where file descriptor 1 (STDOUT) of the executed processes
           is connected to. Takes one of inherit, null, tty, journal, syslog,
           kmsg, journal+console, syslog+console, kmsg+console, file:path,
           append:path, socket or fd:name.

bls
Posts: 1450
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: Have two background processes print to terminal

Tue May 11, 2021 10:59 pm

jojopi wrote:
Tue May 11, 2021 10:10 pm
StandardOutput=inherit did not mean to write to a file named "inherit", so we might expect to need some additional syntax when we do want to provide a literal file name.

man systemd.exec says:

Code: Select all

       StandardOutput=
           Controls where file descriptor 1 (STDOUT) of the executed processes
           is connected to. Takes one of inherit, null, tty, journal, syslog,
           kmsg, journal+console, syslog+console, kmsg+console, file:path,
           append:path, socket or fd:name.
Good catch. Obviously I neglected to check that. Well, actually, I meant to leave that for OP to sort out. Yea, that's the ticket :lol:
Pi tools:
Quickly and easily build customized-just-for-you SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

LarsiParsii
Posts: 7
Joined: Tue Dec 29, 2020 3:11 pm

Re: Have two background processes print to terminal

Wed May 12, 2021 1:18 am

Thank you so much, both of you! I eventually got it to work by adding the file: prefix.

Don't expect me to figure even the basics out :P , I'm a beginner in regards of RPi and Linux in general, so I try to learn by doing (and googling). Also, is there a way to display the text being sent to the log in real time, without the need of reopening the log file (similar to how a program normally prints to a terminal)?

bls
Posts: 1450
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA

Re: Have two background processes print to terminal

Wed May 12, 2021 3:10 am

LarsiParsii wrote:
Wed May 12, 2021 1:18 am
Thank you so much, both of you! I eventually got it to work by adding the file: prefix.

Don't expect me to figure even the basics out :P , I'm a beginner in regards of RPi and Linux in general, so I try to learn by doing (and googling). Also, is there a way to display the text being sent to the log in real time, without the need of reopening the log file (similar to how a program normally prints to a terminal)?

Code: Select all

tail -f filename
will display the lines of text added to a file in real time.
Pi tools:
Quickly and easily build customized-just-for-you SSDs/SD Cards: https://github.com/gitbls/sdm
Easily run and manage your network's DHCP/DNS servers on a Pi: https://github.com/gitbls/ndm
Easy and secure strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

GlowInTheDark
Posts: 1713
Joined: Sat Nov 09, 2019 12:14 pm

Re: Have two background processes print to terminal

Wed May 12, 2021 4:12 am

tail -f filename
will display the lines of text added to a file in real time.
You are all making this problem so much more difficult than it has to be.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Loves Linux; loves to dance.

LarsiParsii
Posts: 7
Joined: Tue Dec 29, 2020 3:11 pm

Re: Have two background processes print to terminal

Sun May 16, 2021 12:18 am

GlowInTheDark wrote:
Wed May 12, 2021 4:12 am
tail -f filename
will display the lines of text added to a file in real time.
You are all making this problem so much more difficult than it has to be.
Ok.

Return to “Beginners”