Friday, July 10, 2020

How to run a program during start up on Linux using systemctl


How to run a program during start up on Linux
Use the following steps to run a program on start up or booting on Linux machine.  Any program that needs to be run during start or boot is termed as service. We will use systemd to run a service in the background during startup. systemd is a system and service manager for Linux, also capable of replacing the traditional SysV init system
1.       To create a service run the following command
sudo nano /etc/system/system/NAME_OF_YOUR_SERVIC.service.

2.        nano editor will open a blank file. Put the following command and save the file.
Description = DESRIPTION_OF_YOUR_SERVICE // A small description of your service here.
Wants=network.target // This means the service will start after the network is ready.
[Service]
Type=simple
ExecStart="Application/script name with path"  ex: /home/pramod/mybgapp.sh
Restart=on-failure
RestartSec=10
KillMode=process

[install]
WantedBy=multi-user.target

3.       Once the service is created we need to reload the service using systemctl. Use the following command to reload the service.
Sudo systemctl daemon-reload
4.       Use the following command to enable the service.
Sudo systemctl enable YOUR_SERVICE_NAME
5.       To start the service use the following command
Sudo systemctl start YOUR_SERVICE_NAME
6.       To check if the service is running or not use the following command.
Systemctl status YOUR_SERVICE_NAME
7.       Now reboot the system. Application will start automatically else crash if failed.





No comments:

Post a Comment