Run a script at boot of Raspberry Pi

It happens that when creating a server, you want to run certain programs at the start of the bone. Here's how under Raspbian.

76c171b2-de28-f1d9-f84a-ff0c977d55d3

When install you a tool, generally it will provide you with the(s) script(s) to be run at the start of the machine. This is the case of Nginx, PHP - fpm, Apache, MySQL, etc.

First and foremost, the script

But if you want to make a script you even or so that this tool is cross-platform, It is complicated. For example, If you install a Minecraft Server (merci Pierre pour l’idĂ©e d’article 🙂 ).

You create a file in the directory /etc/init.d. This file XYZ will have as the basic content :

#!/bin/bash

# After this line commands will be performed systematically

# ...

# After this line commands executed on the basis of the parameter passes command-line box "$1" in start)

        # Commands executed with the start parameter (that at boot)

        ;;

    stop)

        # Commands executed with the stop parameter (that when the system is shut down)

        ;;

    reload|restart)

        $0 stop

        $0 Start

        ;;

    *)

        echo "Use: $0 Start|stop|restart|reload"

        exit 1

ESAC exit 0

Don't forget to make the script executable with the command chmod :

chmod 755 /etc/init.d/XYZ

You can then test your script with the command service. You pass as a parameter the argument you want to. For example to start your service :

Service XYZ start

Enable scripting

If it works as you want, You can then enable the launch of the script at startup of the system with the command update - rc.d :

update - rc.d defaults XYZ

This command adds the script in your Raspberry boot sequence. You can use other values instead of defaults :

  • Remove : to remove the script from the boot sequence
  • Disable : to disable the script
  • enable : to enable scripting

Other settings allow you to adjust the boot sequence and priorities of the scripts by modifying the LSB configuration (more info in This post).

Yes but if I interact with my service ?

You have now all the information in order to create you even a service that launches at startup of your Raspberry. However, There is a special situation which can be problematic : How to do if my program does not make the hand and is interactive ?

The magic command is screen. This command will allow you to run a command in your terminal stain but to continue to interact with this command.

For example :

screen - dmS ma_session/path/to/my/script

This command will launch the script in a called session ma_session. The program runs and works normally but back plan.

To interact with you can always use the command screen :

screen p 0 -S ma_session x eval ' stuff "ma_commande_a_executer"15'

By activating the default window of the application attached to ma_session, I ask to run the command ma_commande_a_executer. This command could be in the XYZ script (in /etc/init.d) with a particular parameter, for example do_my_cmd and will be then invoked with :

Service XYZ do_my_cmd

VoilĂ , I hope that this article you will be given information and tracks to make your own services. Do not hesitate to share your experiences in this technical field (or correct mistakes that have would be found).

Like this article ? Bear with me on patreon!

You may also like...