How it works...

Let's understand our script in detail:

  1. In step 1, we create a trivial looping program to be ran at system startup called myscript.sh.
  2. In step 2, we copy the script to the /usr/bin directory and add permissions using the chmod command for everyone to be able to execute the script (chmod a+x myscript.sh). Notice the use of sudo permissions to create a file in this directory and to apply permissions.
  3. In the third step, we create the service configuration file, which describes a service unit for systemd. It goes by the name of myscript and within the [Service] directive, the two most important parameters are present: ExecStart and ExecStop. Notice that the start and stop sections look similar to the SysV/init.d approach.

 

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.