Run service on boot 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.

Читайте также:  Linux structure file system

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 can I configure a service to run at startup

but it’s not configured to auto start when the PC reboots. How can I configure it to start automatically, even if no one is logged into the PC?

@user154721 What arguments did you give update-rc.d to make autostart work? I’ve tried various options but haven’t had any luck.

Auto-start programs are configured in autostart manifests or in *.service files in several locations, as well as in init.d or crontab . See: unix.stackexchange.com/a/525845/43233

5 Answers 5

sudo update-rc.d minidlna defaults 

This should add the service to the automatic startup system. But if you get:

System start/stop links for /etc/init.d/minidlna already exist. 
sudo update-rc.d minidlna enable 

P.S.: For further detail look at the man page for update-rc.d by typing the command man update-rc.d

thanks, but what if I get this output when running that command?: System start/stop links for /etc/init.d/minidlna already exist.

if minidlna doesnt write to any logfile you won’t find it any where. Does it maybe fail because there is no network connection when it is started? Try to look at the configuration if it is possible turn on debugging and/or logging to find out whats wrong.

I am also, got the same error update-rc.d: /etc/init.d/mongod: file does not exist . Then, sudo systemctl enable mongod.service worked for me.

Since Ubuntu 15.10 and newer (resp. Debian 8 «jessie» and newer), you have to use the following command to configure your service minidlna to run at startup:

sudo systemctl enable minidlna.service 

And to disable it again from starting at boot time:

sudo systemctl disable minidlna.service 

This works with all service names available on your system. To find out available service names, just list the filenames of the service files:

ls /lib/systemd/system/*.service ls /etc/systemd/system/*.service 

As for sudo, I recieve Unknown operation enable. If I discard sudo it will ask me which user I would like to choose. Pick your root user and you will be able to set the desired value.

Читайте также:  Chess on linux mint

@tanius I have followe the procedure of adding new services to /etc/systemd/system/*.service. I would like to know why no one suggested creating or editing these files as it seems to provide mode options (Restart, RestartSec, ExecStop, etc. )

@tiagoams Creating a service file was just not the question here, since the OP states that a service minidlna already exists but does not autostart. But thanks for the pointer to service files under /etc , I added that to the answer now.

update-rc.d service_name defaults 
update-rc.d -f service_name remove 

defaults => default run levels 2,3,4 and 5

update-rc.d tomcat7 defaults 

When I do this command, I get «System start/stop links for /etc/init.d/tomcat7 already exist». however, when I reboot, it does not start tomcat, I always have to do «service tomcat7 start».

in my case sudo update-rc.d myservice default always completes with no output and the service won’t start on boot-up or with sudo service myservice start which also completes silently. sudo /etc/init.d/myservice start works however

Sometimes you need to run a script on boot process, for example run an iptables config at boot process. So you don’t have to run the script manually every rebooting.

You can run your script on boot process in Ubuntu by adding it to /etc/init.d/rc.local file. Look the steps below.

    Open /etc/rc.local file with this command:

sh /home/ivan/iptables.sh echo 'Iptable Configured!' 

Comments of /etc/init.d/rc.local in Ubuntu 12.04 says «Short-Description: Run /etc/rc.local if it exist», so maybe adding the scripts to /etc/rc.local would be better idea?

Vadim, rc.local is just run at boot, nothing is achieved on shutdown. The process would most likely be killed by the OS on shutdown.

FYI: The difference between rc.local vs adding it to init, is that rc.local is executed at the end of the init startup sequence, rather than as part of it

In ubuntu version 18.04 TLS, I found that update-rc.d does not work fine if there is no specific comment block in the start script that looks like this:

### BEGIN INIT INFO # Provides: myprogram # Required-Start: $ local_fs $ remote_fs $ syslog $ network $ time # Required-Stop: $ local_fs $ remote_fs $ syslog $ network # Default-start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: myprogram some description ### END INIT INFO 

Источник

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?

Читайте также:  Adobe flash player linux терминал

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.

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

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