timothyy
Posts: 22
Joined: Thu Aug 08, 2019 9:20 am

Headless RasPi running Python script

Mon Sep 23, 2019 5:05 pm

Hi,

I have a headless Raspberry 3 which i am planning to put in a exhibition for 2 months. So, how to tell the headless Raspberry Pi to run a python script that i wrote at the beginning and keep it running without stopping for 2 months (yes, no monitor)?


Timothy

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

Re: Headless RasPi running Python script

Mon Sep 23, 2019 5:22 pm

Seeing as its headless I guess there is no output to a display required so you could just use cron to run your python program at boot .

so using the command

Code: Select all

crontab -e
at the command line

add this line to the end of the displayed lines

Code: Select all

@reboot python3 /home/pi/myprogram.py
this assumes you program is running using python 3, the program is saved in the /home/pi directory and the file is called myprogram.py,

you can change any of those if you need to use another directory or a different file name.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Kendek
Posts: 145
Joined: Thu Jul 25, 2019 4:39 pm
Location: Kaposvár, Hungary

Re: Headless RasPi running Python script

Mon Sep 23, 2019 5:25 pm

/etc/rc.local

Code: Select all

while : ; do script.py ; done &
So the script will be restarted immediately even if exited unexpectedly.

jbudd
Posts: 1143
Joined: Mon Dec 16, 2013 10:23 am

Re: Headless RasPi running Python script

Mon Sep 23, 2019 6:26 pm

You can set up a systemd service so that it automatically restarts.

Do you have any way to verify that the pi itself stays alive unattended, eg the hardware watchdog, sending occasional "I'm still alive" messages etc.?

timothyy
Posts: 22
Joined: Thu Aug 08, 2019 9:20 am

Re: Headless RasPi running Python script

Fri Sep 27, 2019 1:45 pm

Hi, i also got a writetotxtfile("something","text1.txt"), everytime i reboot it the data doesn't write to the textfile. Any help?

User avatar
B.Goode
Posts: 9594
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Headless RasPi running Python script

Fri Sep 27, 2019 2:43 pm

timothyy wrote:
Fri Sep 27, 2019 1:45 pm
Hi, i also got a writetotxtfile("something","text1.txt"), everytime i reboot it the data doesn't write to the textfile. Any help?

In which directory(ies) are you looking for "text1.txt" ?

Hint: it probably will not be written in the Home directory for the default pi user.

timothyy
Posts: 22
Joined: Thu Aug 08, 2019 9:20 am

Re: Headless RasPi running Python script

Fri Sep 27, 2019 2:45 pm

/home/pi/Desktop/Testing/text1.txt

User avatar
B.Goode
Posts: 9594
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Headless RasPi running Python script

Fri Sep 27, 2019 2:48 pm

timothyy wrote:
Fri Sep 27, 2019 2:45 pm
/home/pi/Desktop/Testing/text1.txt


"Hint: it probably will not be written in the Home directory for the default pi user."

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

Re: Headless RasPi running Python script

Fri Sep 27, 2019 2:53 pm

So how is the program writing to this text1.txt , is it accessing the file from within the proram or are you redirecting output from the file when you all it ?

How are you calling the file on boot.?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

timothyy
Posts: 22
Joined: Thu Aug 08, 2019 9:20 am

Re: Headless RasPi running Python script

Fri Sep 27, 2019 2:56 pm

Code: Select all

def writetotxtfile(texttowrite,filename):
    file = open(str(filename), 'a')
    file.write(str(datetime.datetime.now()) + " " + str(texttowrite) + '\n')
    file.close()
I call the file on boot is /home/pi/Desktop/Testing/Text1.txt

User avatar
B.Goode
Posts: 9594
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Headless RasPi running Python script

Fri Sep 27, 2019 3:03 pm

timothyy wrote:
Fri Sep 27, 2019 2:56 pm

Code: Select all

def writetotxtfile(texttowrite,filename):
    file = open(str(filename), 'a')
    file.write(str(datetime.datetime.now()) + " " + str(texttowrite) + '\n')
    file.close()
I call the file on boot is /home/pi/Desktop/Testing/Text1.txt


That will work as you expect if you run the script while you are the default pi user and /home/pi/Desktop/Testing/ is your Current Working Directory.
(Apart from the issue of the difference in case between text1 and Text1.)

But for a script started during the system start-up process neither of these things is the case.

Have a think about which user runs the system startup, and where its default filestore is kept...

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

Re: Headless RasPi running Python script

Fri Sep 27, 2019 3:05 pm

Yes but how are you calling the file on boot ?

are you using cron, rc.local or some other way.

which you use affect the environment in which you file is run, and were the output is sent.

your filename variable probable needs to have the full path in it as well as the file name, so the file goes to the directory you want.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

timothyy
Posts: 22
Joined: Thu Aug 08, 2019 9:20 am

Re: Headless RasPi running Python script

Fri Sep 27, 2019 3:08 pm

i typed this at the end of the command

Code: Select all

@reboot sudo python /home/pi/Desktop/Testing/Testing.py

User avatar
B.Goode
Posts: 9594
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Headless RasPi running Python script

Fri Sep 27, 2019 3:22 pm

timothyy wrote:
Fri Sep 27, 2019 1:45 pm
Hi, i also got a writetotxtfile("something","text1.txt"), everytime i reboot it the data doesn't write to the textfile. Any help?

Code: Select all

writetotxtfile("something","/home/pi/Desktop/Testing/Text1.txt")

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

Re: Headless RasPi running Python script

Fri Sep 27, 2019 3:31 pm

timothyy wrote:
Fri Sep 27, 2019 3:08 pm
i typed this at the end of the command

Code: Select all

@reboot sudo python /home/pi/Desktop/Testing/Testing.py
Do you really need to call this program with sudo ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jbudd
Posts: 1143
Joined: Mon Dec 16, 2013 10:23 am

Re: Headless RasPi running Python script

Fri Sep 27, 2019 5:32 pm

Does the directory /home/pi/Desktop/Testing with a capital T exist?
What does ls -l /home/pi/Desktop/Testing show you?

Return to “Advanced users”