CentOS 7

CentOS7下的各种应用


CentOS 7 关机和重启前执行自定义脚本

<h2>前言</h2> <p>由于挂载ISCSI存储服务器之后,在关机非常缓慢,最后电源一直没关闭成功。所以,在关机手工执行umout iscsi device,注销iscsi再关机就可以正常关机。下面介绍在关机前执行自定义(关闭相应服务)脚本。 如果你在linux系统上启动了很多自己的服务,如果直接执行系统的reboot或者shutdown命令,可能会发生一些异常的现象,所以我们在重启和关机前,应该按照我们的意愿去自定义清理一些服务或者资源等。但是网上的方式大多是以Centos6为教程,修改chkconfig和rc0.d,rc6.d的内容。在Centos7中以及不适用了。 本文就介绍一下在Centos7系统中,使用systemctl的方式来自定义关机前执行脚本。</p> <h3>1.创建systemd服务</h3> <pre><code class="language-bash">vi /usr/lib/systemd/system/stopSvc.service</code></pre> <p>创建该文件然后加入下面的内容,需要把<code>ExecStart=/opt/umntNASNFS.sh</code>这一行换成自己的脚本路径</p> <pre><code class="language-bash">[Unit] Description=close services before reboot and shutdown DefaultDependencies=no #Before=shutdown.target reboot.target halt.target Before=network.target iscsi.service iscsid.service shutdown.target reboot.target halt.target # This works because it is installed in the target and will be executed before the # target state is entered # Also consider kexec.target [Service] Type=oneshot ExecStart=/opt/.umntNASNFS.sh [Install] WantedBy=network.target iscsi.service iscsid.service</code></pre> <h3>2.设置随机启动服务</h3> <pre><code class="language-bash">systemctl enable stopSvc</code></pre> <p>经过这两步操作,关机和重启前,就会自动执行你自定义的脚本了</p> <h3>3.报错问题解决</h3> <p><img src="https://www.showdoc.cc/server/api/common/visitfile/sign/8606c3f5959d1d3c27e1819847b0ce54?showdoc=.jpg" alt="" /> 报错原因: CentOS 7系统在挂载iSCSI卷时, (1)iSCSI会话在启动时不会自动登录。#CentOS7早期版本BUG。现在CentOS7.7版本需要等待约2分钟左右。 (2)在关闭时“挂起”,即在卸载并先退出iscsi会话时挂起; 解决: (1)iSCSI节点定义文件在/var/lib/iscsi/nodes/iqn.../IP,3260,1/default 中, 将 “node.startup = onboot” 更改为 “node.startup = automatic”。 最初,我是通过手动运行“ iscsiadm -m Discovery -t st -p <target_ip_address>”创建节点定义的,然后也手动启动了iscsid和iscsi服务。存在两个节点文件,因为通过多路径可以到达目标。 (2)另外,有服务依赖于iSCSI卷作为其存档存储。通过将以下行添加到此服务的Systemd单元文件中,就能够使系统启动和关闭顺利进行: After=network.target iscsi.service iscsid.service Wants=network.target iscsi.service iscsid.service 这些行确保proserver服务在系统启动时在iscsi和iscsid之后启动,并在系统关闭之前在它们之前停止。</p> <p><a href="https://groups.google.com/forum/#!topic/open-iscsi/8U9mAXutlyE">https://groups.google.com/forum/#!topic/open-iscsi/8U9mAXutlyE</a> 需科学上网才能访问。</p>

页面列表

ITEM_HTML