eddieprice7
Posts: 9
Joined: Wed Dec 05, 2018 10:11 am

run script at startup

Fri May 24, 2019 2:36 pm

I have searched through the forum for ideas on how to get a script to run at startup, but everyone seems to have their own approach and I find it very confusing. I tried using rc.local. I edited rc.local and entered the following before the exit 0 line :-

sudo python3 /home/pi/train-controller/train-display.py &

I checked the path by entering :-

cat /home/pi/train-controller/train-display.py

and was able to view the script.

I switch on and nothing happens. The TFT display illuminates, but that only proves that the back light is working. I should say that it runs fine when attached to a usb display. Am I missing something when I edit rc.local? I did see one reference that suggested selecting 'autostart' in raspi-config, but my version of rasbian does not have this.

Andyroo
Posts: 2987
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: run script at startup

Fri May 24, 2019 4:08 pm

A few question to help folk here:

1) Does your script require a network?
2) Does the script require the desktop and the user to be logged in?
3) Does you script need to run under SUDO?
4) what do you mean by TFT display vs USB display?
5) Will anyone need to interact with this (eg read the screen / enter info / use the mouse on the screen etc) or will this just run in the background (so no one sees the program and can use the Pi)
6) What operating system and version are you using?
Need Pi spray - these things are breeding in my house...

dl324
Posts: 114
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: run script at startup

Fri May 24, 2019 4:51 pm

eddieprice7 wrote:
Fri May 24, 2019 2:36 pm
I have searched through the forum for ideas on how to get a script to run at startup, but everyone seems to have their own approach and I find it very confusing. I tried using rc.local.
Having been a Unix sysadmin, my preference is to advise Users to do things in a ways that don't require root privileges.

My suggestion is that you start the script in your crontab using the @reboot option to run the command at system startup.

Add this to your crontab:

Code: Select all

@reboot sudo python3 /home/pi/train-controller/train-display.py &

Andyroo
Posts: 2987
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: run script at startup

Fri May 24, 2019 4:57 pm

Depending on whats needed the OP may have to add a sleep 60 && to the start of the command to give the OS time to boot.

systemd (hate it or not) is a better way to control when things start but we really need answers from the OP
Need Pi spray - these things are breeding in my house...

eddieprice7
Posts: 9
Joined: Wed Dec 05, 2018 10:11 am

Re: run script at startup

Sat May 25, 2019 12:39 pm

Couldn't get it to work with rc.local, but managed with crontab as follows:

cd to program folder

sudo crontab -e

add a line to bottom of crontab script as :-

@reboot python3 /home/pi/folder/myprogram.py &

save changes with Ctrl X and Y

I must admit that I do not really understand what is happening, but when I power up the program runs.

User avatar
rpdom
Posts: 14173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: run script at startup

Sat May 25, 2019 12:43 pm

eddieprice7 wrote:
Sat May 25, 2019 12:39 pm
@reboot python3 /home/pi/folder/myprogram.py &
Just to say you don't need the "&" on the end of the line when used in crontab, only if used in rc.local
Signature is on holiday.

dl324
Posts: 114
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: run script at startup

Sat May 25, 2019 4:50 pm

eddieprice7 wrote:
Sat May 25, 2019 12:39 pm
I must admit that I do not really understand what is happening, but when I power up the program runs.
Try reading the manpage on cron. If you have questions about crontab syntax, read the manpage for crontab(5) (i.e. man 5 crontab).

The number in () is the manual number. It's used when a command appears in multiple manuals. If a command exists in multiple manuals, and manual number isn't specified, the command in the lower numbered manual is displayed. The manpage for cron lists related commands in the "SEE ALSO" section at the bottom of the "page".

RDS
Posts: 710
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: run script at startup

Sat May 25, 2019 10:01 pm

Crontab works well for me.

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

Re: run script at startup

Sun May 26, 2019 10:56 am

+1 and if you mess up the cron line in crontab it does not prevent your pi from booting so you can get back in to crontab and fix your error , unlike rc.local were a messed up line can lock you out of your pi.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

dl324
Posts: 114
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: run script at startup

Sun May 26, 2019 2:56 pm

eddieprice7 wrote:
Sat May 25, 2019 12:39 pm
Couldn't get it to work with rc.local, but managed with crontab as follows:

cd to program folder
You didn't need to 'cd' to the program directory. You either give the full path to the script as you've done, or you modify the PATH variable in crontab.
sudo crontab -e
This accomplishes the same thing as executing the command with sudo in your crontab. Since user=pi doesn't have to enter a password for sudo, I think it's cleaner if you put user stuff in a user's crontab.

User avatar
rpdom
Posts: 14173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: run script at startup

Sun May 26, 2019 4:18 pm

dl324 wrote:
Sun May 26, 2019 2:56 pm
eddieprice7 wrote:
Sat May 25, 2019 12:39 pm
Couldn't get it to work with rc.local, but managed with crontab as follows:

cd to program folder
You didn't need to 'cd' to the program directory. You either give the full path to the script as you've done, or you modify the PATH variable in crontab.
That depends on if the program uses relative paths for any files it might access.
Signature is on holiday.

dl324
Posts: 114
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: run script at startup

Sun May 26, 2019 4:23 pm

rpdom wrote:
Sun May 26, 2019 4:18 pm
That depends on if the program uses relative paths for any files it might access.
Still wouldn't make a difference. The current working directory has no effect on entries put in crontab and crontab has it's own definition for PATH.

User avatar
rpdom
Posts: 14173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: run script at startup

Sun May 26, 2019 4:46 pm

dl324 wrote:
Sun May 26, 2019 4:23 pm
rpdom wrote:
Sun May 26, 2019 4:18 pm
That depends on if the program uses relative paths for any files it might access.
Still wouldn't make a difference. The current working directory has no effect on entries put in crontab and crontab has it's own definition for PATH.
So this crontab entry wouldn't work

Code: Select all

0 5 * * * cd /home/rpdom/videos;/home/rpdom/crons/mycron.sh
with this code

Code: Select all

#!/bin/bash

# /home/rpdom/crons/mycron.sh - example cron script

ls -l >/tmp/videolist.txt
ls -l ../photos >/tmp/photolist.txt
Signature is on holiday.

dl324
Posts: 114
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: run script at startup

Sun May 26, 2019 5:43 pm

rpdom wrote:
Sun May 26, 2019 4:46 pm
So this crontab entry wouldn't work

Code: Select all

0 5 * * * cd /home/rpdom/videos;/home/rpdom/crons/mycron.sh
non sequitur

eddieprice7
Posts: 9
Joined: Wed Dec 05, 2018 10:11 am

Re: run script at startup

Fri May 31, 2019 9:49 am

Interesting replies.

I have previously used MPLAB from Microchip to program controllers for my model railway, and these run from the moment power is applied. Is it possible to use a Raspberry Pi in the same way? At the moment I have to wait about 60 secs or so, when I assume that Rasbian is being loaded, and it is only when that is loaded that it can run the pyrhon script. Can an RPi be used to run python straight away?

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

Re: run script at startup

Fri May 31, 2019 2:29 pm

eddieprice7 wrote:
Fri May 31, 2019 9:49 am
Interesting replies.

I have previously used MPLAB from Microchip to program controllers for my model railway, and these run from the moment power is applied. Is it possible to use a Raspberry Pi in the same way? At the moment I have to wait about 60 secs or so, when I assume that Rasbian is being loaded, and it is only when that is loaded that it can run the pyrhon script. Can an RPi be used to run python straight away?
No the pi is a fully fledged pc so it requires an operating system ( linux ) its the operating system that is loading during the 60 seconds you are waiting before you can run python programs.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
rpdom
Posts: 14173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: run script at startup

Fri May 31, 2019 4:07 pm

As pcmanbob said, the Pi is a full computer, not a dedicated chip.

The big difference is that the code gets burnt into the PIC microcontroller's memory so that when it gets a power-on reset the first thing it does is to start running that raw code at the first instruction. It doesn't have to do anything else. With the Pi it has to load the internal bootloader, search for a boot device (SD card/USB/Network or other), load the initial bootcode.bin and run that on the Videcore GPU, look for the appropriate start.elf, load that and run it, look for a kernel file, load that and run it on an ARM CPU, that then scans the hardware to work out what computer it is running on and sets things up, then it starts running the various background tasks required to run the system including some form of log in, GUI or command line.

It is possible to get the Pi to boot faster, one of the fastest methods would be to replace Linux with your own OS (Bare Metal). That is a lot of work and you'd need to write or find code to drive the various bits of hardware you need.

There are some projects that will boot faster, but you will still have a restricted system.
Signature is on holiday.

Return to “Beginners”