Execute script and services on Start-Up – RPi /Linux

pi

In order to have a command or program run when the Pi boots, you can add commands to the rc.local file. This is especially useful if you want to be able to plug your Pi in to power headless, and have it run a program without configuration or a manual start.

Example – Create autostart for one service and one bash/shell (*.sh):

sudo nano /etc/rc.local

Content:

#!/bin/sh -e
# rc.local
/wifiscan/startup.sh &            #<< startup bash
service yourservice start         #<< startup service 
/etc/init.d/cron start            #<< start cron for future usage with full path
exit 0

Now, your RPi it’s ready to auto-start your tasks!

>> create startup.sh file / bash:

sudo nano /YourFolder/startup.sh
#!/bin/bash

#example - update ntp clock
sudo service ntp stop
sudo ntpdate -s $NTP_SERVER
sudo service ntp start

echo "Today is $(date)"

>> set file permissions:

sudo chmod +x /YourFolder/startup.sh

>> create yourservice file:

sudo nano /etc/init.d/yourservice
#! /bin/sh
#/etc/init.d/wifiscan

### BEGIN INIT INFO
# Provides: YOURSERVICENAME
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start a program from boot
# Description: start YOURSERVICENAME program
### END INIT INFO

# Put any commands you always want to run here.

case "$1" in
start)
echo "Starting example"
# run the program you want to start
/YOURFOLDER/YOURBASH.sh
;;
stop)
echo "Stopping example"
# end the program you want to stop
killall YOURSERVICENAME
;;
*)
 echo "Usage: /etc/init.d/YOURSERVICENAME {start|stop}"
 exit 1
 ;;
esac

>> set file permissions:

sudo chmod +x /etc/init.d/yourservice

Your service is ready to start.

You need to restart your service at 01h00 o’clock daily | weekly ?? use crontab -e

crontab-format

sudo crontab -e  (note ... crontab -e != sudo crontab -e)
# 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
0 0 * * * /YOURFOLDER/remove_old_files_example.sh > /tmp/log 2>&1 # remove old files at 0h00 o'clock and add to log
0 1 * * * /etc/init.d/YOURSERVICE restart > /tmp/log 2>&1 # restart service at 1h00 o'clock and add to log

#test here :https://crontab.guru/examples.html

Anúncios

Deixe uma Resposta

Preencha os seus detalhes abaixo ou clique num ícone para iniciar sessão:

Logótipo da WordPress.com

Está a comentar usando a sua conta WordPress.com Terminar Sessão / Alterar )

Imagem do Twitter

Está a comentar usando a sua conta Twitter Terminar Sessão / Alterar )

Facebook photo

Está a comentar usando a sua conta Facebook Terminar Sessão / Alterar )

Google+ photo

Está a comentar usando a sua conta Google+ Terminar Sessão / Alterar )

Connecting to %s