Page 1 of 1

Running Python script from crontab doesn't display

Posted: Sat Apr 18, 2020 8:01 pm
by davef
Re-writing some Bash scripts in Python, the following Bash script:

Code: Select all

echo "test" >> /dev/tty1

displays on the console when run manually in /home AND when the Bash
script is called by sudo crontab.

However, if I use this Python script:

Code: Select all

#!/usr/bin/python3

import sys

sys.stdout.write("test")
sys.stdout.flush()
it displays "test" when run from /home BUT not when the Python script
is called by sudo crontab

I have tried user crontab and it still doesn't work. There seems to be
something different when running a Python script from crontab.

Appreciate any clues.

Re: Running Python script from crontab doesn't display

Posted: Sat Apr 18, 2020 8:09 pm
by pcmanbob
Anything run by Cron is run in the background, so no screen output.

Your first shell command works because you are directing the output to the screen.

So to make you python program display on the screen you need to do the same.

For example

@reboot python3 /home/pi/my program.py >> /dev/tty1

Re: Running Python script from crontab doesn't display

Posted: Sun Apr 19, 2020 12:18 am
by davef
That did it, thanks.

Re: Running Python script from crontab doesn't display

Posted: Sun Apr 19, 2020 10:38 am
by pcmanbob
Really if you want to run you python program with output to the display you should be using the GUI auto start file

/etc/xdg/lxsession/LXDE-pi/autostart

So you would access if from the command line like this

Code: Select all

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
and add you start line for your python program like this

Code: Select all

@python3 /home/pi/your_script.py

Re: Running Python script from crontab doesn't display

Posted: Sun Apr 19, 2020 11:31 am
by thagrol
pcmanbob wrote:
Sun Apr 19, 2020 10:38 am
Really if you want to run you python program with output to the display you should be using the GUI auto start file
Doesn't that only apply if you're both using the GUI and logged in? And won't it start a new copy of the program whenever anyone logs in whether on the console or via vnc?

Re: Running Python script from crontab doesn't display

Posted: Sun Apr 19, 2020 11:51 am
by pcmanbob
thagrol wrote:
Sun Apr 19, 2020 11:31 am
Doesn't that only apply if you're both using the GUI and logged in? And won't it start a new copy of the program whenever anyone logs in whether on the console or via vnc?
Yes to the first question .

probably yes to the first part of question two , don't know about VNC not tried it.