1

I have an on_reboot.sh file that contains the following:

#!/bin/bash

source /home/pi/.profile
workon cv
cd /home/pi/reboot
python pi_test.py

After that I made the file executable using

$ chmod +x on_reboot.sh

and then in my crontab I put the following to run it on boot:

@reboot /home/pi/reboot/on_reboot.sh

However, it is not working when the Pi boots up. When I run the sh file on the command line it is working fine. I tried modifying the crontab like this:

@reboot sudo su – pi bash -c ‘/home/pi/reboot/on_reboot.sh’

to run the startup script as user pi but is not working either. How can I fix this?

1 Answer 1

2

You should start your script as service with a systemd unit. It seems the script only runs one time and does not stay in the background. I don't know other edge conditions of your script but with this what I see I would define a unit file with:

rpi ~$ sudo systemctl edit --force on_reboot.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=Script On Reboot

[Service]
User=pi
WorkingDirectory=/home/pi
Type=oneshot
RemainAfterExit=yes
ExecStart=/home/pi/reboot/on_reboot.sh

[Install]
WantedBy=multi-user.target

Edit again with sudo systemctl edit --full on_reboot.service.

Enable the service:

rpi ~$ sudo systemctl enable on_reboot.service

Reboot.

Check with systemctl status on_reboot.service

4
  • sir the script runs on reboot, but after 1 to 2 seconds it became off the script is not running anymore
    – eastwind
    Commented Sep 17, 2018 at 12:50
  • this is the error when i check it pastebin.com/xJ2nE4iP
    – eastwind
    Commented Sep 17, 2018 at 12:53
  • Yes, that's because the script do things I don't know. It will connect to a server. What server? What connection? Python will output something. What output? Seems it is a graphical output.
    – Ingo
    Commented Sep 17, 2018 at 13:02
  • Already fix sir im using opencv.imshow which shows the video stream on screen i jst disable it and everything works fine now thanks sir
    – eastwind
    Commented Sep 17, 2018 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.