【转】怎么在rhel7中定义可以在系统启动时运行的脚本?

How to make custom script that runs automatically during boot up in RHEL7

https://access.redhat.com/solutions/1163283

 SOLUTION 已验证 - 已更新 2019年三月14日06:49 - 

English 

环境

  • Red Hat Enterprise Linux 7

问题

  • How to make custom script that runs automatically during boot up in RHEL7?
  • In RHEL 5 and 6, we were using automatic startup feature of RHEL through /etc/rc.d/init.d.

决议

NOTE: Red Hat does not support implementation of custom scripts, including custom systemd startup scripts. This article is provided as a how-to and Red Hat will not troubleshoot any issues after the implementation of the steps provided here. For more information please see the Production Support Scope of Coverage
Related solutions:
How to configure a command, script, or daemon to run after boot has finished in RHEL 7
How to allow a custom service script in RHEL7 to run in the foreground and accept user input
How to configure a systemd service in RHEL7 to run as a custom user or group


  1. Create a new service unit file at /etc/systemd/system/example.service1 with content based on the following

    Raw

    [Unit]
    Description=Example Service Script description goes here
    After=network.target
    
    [Service]
    Type=simple
    ExecStart=/usr/local/sbin/example.sh
    TimeoutStartSec=0
    
    [Install]
    WantedBy=default.target
    

    Optionally, consult footnotes for explanation:
    After=2 Type=3 TimeoutStartSec=4

  2. Create the example shell script mentioned in the unit file's ExecStart directive, e.g., save the following to /usr/local/sbin/example.sh, making sure the file is executable

    Raw

    #!/bin/bash
    z=0
    for i in {1..5}; do 
        sleep 1m
        ((z++))
        wall example.sh: finished minute ${z}
    done
    wall example.sh: COMPLETELY FINISHED
    
  3. Reload the systemd process to consider newly created example.service OR every time when example.service gets modified.

    Raw

    # systemctl daemon-reload
    
  4. Enable the service and then reboot to test

    Raw

    # systemctl enable example.service
    
    # reboot
    

  1. The unit file should be located in either /usr/lib/systemd/system/ or /etc/systemd/system/. See UNIT LOAD PATH section in  man systemd.unit

  2. After=   If the script needs any other system facilities (networking, etc), modify the [Unit] section to include appropriate After=Wants=, or Requires= directives, as described in:  man systemd.unit

  3. Type=   Switch Type=simple for Type=idle in the [Service] section to delay execution of the script until all other jobs are dispatched (see  man systemd.service  for even more choices -- e.g., Type=oneshot can be useful in concert with other services). 

  4. TimeoutStartSec=   When a service doesn't signal start-up completion within TimeoutStartSec, systemd considers the service failed; for long-running shell scripts it is essential to modify TimeoutStartSec or disable the timeout logic altogether as above, with TimeoutStartSec=0. See  man systemd.service  for more details. 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值