How to write an init script that will execute an existing start script in Ubuntu 20.04?

9,893

There are two common ways to start a service on Ubuntu:

  1. Making a service file in /etc/init.d directory.

You can check the existing ones as examples. There used to be a template file named skeleton which is no more exists. However you can find a basic example and more help by running man init-d-script.

After creating the file run sudo update-rc.d myservice defaults to install your service (here referred as myservice). refer to update-rec.d, Then you can start and stop your service using sudo service myservice start

  1. The second and recommended or new one is creating a SystemD service.

To know the difference with the above check: Difference between systemctl init.d and service

Here is a simple example of how to create and run your service:

https://www.linode.com/docs/quick-answers/linux/start-service-at-boot/

You can also refer to his manual page for detailed descriptions of these two styles:

http://0pointer.de/public/systemd-man/daemon.html

Share:
9,893
Ahmad
Author by

Ahmad

I am a university instructor teaching computer courses, I am also a researcher, programmer, web designer and application developer I mainly work with C# .Net, PHP Mysql.

Updated on September 18, 2022

Comments

  • Ahmad
    Ahmad over 1 year

    my question similar to: How to write an init script that will execute an existing start script?

    However, that question is outdated and I like to know how I can do that in Ubuntu 20.04

    This my try in init.d directory:

    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides:          myrec
    # Required-Start:    $all
    # Required-Stop:
    # Default-Start:     2 3 4 5
    # Default-Stop:
    # Short-Description: your description here
    ### END INIT INFO
    
    PATH=/home/ahmad/recordings
    DESC="Recording audio output"
    NAME=myrec
    DAEMON=/home/ahmad/recordings/myrec.sh
    PIDFILE=/var/run/$NAME.pid
    SCRIPTNAME=/etc/init.d/$NAME
    
    • I would like to run it after all processes executed, so should I use a systemd service or the above is okay?

    • Should I add other code to above or it runs my sh (myrec.sh) file?

    In general What is a hello world for executing a simple bash (so that I can see the result) for ubuntu 20.04? Every thing I found in the web is outdated