
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Run a Bash Script as Daemon
Sometimes, we need to run an automated process, and for this, we use a script that runs continuously in the background. These types are called "daemons" in Linux. These daemons allow us to run independently from the terminal session. This means that the script will keep running even if we close the session.
In this tutorial, we will go through how we can turn a bash script into a daemon.
What is a Daemon?
A daemon is just a program or process that runs in the background continuously without needing any interaction from the user. In your day-to-day Linux tasks, you may already work with some processes like this. For example, if you use a database like MySQL or Postgres, they have services like this.
For example, to start Postgres, you say service Postgres start. What this command does is run a service (daemon) to start the database in the background, and it keeps running until you stop it. This is a real example of what a daemon is.
The system needs daemons to run some services and tasks in the background. To see all the daemons that are running in your system, run the following command ?
ps axl
This will list all the daemons that are up and running in your system.
Run a Bash Script as a Daemon
Bash script is a powerful scripting language that allows us to automate tasks, and using a daemon can give us a lot more benefits, such as automating repetitive tasks and ensuring that our script is running continuously in the background. This also allows us to focus on other tasks as our daemon is detached from the session.
Let's go through the steps and methods we can use to transform a simple bash script into a daemon.
First, let's create a simple bash script file with the example ?
#!/bin/bash i=0 while true; do echo "Running ... $i" ((i++)) sleep 10 done
We create an example that will just print the message "Running ..." every 10 seconds and increment the variable i each time.
Terminal Sessions
When we are working with a terminal session, all processes within this session will be killed if we close the terminal window (session). For example, if you run a process like this ?
sleep 300 &
This will run the sleep command in the background, and you can see the process running in the background using the jobs command. However, if you close the terminal and open it again, you will see that the process has been killed because it is attached to the current shell session. You can check this using the pstree command, which shows the parent of the process. For example ?
pstree -s 6542
Here, we pass the process ID to pstree, which will show an output like this ?
systemd???systemd???gnome-terminal???bash???sleep
In this case, the parent of the sleep command is bash, and the parent of bash is the terminal itself, which in my case is gnome-terminal.
Create a Daemon using nohup
In order to convert a script or a process into a daemon, we will use a tool called nohup, which stands for 'no hang up'. This command takes some options like this ?
nohup path/script.sh > /dev/null 2>&1 &
Let's explain what this does ?
- path/script ? It is the file we need to convert into a daemon.
- /dev/null 2>$1 ? It redirects all output to /dev/null, preventing the script from blocking.
- & ? It will make the script run in the background.
Using Systemd
We can use systemd (a system and service manager available in almost all distributions) to have more control over your daemon.
To do this, create a service unit file at the location /etc/systemd/myexample.service ?
[Unit] Description=My Script Daemon [Service] ExecStart=/path_to_script.sh Restart=always User=root StandardOutput=null StandardError=null [Install] WantedBy=multi-user.target
This will give us more control over the daemon.
To enable and start the service, we use the command ?
sudo systemctl enable myexample.service sudo systemctl start myexample.service
Managing the Daemon
Using systemd will give us some commands to manage the service (daemon).
To start a daemon, use the command ?
sudo systemctl start myexample.service
To stop a daemon, use the command ?
sudo systemctl stop myexample.service
And you can check the status of the daemon using the command ?
sudo systemctl stop myexample.service
Conclusion
In this tutorial, we discussed how to convert a simple Bash script into a daemon. We used two methods: a simple approach using the nohup command, and another using systemd to provide more control over the daemon.