Linux Daemon: Create Background Processes
To set a process as a daemon, you can follow these steps:
- Write a script or program to implement the required service or program to be executed.
- Open the terminal and log in as a root user or a user with administrative privileges.
- Run the script or program using the “nohup” command so that it continues to run even after the terminal is closed. For example: nohup /path/to/your/script.sh &
- Check the running processes by using the command ‘ps -ef | grep your_script.sh’ to ensure that it is running.
- Create a Systemd service unit file using the command sudo systemctl enable your_script.service. Replace “your_script” with the name of your script or program.
- Open the systemd service unit file in a text editor, for example: sudo nano /etc/systemd/system/your_script.service.
- Add the following content in the open file:
[Unit]
Description=Your Service
After=network.target
[Service]
ExecStart=/path/to/your/script.sh
[Install]
WantedBy=default.target
Replace “Your Service” with a description of your service, and replace “/path/to/your/script.sh” with the path to your script or program.
- Save and close the file.
- Start the service by using the command sudo systemctl start your_script.service.
- Check the status of the service’s operation by running the command sudo systemctl status your_script.service.
- Set your service to start automatically at boot by using the command sudo systemctl enable your_script.service.
Your script or program has now been configured as a daemon and will automatically run at system startup.