\begin{exe}{Adding a New Startup Service with SysVinit}

   In this and the following exercise, we will create a
   simple startup service.  First we will do it for a
   \textbf{SysVinit} system.  Note that if you are using a
   \textbf{systemd}-based system everything should still
   work because of the backwards compatibility layer that
   all distributions utilize. However, in the next exercise
   we will do natively for \textbf{systemd}.

   \begin{debianbox}[-based Distributions]
      If you are on a \textbf{Debian}-based system like
      \textbf{Ubuntu}, make sure you have installed the
      \textbf{sysvinit-utils} and \textbf{chkconfig}
      packages.  However, recent versions of \textbf{Ubuntu}
      no longer package \textbf{chkconfig}; you'll have to
      use the \textbf{update-rc.d} utility instead.
   \end{debianbox}

   First we have to create the service-specific script;
   you can create one of your own for fun, or to get the
   procedure down just (as root) create a file named
   \url{/etc/init.d/fake_service}
   (which can be extract from your downloaded
   \verb?SOLUTIONS? file as
   \lablink{fake\_service})  containing the following:

   \shfile[\texttt{/etc/init.d/fake\_service}]{labs/fake_service}


   If you are taking the online self-paced version of this course,
   the script is available for download from your \textbf{Lab}
   screen.

   Make the file above executable and give other proper
   permissions:
   \begin{cmd}
$ sudo chmod 755 /etc/init.d/fake_service
   \end{cmd}

   You'll notice the script includes the file
   \texttt{/etc/sysconfig/fake\_service}.  (On
   non-\textbf{RHEL} systems you should change this to
   \url{/etc/default/fake_service}.)  Create it and
   give it the following contents:

   \begin{shlst}[\texttt{/etc/sysconfig/fake\_service} or
      \texttt{/etc/default/fake\_service}]
      VAR1="true"
      VAR2="true"
   \end{shlst}

   Test to see if the script works properly by running
   the following commands:
   \begin{cmd}
$ sudo service fake_service
$ sudo service fake_service start
$ sudo service fake_service stop
   \end{cmd}

   Look at the file named \filelink{/var/log/fake\_service.log}.
   What does it contain?

   For fun you can add additional modes like
   \verb?restart?  to the script file; look at other
   scripts in the directory to get examples of what to
   do.

   Next we will want to have the ability to start
   \textbf{fake\_service} whenever the system starts, and
   stop it when it shuts down.  If you do:
   \begin{cmd}
$ sudo chkconfig --list fake_service
   \end{cmd}
   you will get an error as it hasn't been set up yet for
   this.  You can easily do this with:
   \begin{cmd}
$ sudo chkconfig --add fake_service
   \end{cmd}
   and you can turn it on or off at boot time with
   \begin{cmd}
$ sudo chkconfig fake_service on
$ sudo chkconfig fake_service off
   \end{cmd}
   To test this completely you'll have to reboot the
   system to see if it comes on automatically.  You can
   also try varying the runlevels in which the service is
   running.
\end{exe}

