linux添加开机自启脚本

自定义服务,通过Systemctl进行管理

  1. 写好执行脚本,并保存到指定目录:
    #!/bin/sh
    ... 脚本内容
    
  2. 写好服务文件,文件名以 .service结尾,保存到/usr/lib/systemd/system/目录下面:
    简单点:
    [Unit]
    Description = test scripy  # 描述
    
    [Service]
    Type=forking  # 后台运行
    ExecStart=/root/test.sh  # 该脚本的启动命令
    PrivateTmp=true   # 独立分配临时空间
    
    [Install]
    WantedBy=multi-user.target   # 多用户登录服务
    Alias=pppdrun.setvice   # 服务别名
    
    参考nginx.service的服务文件
    [Unit]
    Description=nginx - high performance web server
    After=network.target remote-fs.target nss-lookup.target
     
    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
     
    [Install]
    WantedBy=multi-user.target
    
  3. 设置未开机自启动:
    systemctl enable your_service_name.service
    

其他命令,以nginx为例:

启动nginx服务
[root@localhost ~]# systemctl start nginx.service
停止nginx服务
[root@localhost ~]# systemctl start nginx.service
重启nginx服务
[root@localhost ~]# systemctl restart nginx.service
查看nginx服务当前状态
[root@localhost ~]# systemctl status nginx.service
查看所有已启动的服务
[root@localhost ~]# systemctl list-units --type=service
停止开机自启动
[root@localhost ~]# systemctl disable nginx.service

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值