Linux启动项管理
文章目录
前言
当我们为Linux搭建的服务器添加完应用之后,我们一般都是希望服务器重启时自动打开这个应用,因此我们就需要提前对启动项进行配置。
一、启动项是什么?
启动项是在系统启动时就自动执行的程序。
应用通过yum完成安装之后,会自动添加到启动项中,但是有些应用没有通过yum或者rpm,就不一定会添加到system的启动项中,此时需要使用其它方式进行操作,将文件添加到rc.local中
二、在启动项中的应用的管理
1.启动项查询
基本命令:ystemctl list-unit-files
可配合管道命令进行过滤----如下所示,查看启动项中状态为enable的项
[root@localhost test]# systemctl list-unit-files | grep enable
cups.path enabled
abrt-ccpp.service enabled
abrt-oops.service enabled
abrt-vmcore.service enabled
abrt-xorg.service enabled
abrtd.service enabled
accounts-daemon.service enabled
atd.service enabled
2.关闭服务
systemctl disable 要关闭的项目名
如下:
[root@localhost share]# systemctl disable postfix.service
3.查询服务状态
systemctl status postfix.service 要查询的项目名
如下:
[root@localhost share]# systemctl status postfix.service
● postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-06-21 15:54:31 CST; 36min ago #表示运行状态
Main PID: 1230 (master)
CGroup: /system.slice/postfix.service
├─1230 /usr/libexec/postfix/master -w
├─1245 pickup -l -t unix -u
└─1246 qmgr -l -t unix -u
三、不启动项中的应用的管理
没有添加到system的启动项中的应用,此时需要使用如下方式进行操作,将文件添加到rc.local中
这里我们通过创建一个脚本文件-它能记录我们每一次登录的系统时间,来说明不启动项中的应用的配置过程
1.配置流程
1,创建脚本文件,并在文件中写入获取时间,和将时间记录到另一个文件的命令。
2,将改脚本文件的路径写入到rc.local中。
2.rc.local基础
rc.local文件是系统应用启动完后,就会立刻执行其中命令的一个文件
使用命令:ll /etc/rc*
[root@localhost etc]# ll /etc/rc*
lrwxrwxrwx. 1 root root 10 Jun 19 10:43 /etc/rc0.d -> rc.d/rc0.d
lrwxrwxrwx. 1 root root 10 Jun 19 10:43 /etc/rc1.d -> rc.d/rc1.d
lrwxrwxrwx. 1 root root 10 Jun 19 10:43 /etc/rc2.d -> rc.d/rc2.d
lrwxrwxrwx. 1 root root 10 Jun 19 10:43 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx. 1 root root 10 Jun 19 10:43 /etc/rc4.d -> rc.d/rc4.d
lrwxrwxrwx. 1 root root 10 Jun 19 10:43 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx. 1 root root 10 Jun 19 10:43 /etc/rc6.d -> rc.d/rc6.d
lrwxrwxrwx. 1 root root 13 Jun 19 10:44 /etc/rc.local -> rc.d/rc.local enabled
和命令:ll /etc/rc.d/
[root@localhost rc.d]# ll /etc/rc.d/
total 4
drwxr-xr-x. 2 root root 70 Jun 20 08:17 init.d
drwxr-xr-x. 2 root root 45 Jun 20 08:17 rc0.d
drwxr-xr-x. 2 root root 45 Jun 20 08:17 rc1.d
drwxr-xr-x. 2 root root 45 Jun 20 08:17 rc2.d
drwxr-xr-x. 2 root root 45 Jun 20 08:17 rc3.d
drwxr-xr-x. 2 root root 45 Jun 20 08:17 rc4.d
drwxr-xr-x. 2 root root 45 Jun 20 08:17 rc5.d
drwxr-xr-x. 2 root root 45 Jun 20 08:17 rc6.d
-rw-r--r--. 1 root root 473 Nov 7 2016 rc.local
可以看到etc目录下有一个rc.loacl文件,etc/rc.d下也有一个rc.local文件但前者只是后者的一个软链接—相当于windows的一个快捷方式而已,所以实际上是要修改他的本体即—etc/rc.d下rc.local。实际操作中不要弄错
并且我们注意看:
软链接的权限
lrwxrwxrwx. 1 root root 13 Jun 19 10:44 /etc/rc.local -> rc.d/rc.local
本体的权限
-rw-r--r--. 1 root root 473 Nov 7 2016 rc.local
可以看到etc/rc.d下rc.local对所有用户都是没有执行权限的(只是软链接可执行),所以我们要开启它的权限
[root@localhost etc]# chmod 755 /etc/rc.d/rc.local
3.创建脚本文件
首先我们我们要知道:date是获取当前时间
[root@localhost rc.d]# date
Sun Jun 25 15:24:04 CST 2023
只是在linux没启动时,脚本文件中date命令是找不到的,因此我们要找到它的完整路径
[root@localhost etc]# whereis date
date: /usr/bin/date /usr/share/man/man1/date.1.gz /usr/share/man/man1p/date.1p.gz
创建脚本文件,写入命令并开启执行权限
cd /test
touch run.bat
echo >> /usr/bin/date >> /test/log
chmod 755 /test/run.bat
4.写入rc.local
root@localhost test]# more /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/test/run.bat
重新启动服务器进行测试
[root@localhost test]# more log
Wed Jun 21 16:42:28 CST 2023
总结
例如:以上就是今天要讲的内容,本文仅仅简单介绍了Linux启动项管理。至于不启动项中的应用的管理也可以不用rc.local(个人对它比较熟悉),而使用centos7中推荐的systemd的方式来管理,下回继续分解。