How to start a program automatic

Reference URL: http://unix.stackexchange.com/questions/20357/how-can-i-make-a-script-in-etc-init-d-start-at-boot

試過 It's work.

注意Script 内容格式,按下面写,如果发现not work 就检查script是否有错。名字可不加.sh, 如:tomcat都ok

 

 If you are on a Red Hat based system, as you mentioned, you can do the following:

  1. Create a script and place in /etc/init.d (e.g /etc/init.d/myscript). The script should have the following format:
#!/bin/bash# chkconfig: 2345 20 80# description: Description comes here....# Source function library../etc/init.d/functions

start(){# code to start app comes here # example: daemon program_name &}

stop(){# code to stop app comes here # example: killproc program_name}case"$1"in 
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)# code to check status of app comes here # example: status program_name;;*)
       echo "Usage: $0 {start|stop|status|restart}"esac

exit 0

The format is pretty standard and you can view existing scripts in /etc/init.d. You can then use the script like so /etc/init.d/myscript start or chkconfig myscript start. The ckconfig man page explains the header of the script:

 > This says that the script should be started in levels 2,  3,  4, and
 > 5, that its start priority should be 20, and that its stop priority
 > should be 80.

The example start, stop and status code uses helper functions defined in /etc/init.d/functions

  1. Enable the script

    $ chkconfig --add myscript 
    $ chkconfig --level 2345 myscript on 
    
  2. Check the script is indeed enabled - you should see "on" for the levels you selected.

    $ chkconfig --list | grep myscript
    

Hope this is what you were looking for.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值