admin管理员组

文章数量:1026989

I am trying to autorun my Python script whenever my Raspberry Pi 4 boots up or when I start connecting it to a power source, but I already tried different methods but didn't work.

By the way, my Python script is about image processing, where it will open the raspberry pi camera for pothole detection.

I already tried the -cron method and put this code inside but it didn't work:

@reboot /usr/bin/python3 /home/raspberrypi/tflite-custom-object-bookworm-main/d>

Tried using rc.local and didn't work as well:

/usr/bin/python3 /home/raspberrypi/tflite-custom-object-bookworm-main/detect1.p>

Tried LXSession on my .config and placed this code on my autostart file but didn't work:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
point-rpi
@lxterminal -e sudo python3 /home/raspberrypi/Desktop/detect1.py

Done doing the systemd method as well and it didn't work. Here's the code I used:

[Unit]
Description=My Auto-start Service
After=network.target

[Service]
ExecStart=/path/to/your/script.sh
WorkingDirectory=/path/to
StandardOutput=inherit
StandardError=inherit
Restart=always
User=raspberrypi

[Install]
WantedBy=multi-user.target

So far, when I reboot my raspberry pi, the script is not auto running, I need to open the terminal first before the script autorun. Please help! Thank you in advance!

I am trying to autorun my Python script whenever my Raspberry Pi 4 boots up or when I start connecting it to a power source, but I already tried different methods but didn't work.

By the way, my Python script is about image processing, where it will open the raspberry pi camera for pothole detection.

I already tried the -cron method and put this code inside but it didn't work:

@reboot /usr/bin/python3 /home/raspberrypi/tflite-custom-object-bookworm-main/d>

Tried using rc.local and didn't work as well:

/usr/bin/python3 /home/raspberrypi/tflite-custom-object-bookworm-main/detect1.p>

Tried LXSession on my .config and placed this code on my autostart file but didn't work:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
point-rpi
@lxterminal -e sudo python3 /home/raspberrypi/Desktop/detect1.py

Done doing the systemd method as well and it didn't work. Here's the code I used:

[Unit]
Description=My Auto-start Service
After=network.target

[Service]
ExecStart=/path/to/your/script.sh
WorkingDirectory=/path/to
StandardOutput=inherit
StandardError=inherit
Restart=always
User=raspberrypi

[Install]
WantedBy=multi-user.target

So far, when I reboot my raspberry pi, the script is not auto running, I need to open the terminal first before the script autorun. Please help! Thank you in advance!

Share Improve this question asked Nov 16, 2024 at 15:42 Genesis CoronelGenesis Coronel 1
Add a comment  | 

1 Answer 1

Reset to default 0

Different OS and versions use different methods to get things started at boot. Which OS are you using? I have a Raspi 4B running Raspian OS lite Bookworm and I was able to get my python script to auto-start on boot working with crontab using this example: https://www.instructables/Raspberry-Pi-Launch-Python-script-on-startup/ Here are the exact commands I used:

Create a startup script:

sudo nano start_on_power_on.sh

File contents below. Username is your current login username and assumes your file is located at your /home/Username directory. "main_loop.py" is the python filename:

cd /
cd home/Username
sudo python main_loop.py
cd /

Hit CTRL+X then Y to save to file. Then make .sh file executable:

sudo chmod 755 start_on_power_on.sh

Prepare log file directory:

sudo mkdir logs

Edit crontab

sudo crontab -e

Add this line below the comments:

@reboot sh /start_on_power_on.sh >/home/Username/logs/cronlog 2>&1

At first it wasn't working, so I reviewed the log file located here: /home/Username/logs/cronlog by issuing:

cd /logs
cat cronlog

I learned from bash reported errors that my directories were off a bit, so I made some adjustments and the corrected directories are included above. Running again I learned my Python code ran into new errors because things that depended on the internet were failing because the network and TCP/IP was not available yet. So after multiple rounds of reviewing the cronlog, code updates, and sudo reboots I was able to get my python code to run at startup successfully.

You can also see if your script is running by issuing a command similar to this:

ps ax | grep python

And you should get a response something like this:

532 ?        S      0:00 sudo python main_loop.py
548 ?        S      0:02 python main_loop.py
970 pts/0    S+     0:00 grep --color=auto python

I am trying to autorun my Python script whenever my Raspberry Pi 4 boots up or when I start connecting it to a power source, but I already tried different methods but didn't work.

By the way, my Python script is about image processing, where it will open the raspberry pi camera for pothole detection.

I already tried the -cron method and put this code inside but it didn't work:

@reboot /usr/bin/python3 /home/raspberrypi/tflite-custom-object-bookworm-main/d>

Tried using rc.local and didn't work as well:

/usr/bin/python3 /home/raspberrypi/tflite-custom-object-bookworm-main/detect1.p>

Tried LXSession on my .config and placed this code on my autostart file but didn't work:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
point-rpi
@lxterminal -e sudo python3 /home/raspberrypi/Desktop/detect1.py

Done doing the systemd method as well and it didn't work. Here's the code I used:

[Unit]
Description=My Auto-start Service
After=network.target

[Service]
ExecStart=/path/to/your/script.sh
WorkingDirectory=/path/to
StandardOutput=inherit
StandardError=inherit
Restart=always
User=raspberrypi

[Install]
WantedBy=multi-user.target

So far, when I reboot my raspberry pi, the script is not auto running, I need to open the terminal first before the script autorun. Please help! Thank you in advance!

I am trying to autorun my Python script whenever my Raspberry Pi 4 boots up or when I start connecting it to a power source, but I already tried different methods but didn't work.

By the way, my Python script is about image processing, where it will open the raspberry pi camera for pothole detection.

I already tried the -cron method and put this code inside but it didn't work:

@reboot /usr/bin/python3 /home/raspberrypi/tflite-custom-object-bookworm-main/d>

Tried using rc.local and didn't work as well:

/usr/bin/python3 /home/raspberrypi/tflite-custom-object-bookworm-main/detect1.p>

Tried LXSession on my .config and placed this code on my autostart file but didn't work:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
point-rpi
@lxterminal -e sudo python3 /home/raspberrypi/Desktop/detect1.py

Done doing the systemd method as well and it didn't work. Here's the code I used:

[Unit]
Description=My Auto-start Service
After=network.target

[Service]
ExecStart=/path/to/your/script.sh
WorkingDirectory=/path/to
StandardOutput=inherit
StandardError=inherit
Restart=always
User=raspberrypi

[Install]
WantedBy=multi-user.target

So far, when I reboot my raspberry pi, the script is not auto running, I need to open the terminal first before the script autorun. Please help! Thank you in advance!

Share Improve this question asked Nov 16, 2024 at 15:42 Genesis CoronelGenesis Coronel 1
Add a comment  | 

1 Answer 1

Reset to default 0

Different OS and versions use different methods to get things started at boot. Which OS are you using? I have a Raspi 4B running Raspian OS lite Bookworm and I was able to get my python script to auto-start on boot working with crontab using this example: https://www.instructables/Raspberry-Pi-Launch-Python-script-on-startup/ Here are the exact commands I used:

Create a startup script:

sudo nano start_on_power_on.sh

File contents below. Username is your current login username and assumes your file is located at your /home/Username directory. "main_loop.py" is the python filename:

cd /
cd home/Username
sudo python main_loop.py
cd /

Hit CTRL+X then Y to save to file. Then make .sh file executable:

sudo chmod 755 start_on_power_on.sh

Prepare log file directory:

sudo mkdir logs

Edit crontab

sudo crontab -e

Add this line below the comments:

@reboot sh /start_on_power_on.sh >/home/Username/logs/cronlog 2>&1

At first it wasn't working, so I reviewed the log file located here: /home/Username/logs/cronlog by issuing:

cd /logs
cat cronlog

I learned from bash reported errors that my directories were off a bit, so I made some adjustments and the corrected directories are included above. Running again I learned my Python code ran into new errors because things that depended on the internet were failing because the network and TCP/IP was not available yet. So after multiple rounds of reviewing the cronlog, code updates, and sudo reboots I was able to get my python code to run at startup successfully.

You can also see if your script is running by issuing a command similar to this:

ps ax | grep python

And you should get a response something like this:

532 ?        S      0:00 sudo python main_loop.py
548 ?        S      0:02 python main_loop.py
970 pts/0    S+     0:00 grep --color=auto python

本文标签: terminalAutorun script in Raspberry Pi 4Stack Overflow