How to create daemons in Linux with a simple “Hello World” — bash example!

Someshwaran M
7 min readDec 14, 2022
Daemons in Linux (Source credit: https://blog.kalavala.net/)

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:

Sample Format

The while loop in the daemon function allows the code to run continuously, while the sleep 1command 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 Worldto the terminal:

hello_world.sh

The next step is to create the start and stopfunctions. These functions are used to control the execution of the daemon. The start function should be structured as follows:

Start Function

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 stopfunction to stop the daemon.

The stop function should be structured as follows:

Stop Function

The stopfunction uses the killcommand 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 startand stopfunctions to control its execution.

[Important] In order to run the daemon, you will need to make the $Bashscript executable. This can be done using the chmodcommand:

Providing File Permissions

You can use sudo chmodto provide permissions as a rootuser. Once the script is executable, you can use the startfunction to start the daemon:

Start Command

This will run the daemon in the background, printing “Hello world” every second. To stop the daemon, use the stop function:

Stop Command

This will stop the daemon. And, to remove the PID file,

Remove Command

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 restartfunction should be structured as follows:

Restart Function

With this function in place, you can easily restartthe daemon by running the following command:

Restart 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 restartfunctions from your $Bash script. The startup script should be structured as follows:

Startup Script

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:

Providing File Permissions

The first command makes the startup script executable and you can use sudo chmodto provide permissions as a rootuser. 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:

Service Commands

These commands will start, stop , and restart your daemon, respectively. You can also check the statusof your daemon using the status command:

Check Status

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 worldexample 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:

--

--

Someshwaran M

I am an Open-Source Enthusiast. I learned a lot from the Open-Source community and I love how collaboration, knowledge sharing happens through Open-Source!