How to start a *.sh file at bootup?

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

Post Reply
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: How to start a *.sh file at bootup?

Post by Egregius »

Add it to cron.
sudo crontab -e
@reboot /path/to/script.sh
jannl
Posts: 825
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.5.x
Location: Geleen
Contact:

Re: How to start a *.sh file at bootup?

Post by jannl »

Correct
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: How to start a *.sh file at bootup?

Post by ben53252642 »

An alternative method is to make an init.d script that runs your script on startup:

nano /etc/init.d/NAMEOFSCRIPT

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides: startscripts
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description: Starts the script
### END INIT INFO

case "$1" in
'start')
        sudo /root/scripts/myscript.sh
        ;;
'stop')
        ;;
*)
        echo "Usage: $0 { start | stop }"
        ;;
esac
exit 0
Then run these commands:
chmod 755 NAMEOFSCRIPT
update-rc.d NAMEOFSCRIPT defaults

Make sure the command you want the script to run works from the terminal:
eg: sudo /root/scripts/myscript.sh

Some systems might not need the sudo, otherwise to install it: apt-get install sudo
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: How to start a *.sh file at bootup?

Post by Egregius »

Continuing on this init.d script:
Do you know how to run a script at shutdown and reboot?
I need to run a script before domoticz and apache are stopped. Already tried with a init.d script but no luck so far.
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: How to start a *.sh file at bootup?

Post by Egregius »

the script is executable? Also for the root user?
Did you try with chmod 755?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to start a *.sh file at bootup?

Post by waaren »

what is the output of the command sudo crontab -l ?

Could be that cron is already starting your script before all dependencies are fulfilled. A quick (and dirty) work-around for that is to
change the line into:

Code: Select all

@reboot sleep 60 && /home/pi/domoticz/nefitserver.sh
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to start a *.sh file at bootup?

Post by waaren »

what is the output of the command sudo crontab -l ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest