TransWikia.com

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

Ask Ubuntu Asked on November 28, 2021

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

One Answer

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

Answered by Ahmad on November 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP