davef
Posts: 64
Joined: Wed Feb 20, 2013 8:39 pm
Location: Christchurch, NZ

Running Python script from crontab doesn't display

Sat Apr 18, 2020 8:01 pm

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.

pcmanbob
Posts: 10119
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Running Python script from crontab doesn't display

Sat Apr 18, 2020 8:09 pm

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
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

davef
Posts: 64
Joined: Wed Feb 20, 2013 8:39 pm
Location: Christchurch, NZ

Re: Running Python script from crontab doesn't display

Sun Apr 19, 2020 12:18 am

That did it, thanks.

pcmanbob
Posts: 10119
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Running Python script from crontab doesn't display

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

/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
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
thagrol
Posts: 3740
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Running Python script from crontab doesn't display

Sun Apr 19, 2020 11:31 am

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?
Arguing with strangers on the internet since 1993.

pcmanbob
Posts: 10119
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Running Python script from crontab doesn't display

Sun Apr 19, 2020 11:51 am

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.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”