How to create daemons in Linux with a simple “Hello World” — bash example!
Daemons are programs that run in the background of a Linux operating system. They provide essential services and functions, such as managing network connections and running scheduled tasks. Creating a daemon in Linux can be a useful way to run a program continuously without having to manually start it each time.
To create a simple hello world
daemon in Linux, you will need to have a basic understanding of the Bash shell and Linux command line. The first step is to create a Bash script that contains the code for your daemon. This can be done using any text editor, such as Vim
or Nano
.
In the script, you will need to include the following elements:
1. The shebang line,
2. The daemon function,
3. The start function, and
4. The stop function.
The shebang line, which begins with #!
, tells the Linux system what interpreter to use to execute the script. The daemon function is the main body of the script, where you will write the code for your daemon. The start and stop functions are used to control the execution of the daemon.
To create the shebang line, simply add the following line to the top of your script: #!/bin/bash
. This tells the Linux system to use the Bash interpreter to execute the script. You can verify the bash using the command -which bash
to know the location.
Next, you will need to create the daemon function. This function is where the main code for your daemon will go. It should be structured as follows:
The while
loop in the daemon function allows the code to run continuously, while the sleep 1
command ensures that the daemon doesn’t use up too many system resources by running constantly. You can adjust the sleep time to suit your needs. Within the while loop, you can add the code for your daemon. For our hello world
example, we can simply add an echo
command to print Hello World
to the terminal:
The next step is to create the start
and stop
functions. These functions are used to control the execution of the daemon. The start function should be structured as follows:
The start
function runs the daemon function in the background, using the &
operator. It also saves the process id(PID)
of the daemon in a file called /var/run/hello_world.pid
. This file will be used by the stop
function to stop the daemon.
The stop function should be structured as follows:
The stop
function uses the kill
command to stop the daemon. It reads the PID from the /var/run/hello_world.pid
file and uses it to kill the process.
With these functions in place, you have the basic structure of a Linux daemon. You can now add the code for your daemon to the daemon function, and use the start
and stop
functions to control its execution.
[Important] In order to run the daemon, you will need to make the $Bash
script executable. This can be done using the chmod
command:
You can use sudo chmod
to provide permissions as a root
user. Once the script is executable, you can use the start
function to start the daemon:
This will run the daemon in the background, printing “Hello world” every second. To stop the daemon, use the stop function:
This will stop the daemon. And, to remove the PID file,
In addition to the start and stop functions, it is also useful to include a restart function. This function can be used to stop and then start the daemon, allowing you to easily update the code for your daemon without having to manually stop and start it each time. The restart
function should be structured as follows:
With this function in place, you can easily restart
the daemon by running the following command:
Once you have created and tested your daemon, you may want to add it to the system startup
process so that it automatically runs whenever the system boots. This can be done using the Linux init
system, which is responsible for managing the startup and shutdown of services on the system.
To add your daemon to the init
system, you will need to create a startup
script. This script should be placed in the /etc/init.d/
directory and should include the start
, stop
, and restart
functions from your $Bash
script. The startup script should be structured as follows:
The startup
script uses the case statement to determine which function to run based on the first argument passed to the script. For example, running /etc/init.d/hello_world.sh start
will call the start function from the Bash script. The startup script also includes a usage message and an exit code to indicate success or failure.
Once you have created the startup script, you will need to make it executable and add it to the system startup process. This can be done using the following commands:
The first command makes the startup script executable and you can use sudo chmod
to provide permissions as a root
user. And, the second command adds it to the system startup process using the update-rc.d
utility. This will ensure that your daemon is started automatically whenever the system boots.
To test that your daemon is running correctly, you can use the following commands:
These commands will start
, stop
, and restart
your daemon, respectively. You can also check the status
of your daemon using the status command:
This will tell you whether the daemon is currently running or not. With these commands, you can control and manage your Linux daemon easily.
In conclusion, creating a daemon
in Linux is a useful way to run a program continuously in the background. It involves creating a $Bash
script with the necessary functions and adding it to the system startup process. By following the steps outlined in this article, you can create and manage a simple hello world
daemon in Linux.
While this article focused on a simple hello world
example you can use the same principles to create more complex daemons for your own needs. By understanding the basics of Linux daemons, you can create powerful
and useful
programs that run continuously in the background of your Linux system.
With some practice and experimentation, you can become proficient at creating and managing daemons in Linux. This can be a valuable skill for any Linux user or administrator, as it allows you to automate tasks and provide essential services on your Linux system.
I hope this article was insightful and helpful for Linux enthusiasts. Wish you all success and great health, moving forward. Thank you!
Looking for more? Check out the other articles below: