Linux的Service之解析

深入解析Linux系统中Name or service not known错误的排查与修复 本文深入解析Linux系统中常见的'Name or service not known'错误,详细介绍了DNS和hostname配置的排查与修复方法。通过分析/etc/hosts文件、DNS配置及实战排错五步法,帮助系统管理员快速解决主机名解析问题,提升Linux服务器稳定性。 阅读详情



Linux系统下的服务,可以在/etc/rc.d/init.d目录下查询到。

比如常用的Apache服务,对应的是httpd,如果需要启动该服务,就可以使用service命令进行控制。

[root@localhost rc.d]# ls -l /etc/rc.d/init.d/
total 416
-rwxr-xr-x. 1 root root  1708 Aug 26  2010 abrtd
-rwxr-xr-x. 1 root root  1725 Dec  3  2009 acpid
-rwxr-xr-x. 1 root root  2122 Jun  8  2010 atd
-rwxr-xr-x. 1 root root  3378 Jan 14  2010 auditd
-rwxr-xr-x. 1 root root  3740 Jul  1  2010 autofs
-rwxr-xr-x. 1 root root  2540 Jun 30  2010 avahi-daemon
-rwxr-xr-x. 1 root root   710 Jun 30  2010 bluetooth
-rwxr-xr-x. 1 root root  2092 Jun 29  2010 certmonger
-rwxr-xr-x. 1 root root  5335 Jul 14  2010 cgconfig
-rwxr-xr-x. 1 root root  3266 Jul 14  2010 cgred
-rwxr-xr-x. 1 root root 11042 Jul 20  2010 cpuspeed
-rwxr-xr-x. 1 root root  2853 Apr 23  2010 crond
-rwxr-xr-x. 1 root root  2947 Jul 17  2010 cups
-rwxr-xr-x. 1 root root  1407 Nov 20  2009 dnsmasq
-rwxr-xr-x. 1 root root  1987 May  6  2010 dovecot
-rwxr-xr-x. 1 root root  2785 Aug 10  2010 firstboot
-rw-r--r--. 1 root root 17911 Sep  1  2010 functions
-rwxr-xr-x. 1 root root  1701 Jul 28  2010 haldaemon
-rwxr-xr-x. 1 root root  5587 Sep  1  2010 halt
-rwxr-xr-x. 1 root root 17826 Apr 27  2010 hsqldb
-rwxr-xr-x. 1 root root  3386 Oct 19 20:05 httpd
-rwxr-xr-x. 1 root root  8654 Jul 19  2010 ip6tables
-rwxr-xr-x. 1 root root  8559 Jul 19  2010 iptables
-rwxr-xr-x. 1 root root  1873 Oct 10  2009 irqbalance
-rwxr-xr-x. 1 root root  3789 Aug  5  2010 iscsi
-rwxr-xr-x. 1 root root  3671 Jul 11  2010 iscsid
-rwxr-xr-x. 1 root root 12210 Aug 30  2010 kdump
-rwxr-xr-x. 1 root root   652 Sep  1  2010 killall
-rwxr-xr-x. 1 root root  2549 Aug 18  2010 libvirtd
-rwxr-xr-x. 1 root root  7802 Aug 18  2010 libvirt-guests
-r-xr-xr-x. 1 root root  2442 Aug 18  2010 lvm2-monitor
-rwxr-xr-x. 1 root root  2571 Apr 10  2010 mdmonitor
-rwxr-xr-x. 1 root root  2200 Aug 11  2010 messagebus
-rwxr-xr-x. 1 root root  1169 Feb 23  2010 microcode_ctl
-rwxr-xr-x. 1 root root  5424 Jul 16  2010 mysqld
-rwxr-xr-x. 1 root root  2984 Sep  1  2010 netconsole
-rwxr-xr-x. 1 root root  5402 Sep  1  2010 netfs
-rwxr-xr-x. 1 root root  6454 Sep  1  2010 network
-rwxr-xr-x. 1 root root  2205 Aug 11  2010 NetworkManager
-rwxr-xr-x. 1 root root  5982 Aug 26  2010 nfs
-rwxr-xr-x. 1 root root  3517 Aug 26  2010 nfslock
-rwxr-xr-x. 1 root root  2515 Sep  8  2010 nscd
-rwxr-xr-x. 1 root root  1675 Feb 26  2010 nslcd
-rwxr-xr-x. 1 root root  1923 Oct 22  2009 ntpd
-rwxr-xr-x. 1 root root  1866 Oct 22  2009 ntpdate
-rwxr-xr-x. 1 root root  2261 Jan 30  2010 oddjobd
-rwxr-xr-x. 1 root root  1866 Mar  5  2010 portreserve
-rwxr-xr-x. 1 root root  3852 May 26  2010 postfix
-rwxr-xr-x. 1 root root  1556 Apr 19  2010 psacct
-rwxr-xr-x. 1 root root  2992 Jul 28  2010 qpidd
-rwxr-xr-x. 1 root root  1559 May 20  2009 rdisc
-rwxr-xr-x. 1 root root  1815 Aug 25  2010 restorecond
-rwxr-xr-x. 1 root root  2898 Mar 20  2010 rhnsd
-rwxr-xr-x. 1 root root  2073 Aug 16  2010 rpcbind
-rwxr-xr-x. 1 root root  3112 Aug 26  2010 rpcgssd
-rwxr-xr-x. 1 root root  2706 Aug 26  2010 rpcidmapd
-rwxr-xr-x. 1 root root  3084 Aug 26  2010 rpcsvcgssd
-rwxr-xr-x. 1 root root  2262 Mar 31  2010 rsyslog
-rwxr-xr-x. 1 root root  1698 Aug 25  2010 sandbox
-rwxr-xr-x. 1 root root  2056 Apr 12  2010 saslauthd
-rwxr-xr-x. 1 root root   647 Sep  1  2010 single
-rwxr-xr-x. 1 root root  3002 Jan 29  2010 smartd
-rwxr-xr-x. 1 root root  2162 Aug  4  2010 snmpd
-rwxr-xr-x. 1 root root  1738 Aug  4  2010 snmptrapd
-rwxr-xr-x. 1 root root  1664 Mar 18  2010 spamassassin
-rwxr-xr-x. 1 root root  4537 Aug 12  2010 sshd
-rwxr-xr-x. 1 root root  2521 Sep  3  2010 sssd
-rwxr-xr-x. 1 root root  1265 Jul 26  2010 sysstat
-rwxr-xr-x. 1 root root  4751 Jul 14  2010 tgtd
-rwxr-xr-x. 1 root root  9697 Aug 14  2010 tomcat6
-rwxr-xr-x. 1 root root  2296 Sep  1  2010 udev-post
-rwxr-xr-x. 1 root root  3126 Nov 24  2010 vncserver
-rwxr-xr-x. 1 root root  2824 May 26  2010 vsftpd
-rwxr-xr-x. 1 root root  1866 May 14  2010 wpa_supplicant
-rwxr-xr-x. 1 root root  3555 Jun  4  2010 xinetd
-rwxr-xr-x. 1 root root  4799 Jun 24  2010 ypbind
[root@localhost rc.d]# 


常用的Linux服务的管理命令有:
service httpd
service httpd status
service httpd start
service httpd stop
service httpd restart
service httpd condrestart
service httpd try-restart
service httpd force-reload
service httpd fullstatus
service httpd graceful
service httpd help
service httpd configtest


其实该录下的httpd,只是一个bash脚本而已,通过设定一些配置文件(比如log),来启动、停止、重新启动httpd服务。

[root@localhost rc.d]# cat /etc/rc.d/init.d/httpd
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#	       server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server 
#  implementing the current HTTP standards.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        echo ""
        echo "For Forest: httpd is started to be run"
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd a delay of >10 second is required before SIGKILLing the
# httpd parent; this gives enough time for the httpd parent to SIGKILL any
# errant children.
stop() {
	echo -n $"Stopping $prog: "
	killproc -p ${pidfile} -d 10 $httpd
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
        status -p ${pidfile} $httpd
	RETVAL=$?
	;;
  restart)
	stop
	start
	;;
  condrestart|try-restart)
	if status -p ${pidfile} $httpd >&/dev/null; then
		stop
		start
	fi
	;;
  force-reload|reload)
        reload
	;;
  graceful|help|configtest|fullstatus)
	$apachectl $@
	RETVAL=$?
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
	RETVAL=2
esac

exit $RETVAL
[root@localhost rc.d]# 


其实,所有的Linux Service都支持这些命令,并在相应的脚本中用bash实现这些操作。

很简单吧?


至于service,其实只是一个bash脚本而已,存放在/sbin/目录下。

[root@localhost rc.d]# which service
/sbin/service
[root@localhost rc.d]# cat /sbin/service
#!/bin/sh

. /etc/init.d/functions

VERSION="$(basename $0) ver. 0.91"
USAGE="Usage: $(basename $0) < option > | --status-all | \
[ service_name [ command | --full-restart ] ]"
SERVICE=
SERVICEDIR="/etc/init.d"
OPTIONS=

if [ $# -eq 0 ]; then
   echo "${USAGE}" >&2
   exit 1
fi

cd /
while [ $# -gt 0 ]; do
  case "${1}" in
    --help | -h | --h* )
       echo "${USAGE}" >&2
       exit 0
       ;;
    --version | -V )
       echo "${VERSION}" >&2
       exit 0
       ;;
    *)
       if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
          cd ${SERVICEDIR}
          for SERVICE in * ; do
            case "${SERVICE}" in
              functions | halt | killall | single| linuxconf| kudzu)
                  ;;
              *)
                if ! is_ignored_file "${SERVICE}" \
		    && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
                  env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" status
                fi
                ;;
            esac
          done
          exit 0
       elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
          SERVICE="${1}"
          if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
            env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" stop
            env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" start
            exit $?
          fi
       elif [ -z "${SERVICE}" ]; then
         SERVICE="${1}"
       else
         OPTIONS="${OPTIONS} ${1}"
       fi
       shift
       ;;
   esac
done

if [ -f "${SERVICEDIR}/${SERVICE}" ]; then
   env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" ${OPTIONS}
else
   echo $"${SERVICE}: unrecognized service" >&2
   exit 1
fi
[root@localhost rc.d]# 


至于Linux系统下支持的service,有非常多,而且每个人都可以设计自己的service,常用的service已经有人收集了一下,那是相当的多,参考一下吧。

Linux系统下service大全: Linux下的service大全

JAVA进阶面试题:如何设计一个高并发系统? 如何设计一个高并发系统? 面试官心理分析 说实话,如果面试官问你这个题目,那么你必须要使出全身吃奶劲了。为啥?因为你没看到现在很多公司招聘的 JD 里都是说啥,有高并发就经验者优先。 如果你确实有真才实学,在互联网公司里干过高并发系统,那你确实拿 offer 基本如探囊取物,没啥问题。面试官也绝对不会这样来问你,否则他就是蠢。 假设你在某知名电商公司干过高并发系统,用户上亿,一天流量几十亿,高峰期并发量上万,甚至是十万。那么人家一定会仔细盘问你的系统架构,你们系统啥架构?怎么部署的?部署了多少台机器 阅读详情

相关推荐

保姆级教程:用中移ML307模组对接高德/OneOS定位服务,从配置apikey到解析+MLBSLOC状态码

本文详细介绍了如何使用中移ML307系列模组对接高德/OneOS定位服务,从配置apikey到解析+MLBSLOC状态码的全过程。涵盖高德定位服务和OneOS定位服务的配置步骤、常见错误处理及性能优化技巧,帮助开发者快速实现基站定位功能,提升物联网项目的定位精度和稳定性。

weixin_30632089的博客 505

linux 之.service文件简介

linux service 文件简介

qq_42673067的博客 4245

Win10 + Linux 双系统删除(Linux)

删除Linux所使用的磁盘分区 进入此电脑->计算机->管理->磁盘管理

weixin_52247624的博客 943

linux service

linux service

weixin_38301116的博客 3058

AVS3代码阅读HPM4.0

参考AVS3代码阅读(HPM6.0)(一.整体架构以及CU划分部分代码) 版本:HPM4.0 核心文件:app_encoder.c   要做的是帧间预测,所以前面的函数先暂时不管了,挖个坑以后再填。直接从enc_pic看起。 enc_pic()    ...

CRR的博客 3187

全志T113s3主线linux开发(0)--基础准备

嵌入式Linux设备的启动大概是三步:1)启动芯片+内存:常见的是Uboot,不过全志的Uboot主线对SPI NAND 不太友好,用TF卡就方便。对于T113S芯片,推荐使用Awboot,简陋但快速。开发阶段用这个就方便好多,对SPINAND支持也方便。2)启动linux内核:主线内核以下通篇都用6.13以上的内核。对比tina5.0还在用5.4 ,主要是性能、安全上有变化。6.0以上内核在devicetree的编写具体格式上有了变化,网上大量tina5.0和5.4内核的编写方式不适用。

2301_79234138的博客 2247

Linux之service服务-实现程序脚本开机自启

使用service服务实现程序脚本开机自启

qq_34885669的博客 1万+

2025年数学建模美赛(MCM)C题阅读与思考

2025年数学建模美赛(MCM)C题阅读与思考

carefreekid123的博客 7339

Linux的.serivice文件介绍

linux中.servic文件是服务的配置文件,通过systemctl进行操作。存放位置。

Bussy的博客 3186

基于STM32、HAL库的IP2736U_EF快充协议芯片简介及驱动程序设计

@param connected: 连接状态指针 (1=已连接, 0=未连接)= 0x2736) // 检查厂商ID。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。* @param hip: IP2736U句柄指针。

corlin6688的博客 430

假新闻检测论文分享(30)MMDFND: Multi-modal Multi-Domain Fake News Detection

现有的假新闻检测方法通常集中在单一领域的新闻上,如健康或政治。然而,社交媒体上的新闻往往来自多个领域,这给单一领域的假新闻检测方法带来了两个主要挑战:数据稀缺性:处理较小、单一领域的新闻数据集时,由于训练数据的稀缺,模型的有效性显著下降。跨领域检测的准确性下降:单一领域的假新闻检测方法在跨领域新闻数据集上的准确性大幅降低。缓解领域间模态语义偏差:通过DPLE网络提取跨领域和领域特定的知识,缓解了不同领域间文本和图像的语义差异。

weixin_41964296的博客 669

总结:linux之Service

service与systemctl关系梳理 开机启动梳理

w2009211777的专栏 1万+

第1章 机器人SLAM算法工程师主要做什么的?(附SLAM学习路线及资料推荐)

对于想从事机器人与自动驾驶领域的在校大学生、研究生来说,建图定位算法工程师是一个极具发展潜力的岗位,而SLAM知识体系则是胜任该岗位的核心支撑。从基础的计算机工具、C++语言,到核心的开源框架、数学知识,再到硬件衔接与项目实践,每一个环节都不可或缺。建议大家结合自身兴趣确定侧重方向(如激光SLAM、融合定位),先通过开源项目建立实践认知,再补全理论知识,同时积极参与社区交流。

行知SLAM 952

linux service命令解析

转载: linux service命令解析_fmwind的博客-优快云博客_linux service命令 我们平时都会用 service xxx start来启动某个进程,那么它背后究竟执行了什么? 其实service的绝对路径为/sbin/service ,打开这个文件cat /sbin/service,我们会发现其实它就是一个很 普通的shell脚本: [root@zejin240 ~]# cat /sbin/service #!/bin/sh . /etc/init.d/functi

modi000的博客 2624

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

linux service命令解析(重要)

我们平时都会用service xxx start来启动某个进程,那么它背后究竟执行了什么? 其实service的绝对路径为/sbin/service ,打开这个文件cat /sbin/service,我们会发现其实它就是一个很普通的shell脚本: [root@zejin240 ~]# cat /sbin/service #!/bin/sh . /etc/init.d/functions ...

Sunshine 3084

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

linux 下 service 命令解析

linux service命令解析 我们平时都会用service xxx start来启动某个进程,那么它背后究竟执行了什么?其实service的绝对路径为/sbin/service ,打开这个文件cat /sbin/service,我们会发现其实它就是一个很普通的shell脚本:[root@zejin240 ~]# cat /sbin/service #!/bin/sh . /etc/ini...

冬的博客 647

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

linux中systemctl命令理解以及.service文件参数解析

linux中systemctl命令理解以及.service文件参数解析一、systemctl介绍二、利用.service文件管理程序的步骤三、.service配置文件参数详解1. Unit2. Service3. Install四、systemctl命令详解四、参考 一、systemctl介绍 Systemctl是linux系统继init.d之后的一个systemd工具,主要负责控制systemd系统和管理系统服务 systemd即为system daemon(系统守护进程),是linux下的一种init软

weixin_45606237的博客 2万+

Service本质:从Linux systemd到K8s与Android的运行契约解析

Service是现代软件系统中保障逻辑长期稳定运行的核心抽象,它既非简单进程,也非功能模块,而是操作系统、容器编排与移动平台共同定义的‘资源契约’与‘状态机’。其底层原理涵盖进程生命周期管理、依赖拓扑建模、资源隔离控制及跨平台保活机制。技术价值在于实现可管、可扩、可溯的生产级可靠性,广泛应用于微服务治理、后台任务调度、IoT设备守护及云原生网关部署等场景。理解service,就是理解systemd的Type=notify、Kubernetes的EndpointSlice与Android Foreground

weixin_29698641的博客 337

Systemd 与服务管理:深入解析 Linux 启动项与 Service 配置

传统 SysVinit 因顺序启动、脚本依赖和功能单一逐渐被淘汰,而 systemd 通过并行启动、依赖管理和声明式配置成为主流。其核心组件包括 journald、udev等,支持动态监控与资源优化。systemd 以单元文件定义服务行为,替代复杂脚本,显著提升启动效率。例如,Service 单元通过 Type、ExecStart 等指令控制服务生命周期,结合 After、Requires 实现灵活依赖管理。此外,systemd 集成定时任务和套接字激活,进一步扩展了服务管理的边界。

weixin_70208651的博客 1068
上一篇: 如何让Linux成为一台路由器
下一篇: System V之我见
wunderup
博客等级 码龄20年 13粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值