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 # 使其开机不启动

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值