Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
demand
In the test process, there may be a need to restart the server, but the application to the service and more, then one by one manual execution, and slightly tedious, think of a once and for all way is to set the boot since the start, but, but also afraid of a sudden CPU overflowing server down? These problems need to be solved one by one, manually baidu a Linux server can complete boot from several ways!
/etc/rc.local /etc/rc.d/rc.local
Before exit 0, add the absolute path to start the shell script and authorize < executable > : chmod +x XXX.sh
Finally, you need to authorize the startup file chmod 755 /etc/rc.local
Comment: Can say simple and crude
Second, you can save your script to /etc/profile.d/. The system automatically executes all shell scripts in the directory
Comments: Simple
Step 3: Use the chkconfig command to set: write the shell script, place the /etc/init.d/ XXX path, add the service list,
Comments: Tedious
/etc/rc.d/init.d/serverName<variable>
The chkconfig command
The chkconfig command is used to update (start or stop) and query the run-level information of system services. Keep in mind that chkconfig does not automatically disable or activate a service immediately, it simply changes the symbolic link.Copy the code
Use grammar:
Chkconfig [--add][--del][--list][System services] or chkconfig [--level < level code >][System services][ON /off/reset] Chkconfig displays the usage when running without parameters. If the service name is added, it checks whether the service is started at the current runlevel. Returns true if so, false otherwise. If on, off, or reset is specified after the service name, chkconfig changes the startup information for the specified service. On and off indicate that the service is started and stopped, respectively, and reset indicates that the service's startup information is reset, regardless of what the initialization script in question specified. On and OFF. By default, the system only applies to runlevels 3, 4, and 5. Reset applies to all runlevels.Copy the code
Parameter Usage:
--add adds the specified system service, allowing the chkconfig directive to manage it, and adds data to the system startup narrative file. --del deletes the specified system service, no longer managed by the chkconfig directive, and deletes the related data in the system startup narrative file. --level < level code > server_name ON /off Specifies the execution level at which the read system service should be enabled or disabled. Level 0: shutdown Level 1: single-user mode Level 2: Multi-user command line mode without network connection Level 3: Multi-user command line mode with network connection Level 4: unavailable Level 5: Multi-user mode with GUI Level 6: restartCopy the code
It should be noted that the Level option specifies the runlevel to view, not necessarily the current runlevel. There can be only one start script or stop script per run level. When switching runlevels, init does not restart the started service or stop the stopped service again.
Chkconfig --list [name] : Displays the running status of all run-level system services (on or off). If name is specified, only the state of the specified service at different runlevels is displayed. Chkconfig --add name: Adds a new service. Chkconfig ensures that each runlevel has a start (S) or kill (K) entry. If missing, it will be built automatically from the default init script. Chkconfig --del name: delete service from /etc/rc[0-6].d Chkconfig [--level levels] name: sets whether a service is started, stopped, or reset at the specified runlevel.Copy the code
Run-level file:
Each service managed by ChkConfig requires two or more lines of comment in the corresponding script under init.d. The first line tells ChkConfig the default run level to start and the start and stop priorities. If a service is not started at any runlevel by default, use - instead of the runlevel. The second line describes the service and can be commented with a \ cross-line comment.
For example, random.init contains three lines:
#! /bin/sh # chkconfig: 2345 20 80 # description: Saves and restores system entropy pool for \ # higher quality random number generation.Copy the code
The above three lines must not be dropped, otherwise chkconfig –add server_name will report an error
The next two values of chkconfig; 20 indicates the start level. A smaller value indicates the start level. 80 indicates the shutdown level
Add a user-defined service file to the service system and manage it through systemctl
[Unit]# ExecStart # ExecReload for the specific command ExecReload ExecStop # PrivateTmp=True # Assign a separate temporary space to the service. [Install] Set up for the service installation. WantedBy=multi-userCopy the code
Note: Start, restart, and stop commands all require absolute paths
Systemctl list-unit-files # Check all service lists systemctl enable server_name # Add service systemctl disable server_name # Remove service systemctl List-units --type=service # View services startedCopy the code
Fifth: it is added in scheduled task management
crontab -e
@reboot /home/user/test.sh
Copy the code
Comments: Simple to blast