CentOS 7 startup services and script

1. Adding a boot service

  1. Create a configuration file with the suffix service, such as creating the file test.service. The contents of the file are as follows:

    [Unit]
    Description=Test
    After=network.target
    [Service]
    Type=simple
    User=nobody
    Restart=on-failure
    RestartSec=5s
    TimeoutStartSec=0
    ExecStart=/usr/local/test
    [Install]
    WantedBy=multi-user.target

    Parameter definition:

    • After-dependency, indicating that the service is started after the network is started.
      How to handle dependencies:
      When using systemd, the dependencies can be resolved by writing the cell configuration file correctly. Typically, unit A requires unit B to run before A starts. In this case, add Requires=B and After=B to the [Unit] section of the unit A configuration file. If this dependency is optional, add Wants=B and After=B. Note that Wants= and Requires= do not mean After= , ie if the After= option is not specified, the two units will be started in parallel.

      Dependencies are often used on services rather than targets. For example, network.target is typically introduced by a service that configures a network interface, so it is sufficient to queue a custom unit after the service because network.target has already started.
    • Type-type
      Type=simple : (default) systemd thinks the service will start immediately. The service process will not fork. If the service is to start other services, do not use this type to start unless the service is socket activated.
      Type=forking : systemd thinks that when the service process forks and the parent process exits, the service starts successfully. For a regular daemon, you can start with this type unless you are sure that this startup method does not meet your needs. Use this startup type to specify PIDFile= at the same time so that systemd can track the main process of the service.
      Type=oneshot : This option is for a service that performs only one task and then exits immediately. It may be necessary to set RemainAfterExit=yes at the same time so that systemd still considers the service to be active after the service process exits.
      Type=notify : Same as Type=simple, but the contracted service will send a signal to systemd when it is ready. The implementation of this notification is provided by libsystemd-daemon.so.
      Type=dbus : If started in this way, when the specified BusName appears on the DBus system bus, systemd considers the service ready.
      Type=idle : systemd will wait for all tasks to be processed before starting to execute units of type idle. Other behaviors are similar to Type=simple.
    • ExecStart – The command line to start the service, including the execution file path and program parameters.
    • WantedBy-Which user group to install this service
      multi-user.target end user

      Graphical.target desktop user

      Default.target default user
  2. Put the configuration file you just created in
    /usr/lib/systemd/system user package installation directory
    or
    /etc/systemd/system/ System Administrator Installation Directory

  3. Run the following command to install the service:

    systemctl enable test.service       #installation service
    systemctl start test.service        #Startup service
    systemctl daemon-reload             #reload

2. Add a boot script

  1. Move the script to the /etc/rc.d/init.d directory

    mv test.sh /etc/rc.d/init.d
  2. Increase the executable permissions of the script

    chmod +x /etc/rc.d/init.d/test.sh
  3. Add a script to the boot autostart project

    cd /etc/rc.d/init.d
    chkconfig --add test.sh
    chkconfig test.sh

Note:
You directly put your usual scripts written by chkconfig --add so sure to report service test.sh does not support chkconfig error, but you only need to add the following code in the head of the file test.sh where just on the next line of ! /bin/bash

#chkconfig: 345 88 14       #345 Representing permissions

2. Add a boot script(II)

The above method may cause behavior problems that are inconsistent with expectations in many cases, so I still recommends implementing the startup script in the manner recommended by Centos official:

  1. Create your_startup.service file
    # touch /usr/lib/systemd/system/your_startup.service
  2. Edit your_startup.service file
    # vim /usr/lib/systemd/system/your_startup.service
  3. Add the following content, save and exit
    [Unit]
    Description=start your_startup service
    Requires=graphical.target
    After=graphical.target
    [Service]
    Type=forking
    User=root
    Group=root
    Restart=always
    TimeoutSec=5
    IgnoreSIGPIPE=no
    KillMode=process
    GuessMainPID=no
    RemainAfterExit=no
    ExecStart=/etc/rc.d/init.d/your_startup.sh
    [Install]
    WantedBy=graphical.target
  4. Add execute permission to your_startup.sh startup script
    # chmod 755 /etc/rc.d/init.d/your_startup.sh
  5. Setting startup
    # systemctl enable /usr/lib/systemd/system/your_startup.service
  6. Reboot
    # reboot
  7. Check if your_startup.sh started successfully?
5 1 vote
Article Rating
赞(0) 打赏
未经允许不得转载:iemblog » CentOS 7 startup services and script
Subscribe
Notify of
guest

0 COMMENTS
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
免责声明:本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有,请在下载后24小时之内自觉删除,若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系站长删除,我们将及时处理! Disclaimer: Most of the download resources on this site are collected on the Internet, and are only used for learning and communication. The copyright belongs to the original author. Please consciously delete within 24 hours after downloading. If you use it for commercial purposes, please purchase the original version. If the content posted on this site violates your rights, please contact us to delete it, and we will deal with it in time!

联系我们 Contact us

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

0
Would love your thoughts, please comment.x
()
x