Automatically starting services in linux

Use systemd to Start a Linux Service at Boot

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

What is systemd?

systemd is a Linux system tool initially developed by the Red Hat Linux team. It includes many features, including a bootstrapping system used to start and manage system processes. It is currently the default initialization system on most Linux distributions. Many commonly used software tools, such as SSH and Apache, ship with a systemd service.

It is simple to create a custom systemd service that will run any script or process you choose. Although there are several ways to run a script or start a process when your Linode boots, a custom systemd service makes it easy to start, stop, or restart your script, as well as configure it to start automatically on boot. systemd offers the advantage of using a standardized interface that is consistent across all Linux distributions that support it.

Create a Custom systemd Service

  1. Create a script or executable that the service will manage. This guide uses a simple Bash script as an example:
sudo cp test_service.sh /usr/bin/test_service.sh sudo chmod +x /usr/bin/test_service.sh 
[Unit] Description=Example systemd service. [Service] Type=simple ExecStart=/bin/bash /usr/bin/test_service.sh [Install] WantedBy=multi-user.target
sudo cp myservice.service /etc/systemd/system/myservice.service sudo chmod 644 /etc/systemd/system/myservice.service 

Start and Enable the Service

sudo systemctl start myservice 
sudo systemctl status myservice 
● myservice.service - Example systemd service. Loaded: loaded (/lib/systemd/system/myservice.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2018-05-01 18:17:14 UTC; 4s ago Main PID: 16266 (bash) Tasks: 2 Memory: 748.0K CPU: 4ms CGroup: /system.slice/myservice.service ├─16266 /bin/bash /usr/bin/test_service.sh └─16270 sleep 30 May 01 18:17:14 localhost systemd[1]: Started Example systemd service.. May 01 18:17:14 localhost cat[16269]: Example service started at 2018-05-01 18:17:14 May 01 18:17:14 localhost bash[16266]: Looping. 
sudo systemctl stop myservice sudo systemctl restart myservice 
sudo systemctl enable myservice 
Created symlink from /etc/systemd/system/multi-user.target.wants/myservice.service to /lib/systemd/system/myservice.service.
sudo systemctl status myservice 
● myservice.service - Example systemd service. Loaded: loaded (/usr/lib/systemd/system/myservice.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2018-05-02 15:03:07 UTC; 48s ago Main PID: 2973 (bash) CGroup: /system.slice/myservice.service ├─2973 /bin/bash /usr/bin/test_service.sh └─3371 sleep 30 May 02 15:03:07 localhost systemd[1]: Started Example systemd service.. May 02 15:03:07 localhost systemd[1]: Starting Example systemd service. May 02 15:03:07 localhost bash[2973]: Looping. May 02 15:03:37 localhost bash[2973]: Looping. 

For more information about using systemctl commands, see the systemctl guide.

Читайте также:  Chrome linux no webgl

Troubleshooting

  • “Example service started at …” line does not appear in the output of the status command. The systemd-cat output is not reliable because of a race condition. As a workaround update the test_service.sh file as follows:

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on Tuesday, May 1, 2018.

Источник

How to Auto-start Services on Boot in Linux?

unix login prompt

Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

One of the essential for a system administrator to know is how to configure service at boot, so when a server gets a reboot, they start automatically.

There could be various reasons for server reboot, including the following.

By doing the right configuration, you don’t have to start them manually each time you reboot.

A little bit of automation. Isn’t it?

The following examples are for two popular distros tested on DigitalOcean servers.

CentOS or RHEL 6.x

In the following example, I have taken an Apache HTTP server, but the procedure remains the same for any other services you wish to start at boot in Red Hat Enterprise Linux (RHEL) or CentOS 6 version.

You can keep any script file name, and here I’ve kept httpd

[root@Chandan init.d]# ls -ltr httpd -rwxr-xr-x. 1 root root 3371 Jan 6 08:56 httpd [root@Chandan init.d]#

We will use chkconfig utility which is available default on Linux or CentOS.

[root@Chandan init.d]# chkconfig --add httpd [root@Chandan init.d]# chkconfig httpd on
[root@Chandan init.d]# chkconfig --list httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@Chandan init.d]#

That’s all! httpd script will be called to start services on Linux boot.

Читайте также:  Облако на linux собственное

In case you need to disable the auto-start service then you can use the following commands

chkconfig httpd off chkconfig --del httpd

RHEL or CentOS 7.x/8.x

The procedure to configure services on boot in RHEL 7 is slightly different than RHEL 6. It uses systemd to manage the services.

Most of the software like Apache, PHP, MySQL, Nginx scripts are added in services when you install it.

Let’s take an example of PHP-FPM.

First thing first, let’s see the status of php-fpm (this assume you already have scripts in /usr/lib/systemd/system/ )

[root@instance-1 ~]# systemctl status php-fpm php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled) Active: inactive (dead) [root@instance-1 ~]#

As you can see the status is disabled which means it’s not configured to start on boot.

Let’s enable php-fpm to start on boot by using systemctl

[root@instance-1 ~]# systemctl enable php-fpm Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service. [root@instance-1 ~]#
[root@instance-1 ~]# systemctl status php-fpmphp php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled) Active: inactive (dead) [root@instance-1 ~]#

php-fpm is all set to start on boot. Let’s test it by rebooting the server.

If you ever need to disable starting services on boot, then you can use the below command

You may also prefer to check out this post explaining systemd and auto-starting services on boot.

Ubuntu

Configuring auto-start services in Ubuntu is slightly different. Let’s say the script name is Nginx

  • Login to Ubuntu server with root
  • Copy the script in /etc/init.d/ folder
  • Execute the below command
Читайте также:  Broadcast messages in linux

This has helped me and I believe it will be beneficial to you as well.

System administration is always fun and challenging, and if you are looking to supercharge your career in it, then you may refer to this Udemy course.

Источник

Оцените статью
Adblock
detector