How to start a *.sh file at bootup?

Others (MiLight, Hue, Toon etc...)
Post Reply
pascalbianca
Posts: 37
Joined: Monday 18 December 2017 19:20
Target OS: Raspberry Pi
Domoticz version: V3.8153
Location: Susteren
Contact:

How to start a *.sh file at bootup?

Post by pascalbianca » Thursday 11 January 2018 7:09

Hello people,

I made a script to start a server.
That script i normaly start from the terminal screen.
Now i want to see that my script starts when the pi bootsup.

I tryed different things but nothing works.
How can i make that happen, need some help please.

User avatar
Egregius
Posts: 2662
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

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

Post by Egregius » Thursday 11 January 2018 7:19

Add it to cron.
sudo crontab -e
@reboot /path/to/script.sh

pascalbianca
Posts: 37
Joined: Monday 18 December 2017 19:20
Target OS: Raspberry Pi
Domoticz version: V3.8153
Location: Susteren
Contact:

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

Post by pascalbianca » Thursday 11 January 2018 15:09

so @reboot /path/to/script.sh must be placed in that file whats opent by crontab?

User avatar
jannl
Posts: 955
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Geleen
Contact:

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

Post by jannl » Thursday 11 January 2018 16:08

Correct

ben53252642
Posts: 381
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 » Thursday 11 January 2018 16:32

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

User avatar
Egregius
Posts: 2662
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

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

Post by Egregius » Thursday 11 January 2018 16:45

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.

pascalbianca
Posts: 37
Joined: Monday 18 December 2017 19:20
Target OS: Raspberry Pi
Domoticz version: V3.8153
Location: Susteren
Contact:

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

Post by pascalbianca » Thursday 11 January 2018 16:53

i placed my script into the cronjob but stil its not loading if i reboot the pi.
The code what i have pasted in cronjob is.:

Code: Select all

@reboot /home/pi/domoticz/nefitserver.sh

User avatar
Egregius
Posts: 2662
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

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

Post by Egregius » Thursday 11 January 2018 17:11

the script is executable? Also for the root user?
Did you try with chmod 755?

User avatar
waaren
Posts: 601
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post by waaren » Thursday 11 January 2018 17:29

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
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave , dzVents 2.4.n, Amazon echo

pascalbianca
Posts: 37
Joined: Monday 18 December 2017 19:20
Target OS: Raspberry Pi
Domoticz version: V3.8153
Location: Susteren
Contact:

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

Post by pascalbianca » Thursday 11 January 2018 17:44

@Egregius, yes i did that.

@waaren , no i'm sorry does also not work.

User avatar
waaren
Posts: 601
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post by waaren » Thursday 11 January 2018 17:46

what is the output of the command sudo crontab -l ?
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave , dzVents 2.4.n, Amazon echo

pascalbianca
Posts: 37
Joined: Monday 18 December 2017 19:20
Target OS: Raspberry Pi
Domoticz version: V3.8153
Location: Susteren
Contact:

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

Post by pascalbianca » Thursday 11 January 2018 17:50

Code: Select all

pi@raspberrypi:~ $ sudo crontab -l
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
@reboot sleep 60 &&  /home/pi/domoticz/nefitserver.sh

pascalbianca
Posts: 37
Joined: Monday 18 December 2017 19:20
Target OS: Raspberry Pi
Domoticz version: V3.8153
Location: Susteren
Contact:

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

Post by pascalbianca » Thursday 11 January 2018 18:18

its finaly working.
I placed it in the file rc.local and now its running like a sharm if i start the pi or do a reboot.
Thanks guys for helping me.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest