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
There are two common ways to start a service on Ubuntu:
/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 runningman init-d-script
.After creating the file run
sudo update-rc.d myservice defaults
to install your service (here referred asmyservice
). refer to update-rec.d, Then you can start and stop your service usingsudo service myservice start
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