开发中我们经常需要设置一些自启动服务,用来做一些定时任务或者是服务器遇到问题重启时自动启动服务;
CentOS设置自启动
在CentOS 6中,可以通过在/etc/init.d/添加自启动脚本来设置自启动;
在CentOS 7中,设置自启动的方法发生了改变:虽然我们仍然可以使用脚本来添加自启动,但是官方更建议使用服务来设置自启动;
通过配置系统服务来设置自启动
已经存在的系统服务
在CentOS 7中,一些软件程序会在安装时自动创建服务,比如:Apache、PHP、MySQL、Nginx等等;
因此,对于已经创建好的服务,唯一要做的就是把服务设置成自启动就可以了;
以MySQL为例:
安装完成MySQL之后,执行语句把MySQL服务设置成自启动:
1 | # systemctl enable mysqld |
设置完成之后,检查一下状态:
1 | # systemctl status mysqld |
会打印出类似下列信息:
1 | ● mysqld.service - MySQL Server |
如果不需要自启动了,可以取消服务的自启动:
1 | # systemctl disable mysqld |
创建自定义服务
当然,我们也可以自己创建服务;
例如,创建一个名为sample.service的服务:
1 | # vi /etc/systemd/system/sample.service |
其中:
- After:代表要在其他的某些程序完成之后再执行;
- Type:代表服务执行顺序和方式;
Type=simple(默认值):systemd认为该服务将立即启动,服务进程不会fork,如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型;Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功;对于常规的守护进程(daemon);使用此启动类型应同时指定PIDFile,以便systemd能够跟踪服务的主进程;Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务;可能需要同时设置RemainAfterExit=yes使得 systemd 在服务进程退出之后仍然认为服务处于激活状态;Type=notify: 与Type=simple相同,但约定服务会在就绪后向 systemd 发送一个信号;这一通知实现由libsystemd-daemon.so提供;Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪;Type=idle: 服务会延迟启动,一直到其他服务都启动完成之后才会启动此服务;
- WantedBy代表启动目标;
- target是一个类似而又不同于启动级别
(runlevel)的概念;参考:Systemd服务简介;
其他配置可以参考一下其他软件的配置:
1 # cat /usr/lib/systemd/system/postgresql-10.service比如PostgreSQL的service文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 # It's not recommended to modify this file in-place,because it will be
# overwritten during package upgrades. If you want to customize,the
# best way is to create a file "/etc/systemd/system/postgresql-10.service",
# containing
# .include /usr/lib/systemd/system/postgresql-10.service
# ...make your changes here...
# For more info about custom unit files,see
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F
# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.
# Note: do not use a PGDATA pathname containing spaces,or you will
# break postgresql-setup.
[Unit]
Description=PostgreSQL 10 database server
Documentation=https://www.postgresql.org/docs/10/static/
After=syslog.target
After=network.target
[Service]
Type=notify
User=postgres
Group=postgres
# Note: avoid inserting whitespace in these Environment= lines,or you may
# break postgresql-setup.
# Location of database directory
Environment=PGDATA=/var/lib/pgsql/10/data/
# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0
ExecStartPre=/usr/pgsql-10/bin/postgresql-10-check-db-dir ${PGDATA}
ExecStart=/usr/pgsql-10/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
# Do not set any timeout value,so that systemd will not kill postmaster
# during crash recovery.
TimeoutSec=0
[Install]
WantedBy=multi-user.target
通过自启动脚本来设置
创建一个名为test_script.sh的脚本并写入内容:
1 | # vi /var/tmp/test_script.sh |
检查文件权限:
1 | # ls -lrt /var/tmp/test_script.sh |
如果没有权限,则添加可执行权限:
1 | # chmod +x /var/tmp/test_script.sh |
将/etc/rc.d/rc.local标记为可执行文件:
在CentOS 7中/etc/rc.d/rc.local文件的权限被降低了,开机的时候执行的脚本是不能启动一些服务的,执行下面的命令将文件标记为可执行的文件:
1 | # chmod +x /etc/rc.d/rc.local |
随后,打开/etc/rc.d/rc.local文件,写入上面的文件:
1 |
|
虽然两种方法都可以设置自启动,但是不建议通过脚本来设置,建议通过设置服务来启动;
附录
文章参考: