如何在Linux上获取基本软件包安装位置?

时间:2014-11-24 07:00:42

标签: service centos package rpm

我在Linux Centos OS上。我明白使用" rpm -qa"为相应的包提供了大量的安装路径。但是,我只需要包的基本包安装位置。在Linux中是否有任何方法/命令/选项来检索相同的?我的代码片段是检索正在运行的服务列表,安装的相应软件包如下: -

for $ in $(service --status-all | grep -v" not running" | grep -E running \ | stopped | awk' {print $ 1}') ; 做 packagename = $(rpm -qf /etc/init.d/$i) servicestatus = $(service --status-all | grep $ i | awk' {print $ NF}' | sed' s /...// g' | sed&# 39; S /.//克&#39);

echo $ tdydate,$(ip route get 8.8.8.8 | awk' NR == 1 {print $ NF}'),$ i,$ packagename,$ servicestatus> " $ HOME / MyLog / running_services.csv" 完成

现在,我还需要获得相应的软件包安装位置,该位置托管正在运行的服务。有没有办法检索这个以及获取包名称。请确认。

提前感谢您提供帮助。

问候。

2 个答案:

答案 0 :(得分:0)

好的,在评论中回答我的问题,这比我最初的问题更清楚......

  

嗨,基本上我需要的是: - 我使用service -status-all获取Centos上所有已安装服务的列表。现在,对于每个服务,我需要知道linux上相应的应用程序包位置。

......我会提出这个问题(在CentOS 6.6上进行测试):

#!/bin/bash for i in `chkconfig --list | awk '{ print $1}'`; do service $i status >/dev/null 2>&1 if [ $?==0 ]; then rpm -qf /etc/init.d/$i fi done | sort | uniq

吐出当前正在运行的服务的所有rpm名称。

关于为什么你当前的方法不起作用的更多细节: service --status-all不会返回可以可靠解析的信息。例如,VM上的输出:

acpid (pid 872) is running... auditd (pid 789) is running... Stopped cgred is stopped Checking for service cloud-init:Checking for service cloud-init:Checking for service cloud-init:Checking for service cloud-init:crond (pid 1088) is running... ip6tables: Firewall is not running. iptables: Firewall is not running. Kdump is not operational mdmonitor is stopped netconsole module not loaded Configured devices: lo eth0 Currently active devices: lo eth0 ntpd (pid 997) is running... master (pid 1076) is running... rdisc is stopped restorecond is stopped rsyslogd (pid 809) is running... sandbox is stopped saslauthd is stopped openssh-daemon (pid 988) is running...

有些服务甚至没有返回他们的名字(第三行)。有人说stopped,其他人not running。如果您解析chkconfig --list的第一列,则您知道所有服务名称,这些名称与/etc/init.d中的文件相对应。然后,您可以单独查询其状态并阅读返回代码$?),其中0表示运行服务(或通常在Unix / Linux世界中成功),1或更高版本未运行或未安装或未完成/故障的服务。

使用/etc/init.d/中的姓名,您可以使用rpm -qf /etc/init.d/<servicename>查询拥有的包,并获得我认为您正在寻找的内容。

编辑:在循环之后添加| sort | uniq,因为一些包包含多个服务,例如cloud-init,它在CentOS上创建了四个不同的服务。因此,您对列表进行排序,然后确保只返回不同的(uniq)名称。 适合我:

acpid-1.0.10-2.1.el6.x86_64 audit-2.3.7-5.el6.x86_64 cloud-init-0.7.5-10.el6.centos.2.x86_64 cronie-1.4.4-12.el6.x86_64 cyrus-sasl-2.1.23-15.el6_6.1.x86_64 initscripts-9.03.46-1.el6.centos.1.x86_64 iptables-1.4.7-14.el6.x86_64 iptables-ipv6-1.4.7-14.el6.x86_64 iputils-20071127-17.el6_4.2.x86_64 kexec-tools-2.0.0-280.el6.x86_64 libcgroup-0.40.rc1-15.el6_6.x86_64 mdadm-3.3-6.el6.x86_64 ntp-4.2.6p5-1.el6.centos.x86_64 ntpdate-4.2.6p5-1.el6.centos.x86_64 openssh-server-5.3p1-104.el6_6.1.x86_64 policycoreutils-2.0.83-19.47.el6_6.1.x86_64 postfix-2.6.6-6.el6_5.x86_64 rsyslog-5.8.10-9.el6_6.x86_64 udev-147-2.57.el6.x86_64

答案 1 :(得分:0)

您正在寻找--whatprovides而不是-qf(进行格式化)。

调整你的例子......

for i in $(chkconfig --list | awk '{ print $1}'); do service $i status >/dev/null 2>&1; if [ 0==$? ]; then echo -n "$i: "; rpm -q --whatprovides /etc/init.d/$i; fi; done | sort

仅供参考 - 这不适用于更现代systemd的系统(CentOS 7)。

我的Fedora 21盒子上的示例:

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole: initscripts-9.56.1-5.fc21.x86_64
network: initscripts-9.56.1-5.fc21.x86_64
相关问题