systemctl start xxxxx.service命令无法正常启动服务

在Linux系统中安装Keepalived后,通过systemctl启动服务失败。原因是启动命令未指定配置文件。通过查看systemctl状态和环境文件发现KEEPALIVED_OPTIONS缺少配置路径。修改环境文件指定配置后,服务成功启动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目场景:

在Linux系统下,源码包解压安装了Keepalived服务,安装完后使用systemctl start keepalived.service命令启动服务未能成功启动服务。


问题描述:

运行了systemctl start keepalived.service命令启动服务;

然后使用ps -aux | grep keepalived 查看服务启动情况;

输出结果为:

root     21068  0.0  0.0 112812   976 pts/0    S+   17:04   0:00 grep --color=auto keepalived

这是服务没有运行起来呀。


原因分析:

运行systemctl status keepalived.service命令查看服务状态;

输出结果为:

● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2021-03-23 16:36:49 CST; 6s ago
  Process: 20699 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=7)

Mar 23 16:36:49 tinghs systemd[1]: Starting LVS and VRRP High Availability Monitor...
Mar 23 16:36:49 tinghs Keepalived[20699]: Starting Keepalived v2.2.2 (03/05,2021)
Mar 23 16:36:49 tinghs systemd[1]: keepalived.service: control process exited, code=exited status=7
Mar 23 16:36:49 tinghs systemd[1]: Failed to start LVS and VRRP High Availability Monitor.
Mar 23 16:36:49 tinghs systemd[1]: Unit keepalived.service entered failed state.
Mar 23 16:36:49 tinghs systemd[1]: keepalived.service failed.

Mar 23 16:36:49 tinghs systemd[1]: Failed to start LVS and VRRP High Availability Monitor.

这个是它报红的一行。

使用journalctl -xe 命令查看详细信息;

输出结果为:

Mar 23 16:36:49 tinghs Keepalived[20699]: Starting Keepalived v2.2.2 (03/05,2021)
Mar 23 16:36:49 tinghs Keepalived[20699]: Running on Linux 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 (built for Linux 3.10.0)
Mar 23 16:36:49 tinghs Keepalived[20699]: Command line: '/usr/local/keepalived/sbin/keepalived' '-D'
Mar 23 16:36:49 tinghs Keepalived[20699]: CPU usage (self/children) user: 0.001035/0.000000 system: 0.014492/0.000000
Mar 23 16:36:49 tinghs Keepalived[20699]: Stopped Keepalived v2.2.2 (03/05,2021)
Mar 23 16:36:49 tinghs systemd[1]: keepalived.service: control process exited, code=exited status=7
Mar 23 16:36:49 tinghs systemd[1]: Failed to start LVS and VRRP High Availability Monitor.
-- Subject: Unit keepalived.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit keepalived.service has failed.
--
-- The result is failed.
Mar 23 16:36:49 tinghs systemd[1]: Unit keepalived.service entered failed state.
Mar 23 16:36:49 tinghs systemd[1]: keepalived.service failed.
Mar 23 16:36:49 tinghs polkitd[685]: Unregistered Authentication Agent for unix-process:20693:165200446 (system bus name :1.1022, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus)

Mar 23 16:36:49 tinghs Keepalived[20699]: Command line: ‘/usr/local/keepalived/sbin/keepalived’ ‘-D’

从这一行可以看出,启动服务时没有指定配置文件。

我们去查看一下/usr/lib/systemd/system/keepalived.service 文件,运行 cat /usr/lib/systemd/system/keepalived.service命令;

输出结果为:

[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/keepalived.pid
KillMode=process
EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived
ExecStart=/usr/local/keepalived/sbin/keepalived  $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived

根据这一行去寻找 config , 运行 cat /usr/local/keepalived/etc/sysconfig/keepalived 命令;

输出结果为:

# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#

KEEPALIVED_OPTIONS="-D"

KEEPALIVED_OPTIONS="-D"

重点就是上面这一行了;


解决方案:

运行 vi /usr/local/keepalived/etc/sysconfig/keepalived 命令,shift+G 将光标移动至文件末尾,点击 i ,将文件改为:

# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)


#KEEPALIVED_OPTIONS="-D"

# 根据个人安装的目录指定keepalived配置文件
KEEPALIVED_OPTIONS="-f /usr/local/keepalived/etc/keepalived/keepalived.conf -D -S 0"

点击 Esc ,输入:wq ,点击Enter,保存并退出。

再次运行systemctl start keepalived.service命令启动服务,就可以启动成功了。

systemctl 服务相关命令

# 重新加载服务
systemctl daemon-reload  
# 启动服务
systemctl start keepalived.service 
# 停止服务
systemctl stop keepalived.service
# 重启服务
systemctl restart keepalived.service 
# 查看服务状态
systemctl status keepalived.service
# 设置开机自动启动服务
systemctl enable keepalived.service  
# 取消开机自动启动服务
systemctl disable keepalived.service 
# 查看服务是否开机启动
systemctl is-enabled keepalived.service
# 查看已启动的服务列表
systemctl list-unit-files|grep enabled
# 查看启动失败的服务列表
systemctl --failed

使用systemctl启动服务的步骤如下: 1. 首先,确定服务的脚本文件存放位置。在CentOS 7上,系统级的服务脚本文件通常存放在`/usr/lib/systemd/system/`目录下,以`.service`结尾。这些脚本文件包括了服务的配置信息和启动命令等。 2. 使用`systemctl start [服务名]`命令启动服务。其中,[服务名]是指服务脚本文件的名称(不包括扩展名)。例如,如果要启动nginx服务,可以使用`systemctl start nginx`命令。 3. 如果服务启动失败,可以使用`systemctl status [服务名]`命令来查看错误信息和其他服务信息。根据报错信息进行相应的修改,直到服务可以成功启动。 4. 如果需要重新启动已经运行的服务,可以使用`systemctl restart [服务名]`命令。 5. 如果需要停止正在运行的服务,可以使用`systemctl stop [服务名]`命令。 请注意,在使用systemctl启动服务之前,确保已经正确安装了该服务,并且相关的配置文件已经正确设置。此外,某些服务可能需要以root权限来执行相关命令。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [systemctl添加服务启动](https://blog.csdn.net/a1058926697/article/details/105246369)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷赫斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值