1

I'm currently using crontab on a Raspberry Pi 4 Model B to launch my python script at boot. I've added this at the bottom of sudo crontab -e :

@reboot sh /home/pi/start.sh > /home/pi/logs/cronlog 2>&1 &

My start.sh script is like that :

#!/bin/sh
# start.sh

cd /home/pi/Desktop/Python_Scripts/Projet
sudo python3 main.py

If I run the shell script manually, everything works fine, but when it runs at boot, the serial communication doesn't work.

I already tried to add some delay in my python script to wait for the serial interface to be fully initialized but it still doesn't work.

Thanks in advance for any kind of help

EDIT : I must clarify that the script runs perfectly if I run

sh /home/pi/start.sh > /home/pi/logs/cronlog 2>&1 &

in the command line. However, the only thing that doesn't work if I run it at boot with crontab is the serial communication (looking up signals with an oscilloscope, it doesn't send data through the serial interface) but every other aspect of the program runs fine.

Log1k
  • 95
  • 6
  • If this is in `root`'s crontab, you don' need `sudo`; you are already `root`. It's even possible that this is causing the error. – tripleee May 11 '21 at 13:04
  • `#!bin/sh` is wrong; it is looking for a directory `bin` in the current directory, and then for `sh` within that. You want `#!/bin/sh` – tripleee May 11 '21 at 13:05
  • Please review [Cron job not running](https://stackoverflow.com/questions/22743548/cronjob-not-running) for standard troubleshooting tips. – tripleee May 11 '21 at 13:06
  • @tripleee I've tried both with and without sudo and neither of them worked... I forgot the `/` before bin when posting the questions, but it was in the script, thanks I edited the question Thank you, I'll look it up – Log1k May 11 '21 at 13:10

3 Answers3

0

Most likely cron is executing before the serial interface is initialized and causing your python script to raise an exception.

This can be verified by adding a relatively small delay (ie: 30 seconds) into your python script to see if it then functions properly.

If the script only needs to be run once, a simple fix could be to utilize the autostart file instead of cron. Commands in this file are run only after the gui is brought up successfully. This is located at /etc/xdg/lxsession/LXDE-pi/autostart

Teejay Bruno
  • 1,716
  • 1
  • 4
  • 11
  • My script doesn't raise any exception and, as I already said in my post, I already tried adding a delay before writing data on the serial interface. – Log1k May 12 '21 at 11:13
0

Try this:

  1. Open terminal
  2. Run sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
  3. Add this line to the file:
@path/app_name 
  1. Save and exit
Michael M.
  • 10,486
  • 9
  • 18
  • 34
-1

I also use Raspberry pi4.
I reccomend you use crontab -e without sudo.
My crontab -e like this:
@reboot bash /usr/bin/start_counter.sh
My /usr/bin/start_counter.sh:

#!/usr/bin/bash
while true
do
    python3 /home/pi/people_counter_android/main.py
done

In my way its work. I hope this help you.

Andrii
  • 74
  • 6
  • If the script needs access to privileged resources, it does need to run as `root` somehow. Of course, we can't tell from the information in the question whether that is actually the case; but the talk about "serial" gave me the impression that it needs to control the unit's I/O ports. – tripleee May 11 '21 at 13:11
  • the script starts well with crontab, but doesn't communicate through the serial interface. Unfortunately, your suggestion does not fix my problem – Log1k May 11 '21 at 13:28