Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 19:22 17 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Linux & pi

Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 07:05pm 19 Feb 2021
Copy link to clipboard 
Print this post

Does anyone know how to get a script running at bootup using crontab?
I have a program that works perfectly and it's called by ./programname.sh but I cannot get it to run on boot

I've tried
@reboot /usr/bin/sh  /home/pi/programname.sh &
@reboot /home/pi/programname.sh &
@reboot /home/pi/./programname.sh &
@reboot /home/pi/ ./programname.sh &
none starts the program on boot
I even tried
@reboot python /home/pi/./programname.sh &

Anyone know how to get this running on power up?

my crontab works because I have a script to reconnect the wifi if it loses connection and that works beautifully
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4147
Posted: 07:25pm 19 Feb 2021
Copy link to clipboard 
Print this post

For those who don't know, crontab is about things being run at specified times (cron being from the greek god Chronos). The times being somewhat akin to calendar entries.

Which user's crontab have you tried? I'm guessing a non-root one called pi.

If you've put the command(s) into a file called (say) mycrontab did you then issue the command
crontab mycrontab
?

You can check any time afterwards what cron knows using
crontab -l

I don't recall ever needing a trailing "&" so maybe that is the problem.

I also expect cron wants a normal Linux text file with normal line endings (i.e. newline not such as CR LF).

John
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 07:40pm 19 Feb 2021
Copy link to clipboard 
Print this post

  JohnS said  
Which user's crontab have you tried? I'm guessing a non-root one called pi.


I really don't understand that question  I used sudo crontab -e  then used nano as the editor

i followed the instructions here and checked many other pages on running programs on boot on the Pi
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3477
Posted: 07:50pm 19 Feb 2021
Copy link to clipboard 
Print this post

I put commands to be executed at power-up in /etc/rc.local

sudo nano /etc/rc.local

Give full path names to any commands you execute.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 08:29pm 19 Feb 2021
Copy link to clipboard 
Print this post

I've never got anything working in .etc/rc.local
aparently it doesn't work so well in the latest Buster and onwards thats why I was trying Crontab

My checkwifi program worked until I rebooted then didn't so I'm doing something wrong
 
hitsware2

Guru

Joined: 03/08/2019
Location: United States
Posts: 735
Posted: 08:43pm 19 Feb 2021
Copy link to clipboard 
Print this post

>> it doesn't work so well in the latest Buster

IMO Buster should be called Busted .....
Stick with Stretch as much as possible ...
my site
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 09:01pm 19 Feb 2021
Copy link to clipboard 
Print this post

I don't want to use Stretch
I have a reason for using the latest Pi OS
for those that actually know about Crontab any help in understanding why the program will not run on boot and why the 2nd one in the Crontab stops working after the pi is rebooted
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4147
Posted: 09:19pm 19 Feb 2021
Copy link to clipboard 
Print this post

  lew247 said  
  JohnS said  
Which user's crontab have you tried? I'm guessing a non-root one called pi.


I really don't understand that question  I used sudo crontab -e  then used nano as the editor

i followed the instructions here and checked many other pages on running programs on boot on the Pi

Using sudo means you're becoming the super user (aka root), so you are editing that user's crontab.

nano should create a normal text file.

Your problem could still be the trailing "&" char(s).

It could also be whatever you put in /home/pi/programname.sh

BTW You would NOT normally want root to run a user's script.  Instead you would normally want that user to run the script i.e. you'd edit the user's crontab instead of using sudo.

John
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3477
Posted: 09:50pm 19 Feb 2021
Copy link to clipboard 
Print this post

QUOTE=lew247]I've never got anything working in .etc/rc.local
aparently it doesn't work so well in the latest Buster and onwards that's why I was trying Crontab

Still works for me in /etc/rc.local in Buster;  cat /etc/os-release shows
VERSION="10 (buster)"
VERSION_CODENAME=buster

Don't know why it wouldn't work for you. I've never gotten "@boot" or the like to work in crontab (but not sure I've tried on a pi because /etc/rc.local has worked).

One gotcha I've seen in crontab--a script or program can run perfectly from the prompt, but fails in crontab because different ash/bash/etc. or different directory or whatever. Can be perplexing. Can you be sure that is it is not actually starting to run, but failing because of one of those issues?

Here's the relevant part of my /etc/rc.local (the "&" makes the program persist).

# following turns off history expansion
set +H
/home/pi/udplisten.sh&
/home/pi/udplisten16523.sh&
#/home/pi/udplisten44.sh&
/home/pi/tcplisten44.sh&
exit 0

And a grepped list of running programs:

> ps -A -f | grep listen
root       511     1  0  2020 ?        00:00:59 /bin/bash /home/pi/udplisten.sh
root       512     1  0  2020 ?        00:00:00 /bin/bash /home/pi/tcplisten44.sh
root       516   512  0  2020 ?        00:00:00 /bin/bash /home/pi/tcplisten44.sh
root      3246   511  0 17:56 ?        00:00:00 /bin/bash /home/pi/udplisten.sh
pi        3436  1596  0 17:58 pts/3    00:00:00 grep --color=auto listen
pi        4060     1  0  2020 ?        00:00:00 /bin/bash /home/pi/udplisten16523.sh
pi        4062  4060  0  2020 ?        00:00:00 /bin/bash /home/pi/udplisten16523.sh

Edited 2021-02-20 08:00 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4147
Posted: 09:57pm 19 Feb 2021
Copy link to clipboard 
Print this post

It probably is one of those.

Best to show what's in that .sh file, but I suspect the problem will be running it as root when it should almost for sure be run as the user.

John
Edited 2021-02-20 07:59 by JohnS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 8296
Posted: 10:03pm 19 Feb 2021
Copy link to clipboard 
Print this post

In Buster won't you have to use systemd or something?
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Rado
Regular Member

Joined: 27/11/2020
Location: Croatia
Posts: 59
Posted: 10:33pm 19 Feb 2021
Copy link to clipboard 
Print this post

The glorious invention that systemd is(*), it does not guarantee the ancient behaviour of rc.local script (to execute as the very last during the init), so there's no guarantee that the script started from there will not start before some other dependency is still inactive, and that might result in that script failing.

One simple and stupid solution is to add some seconds of sleep at the beginning of rc.local, to give time for sluggish services to come alive.

The same trick can be used in crontab, as there might be services that had no time to start before the user logged in (automatically). Solved in a similar way:

@reboot sleep 60 && /home/user/my_special_script.sh

Your script will execute 60 seconds after the cron has run the job, that should be ample time for any dependency to come to life.

(*) I have no intention to start another systemd war in this forum, so let's just accept that I'm a grumpy old sysadmin who can not cope with the glorious wonders of modern times and superb architectures.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3477
Posted: 10:38pm 19 Feb 2021
Copy link to clipboard 
Print this post

Good explanation, Rado--thanks. I've added sleep 30 to the beginning of my /etc/rc.local (though I've never found that my scripts were not working).
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:10am 20 Feb 2021
Copy link to clipboard 
Print this post

@reboot sh /home/pi/myprogram.sh/home/pi/logs/cronlog 2>&1[/QUOTE
in my crontab

This is the error log

[CODEpi@Pi-Weather4:~/logs $ cat cronlog
python3: can't open file 'myprogram.py': [Errno 2 No such
gured out how to add logs

I have
  Quote  @reboot sh /home/pi/myprogram.sh >/home/pi/logs/cronlog 2>&1

in my crontab

This is the error log

pi@Pi-Weather4:~/logs $ cat cronlog
python3: can't open file 'myprogram.py': [Errno 2] No such file or directory


I don't understand why it's changed the file name
I also tried
  Quote  @reboot sh /home/pi/myprogram.sh > /home/pi/logs/cronlog 2>&1
and get the same result

I can run the program by typing ./myprogram.sh or sh myprogram.sh so it should work
Edited 2021-02-20 21:14 by lew247
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3477
Posted: 12:55pm 20 Feb 2021
Copy link to clipboard 
Print this post

  lew247 said  This is the error log

pi@Pi-Weather4:~/logs $ cat cronlog
python3: can't open file 'myprogram.py': [Errno 2] No such file or directory


Where does python3 come from? Does your script try to execute a python script?

Perhaps we need to see myprogram.sh.

Note that any file referred to in the myprogram.sh should have a full path.

What's the smallest script that illustrates the problem? (Given that the log seems to indicate that crontab tried to execute the @reboot command line.)
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4147
Posted: 01:26pm 20 Feb 2021
Copy link to clipboard 
Print this post

We need to see /home/pi/myprogram.sh

I think it will be relying on PATH but you haven't set it to the right one (or at all), bearing in mind you are (or look to be) running as root when you probably do not want that.

John
Edited 2021-02-20 23:27 by JohnS
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3477
Posted: 02:12pm 20 Feb 2021
Copy link to clipboard 
Print this post

Re what John is saying, have you tried it with "crontab -e" rather than "sudo crontab -e"?

The latter may be getting you into trouble with executing as root's crontab rather than the "pi" user crontab.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 06:01pm 20 Feb 2021
Copy link to clipboard 
Print this post

Got it working perfectly now thanks everyone
I had to use a .desktop File as it's a graphics program and needed the X server to finish initializing before running
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 08:33pm 25 Feb 2021
Copy link to clipboard 
Print this post

This is Python so not everyone will be able to but
Could anyone that knows Linux and Matplotlib help me out?
I need to learn how to import a data set from a csv file and display it in Matplotlib on the Pi
Ideally I need to learn how to make a graph that will read the data from the first row which is the header and has the colum names
Then use the data in the rows to make the plot
There are 3 to start with , temp hightemp and low temp
Eventually I'll be expanding this to use others and sub plots but I need to learn how to do this simple one first

I've learnt how to get readings of my weather station at set times during the day and save them to a csv file with headers
I've learnt how to make a Matplotlib with the data and make a graph that shows the data
This was just a simple one with some data points



What I want to learn is how to import data from the csv file attached* and use that data to plot the graph

data.zip

It has 4 columns Day, Temp, Temphigh, TempLow
and the rows have the data for various times throughout the day I want to graph

I know it won't look brilliant and  using a bar graph would be a better option but I couldn't figure out how to get that working with more than one data set

So  if anyone could help me by telling me how to use the data in the csv file to make the (bar)graph by reading the csv I'd be extremely grateful
I can figure out other stuff once I know how to read the data and use it in matplotlib

I did watch several videos and tutorials online but none actually helped as they weren't the same as what I want to do
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025