User avatar
bill z
Posts: 64
Joined: Mon Jun 15, 2015 6:59 pm
Location: Spring, Texas, USA

Booting Up Headless

Mon Sep 21, 2020 4:21 pm

Sort of new user about this area.

Using the GUI environment, I wrote a Python program, tested and now ready to put it into a headless environment but uncertain as exactly how to do it. I reviewed the ‘Getting started with Raspberry Pi’ and the help guides ‘Configuration’ & ‘Remote Access’. I’m confused because it seems there are so many options. I'm not sure what to disregard and what to change.

What I want to do is to create a headless RPi3B+ running Raspbain lite and a clock module that will boot up my Python program and run it for ever (program has an internal loop). The program gets information parameters from a .txt file that it reads every 60 seconds, then closes. I want to be able to access and change the .txt file via wifi or blue tooth using a tablet or cell phone.

The Python program turns on and off relays and sets PWM lengths for LED lights so no GUI is required. I do have a log.txt file to be able to monitor what it did when. I would like to be able to look at this log file every now and then.

Please help.
Computers are fun but freely given relationships are the best!

User avatar
neilgl
Posts: 2543
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Booting Up Headless

Mon Sep 21, 2020 5:27 pm

I use pi’s cron (no sudo) to start programs on boot, e.g.: crontab -e

Code: Select all

@reboot python3 -u /home/pi/scripts/neo.py >> /home/pi/scripts/neo.log 2>&1

User avatar
bill z
Posts: 64
Joined: Mon Jun 15, 2015 6:59 pm
Location: Spring, Texas, USA

Re: Booting Up Headless

Mon Sep 21, 2020 8:25 pm

Neilgl,

I'm not sure what file to put this cron command in so it runs at boot.
Computers are fun but freely given relationships are the best!

User avatar
neilgl
Posts: 2543
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Booting Up Headless

Mon Sep 21, 2020 8:55 pm

Login as pi then type in

Code: Select all

crontab -e
. That will get you an editor (nano) to type the command into. Save via CTRL-O, CTRL-X.

User avatar
bill z
Posts: 64
Joined: Mon Jun 15, 2015 6:59 pm
Location: Spring, Texas, USA

Re: Booting Up Headless

Tue Sep 22, 2020 12:05 pm

Please know that I know how to use nano and how to use the cmd screen. What I do not know is what files to change so at boot my program runs.

Do I change the cmdline.txt or the config.txt or is there another?
Computers are fun but freely given relationships are the best!

User avatar
davidcoton
Posts: 5482
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Booting Up Headless

Tue Sep 22, 2020 12:21 pm

You make an entry in crontab, using the command listed above which invoked nano on the appropriate crontab.

All you need now is the crontab syntax -- which IDK offhand. Try man crontab or wait for a crontab expert. (No jokes about crontab experts being on time, please.)
Location: 345th cell on the right of the 210th row of L2 cache

User avatar
neilgl
Posts: 2543
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Booting Up Headless

Tue Sep 22, 2020 4:14 pm

Yes, so do that crontab -e, then add the line @reboot python3 ..... that I said earlier. Then when you reboot your pi it will start the script.

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

Re: Booting Up Headless

Tue Sep 22, 2020 5:51 pm

crontab has got to be about the worst possible answer to the perennial question: How do I auto-start tasks at boot?
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.

User avatar
neilgl
Posts: 2543
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Booting Up Headless

Tue Sep 22, 2020 8:20 pm

No it isn’t for non-GUI python scripts.

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

Re: Booting Up Headless

Tue Sep 22, 2020 8:24 pm

neilgl wrote:
Tue Sep 22, 2020 8:20 pm
No it isn’t for non-GUI python scripts.
I agree that crontab is not for starting Python scripts at boot-time.
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.

User avatar
neilgl
Posts: 2543
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Booting Up Headless

Tue Sep 22, 2020 9:55 pm

I always use it - it works perfectly for me, YMMV

pidd
Posts: 1258
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: Booting Up Headless

Tue Sep 22, 2020 10:13 pm

I normally put startup programs in /etc/rc.local, it is the sole purpose of that file, it is also the simplest solution.

User avatar
neilgl
Posts: 2543
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Booting Up Headless

Wed Sep 23, 2020 9:37 am

Ok

User avatar
bill z
Posts: 64
Joined: Mon Jun 15, 2015 6:59 pm
Location: Spring, Texas, USA

Re: Booting Up Headless

Sat Oct 03, 2020 2:23 pm

About booting with a program in cron or /etc/rc.local, when the RPi boots and the program is running, how does one stop the program should it need a modification?
Computers are fun but freely given relationships are the best!

User avatar
jahboater
Posts: 6265
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Booting Up Headless

Sat Oct 03, 2020 2:38 pm

My clock runs from /etc/rc.local (just before the exit 0)

Code: Select all

/usr/bin/tvservice -o
nice --adjustment=-20 /home/pi/clock.py &
The first line turns off hdmi to save a little power.
The nice bit increases the priority of the clock program in the (slight) hope it will improve the accuracy :)

You may log into the Pi in the usual way with ssh.
Then you can alter the clock script and reboot, or stop the python program with kill and restart it.
In fact you may happily use the Pi for other work whilst the clock is running!

To save a little bit more power, my Pi Zero clock runs happily with these entries in /boot/config.txt

Code: Select all

over_voltage=-4
arm_freq=600
gpu_mem=16
Pi4 8GB running PIOS64 Lite

pidd
Posts: 1258
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: Booting Up Headless

Sat Oct 03, 2020 4:08 pm

bill z wrote:
Sat Oct 03, 2020 2:23 pm
About booting with a program in cron or /etc/rc.local, when the RPi boots and the program is running, how does one stop the program should it need a modification?
I usually look for the existence of a certain file, you create the file and the program stops. Not always the best solution, I do it out of habit.

User avatar
neilgl
Posts: 2543
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Booting Up Headless

Sat Oct 03, 2020 4:29 pm

To stop the program comment out the line in crontab then reboot, edit & test, if ok uncomment the line in crontab.

User avatar
bill z
Posts: 64
Joined: Mon Jun 15, 2015 6:59 pm
Location: Spring, Texas, USA

Re: Booting Up Headless

Fri Oct 09, 2020 12:37 am

In order to boot up headless, I had to build a bootable headless raspbain lite OS.

I found this on the internet but I do not know how old it is plus, I can not get it to work.

It says 'In boot drive' but it doesn't say what directory to put the files in. I tried the root and the directory called boot but it still doesn't boot up accessing my wifi. Also, what is the country ID for USA?

Also, I have to change the keyboard to USA some how because some characters are not the same. What command can I use to do that?

Can some one help please?


Boot RPi Headless

download Raspbain lite
format SD chip
create bootable SD chip
In boot drive <-------- Here is where I can't find where to put the files to create
create 'ssh' or 'SSH' (no extention)
create wpa_supplicant.conf

edit wpa_supplicant.conf
country=ie <-- your country ID <-- What is the country ID for USA?
update_config=1
ctrl_interface=/var/run/wpa_supplicant

network={
scan_ssid=1
ssid="ssid" <-- Your network name
psk="password" <-- your network password
}

launch putty using the PI IP

Starting Python Program as your RPi boots up
identify file location of program to run
sudo crontab -e or edit bashrc file
choose option #2 nano
Add at the bottom of the file
@reboot python3 /home/pi/??/my_python_program
save
Computers are fun but freely given relationships are the best!

pidd
Posts: 1258
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: Booting Up Headless

Fri Oct 09, 2020 1:59 am

bill z wrote:
Fri Oct 09, 2020 12:37 am
I found this on the internet but I do not know how old it is plus, I can not get it to work.
Why are you using random, probably out of date information from the internet when this site has precise instructions and is fully up to date?

Have a look at the help button above and work from there.

cleverca22
Posts: 1821
Joined: Sat Aug 18, 2012 2:33 pm

Re: Booting Up Headless

Fri Oct 09, 2020 3:49 am

bill z wrote:
Sat Oct 03, 2020 2:23 pm
About booting with a program in cron or /etc/rc.local, when the RPi boots and the program is running, how does one stop the program should it need a modification?
thats why i recommend systemd instead
that lets you stop/start/restart the service, and configure it to auto-start on boot

User avatar
neilgl
Posts: 2543
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Booting Up Headless

Fri Oct 09, 2020 8:56 am

The bit where you have “sudo crontab -e” does not need sudo
To edit pi’s cron use crontab -e

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

Re: Booting Up Headless

Fri Oct 09, 2020 11:44 am

"sudo" means "please" in some foreign languages. And it is always good to be polite.

[moderator, no this is nonsense!]
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.

User avatar
davidcoton
Posts: 5482
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Booting Up Headless

Fri Oct 09, 2020 12:29 pm

GlowInTheDark wrote:
Fri Oct 09, 2020 11:44 am
"sudo" means "please" in some foreign languages. And it is always good to be polite.
But not when addressing crontabs. In this case the "sudo" changes the meaning and doesn't do the same thing (Hint: a different table is edited).
Location: 345th cell on the right of the 210th row of L2 cache

hippy
Posts: 8508
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Booting Up Headless

Fri Oct 09, 2020 1:05 pm

bill z wrote:
Fri Oct 09, 2020 12:37 am
In order to boot up headless, I had to build a bootable headless raspbain lite OS.

I found this on the internet but I do not know how old it is plus, I can not get it to work.
In order to boot up headless there is nothing more you should need to do than use the standard Raspberry Pi OS and not connect a monitor, keyboard or mouse.

That works with Raspberry Pi OS Lite, Desktop, and Desktop with Recommended Software.

Beyond configuring programs to automatically start at boot-up, and enabling some kind of remote access, such as SSH, there is nothing more you should have to do.

User avatar
bill z
Posts: 64
Joined: Mon Jun 15, 2015 6:59 pm
Location: Spring, Texas, USA

Re: Booting Up Headless

Fri Oct 09, 2020 1:47 pm

Please be kind to this beginner.

Previous quote: Beyond configuring programs to automatically start at boot-up, and enabling some kind of remote access, such as SSH, there is nothing more you should have to do.

Exactly what I'm asking how to do. How do I enable the OS to automatically logon to my wifi network and enable SSH at boot so I can access headless?

It was mentioned previously that creating a wpa_supplicant.conf file was OLD and systemd is recommended. I tried running sudo systemd and I get the error: 'Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.' . is systemd a GUI program?
Computers are fun but freely given relationships are the best!

Return to “Beginners”