1. Adding a boot service
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
- 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
[Unit]section of the unit A configuration file. If this dependency is optional, add
After=B. Note that
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.
(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 servicemulti-user.target end user
Graphical.target desktop user
Default.target default user
- After-dependency, indicating that the service is started after the network is started.
Put the configuration file you just created in
/usr/lib/systemd/systemuser package installation directory
/etc/systemd/system/System Administrator Installation Directory
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
Move the script to the /etc/rc.d/init.d directory
mv test.sh /etc/rc.d/init.d
Increase the executable permissions of the script
chmod +x /etc/rc.d/init.d/test.sh
Add a script to the boot autostart project
cd /etc/rc.d/init.d chkconfig --add test.sh chkconfig test.sh
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
#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:
- Create your_startup.service file
# touch /usr/lib/systemd/system/your_startup.service
- Edit your_startup.service file
# vim /usr/lib/systemd/system/your_startup.service
- 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
- Add execute permission to your_startup.sh startup script
# chmod 755 /etc/rc.d/init.d/your_startup.sh
- Setting startup
# systemctl enable /usr/lib/systemd/system/your_startup.service
- Check if your_startup.sh started successfully?