我是靠谱客的博主 谨慎服饰,这篇文章主要介绍Linux设置启动项并开机启动-CentOS 7,现在分享给大家,希望可以做个参考。

CentOS 7 设置启动项并开机启动

假设我们要新建一个 mytest 的启动项,并使其开机启动

1. 编辑启动项脚本

进入指定目录并编辑

cd /etc/init.d/ nano mytest

编辑 mytest 内容如下:

注意:你只需要修改下面的部分

while true do uptime >> /root/mytest.txt sleep 2 done

全部内容如下:

#!/bin/bash start() { echo starting while true do uptime >> /root/mytest.txt sleep 2 done } stop() { echo stoping pid=`ps -ef | grep myuptime | grep -v grep | awk '{print $2}'` kill $pid & } case "$1" in start) start ;; stop) stop ;; *) echo "USAGE $0 {start|stop|restart}" exit esac

修改完毕后,需要对这个文件赋权:

chmod +x mytest

2. 注册启动项

进入目录创建

cd /etc/systemd/system nano mytest.service

编辑内容如下(基本不用改):

[Unit] Description=uptime Service After=network.target [Service] Type=simple User=root ExecStart=/etc/init.d/mytest start ExecStop=/etc/init.d/mytest stop [Install] WantedBy=multi-user.target

3. 设置开机启动

systemctl enable mytest # 设置开机启动 systemctl start mytest # 启动服务

4. 其他命令

systemctl stop mytest # 停止服务 systemctl disable mytest # 使其开机不启动

 

最后

以上就是谨慎服饰最近收集整理的关于Linux设置启动项并开机启动-CentOS 7的全部内容,更多相关Linux设置启动项并开机启动-CentOS内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(112)

评论列表共有 0 条评论

立即
投稿
返回
顶部