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

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 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 or .

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: . This tells the Linux system to use the Bash interpreter to execute the script. You can verify the bash using the command - 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 loop in the daemon function allows the code to run continuously, while the 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 example, we can simply add an command to print to the terminal:

hello_world.sh

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

Start Function

The function runs the daemon function in the background, using the operator. It also saves the of the daemon in a file called . This file will be used by the function to stop the daemon.

The stop function should be structured as follows:

Stop Function

The function uses the command to stop the daemon. It reads the PID from the 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 and functions to control its execution.

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

Providing File Permissions

You can use to provide permissions as a user. Once the script is executable, you can use the function 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 function should be structured as follows:

Restart Function

With this function in place, you can easily the 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 process so that it automatically runs whenever the system boots. This can be done using the Linux system, which is responsible for managing the startup and shutdown of services on the system.

To add your daemon to the system, you will need to create a script. This script should be placed in the directory and should include the , , and functions from your script. The startup script should be structured as follows:

Startup Script

The script uses the case statement to determine which function to run based on the first argument passed to the script. For example, running 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 to provide permissions as a user. And, the second command adds it to the system startup process using the 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 , , and your daemon, respectively. You can also check the of 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 in Linux is a useful way to run a program continuously in the background. It involves creating a 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 daemon in Linux.

While this article focused on a simple 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 and 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:

--

--

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!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
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!