linux开机自启动命令定义,Linux用户自定义开机启动程序或脚本的常用方法和问题...

编程中,在一些特定的环境和业务中,我们常常需要对某一个脚本实现开机启动或定时启动等一些需求,在

Linux

中实现某个脚本的开机启动,目前我常用的几种方式如下:

方法一:将脚本或命令写入到

/etc/rc.local

文件中

需要执行的shell script直接将它写入/etc/rc.local(或/etc/rc.d/rc.local 根据系统,rc.local文件位置有所不同)文件中,或把需要的shell script 写入一个自己的脚本中,然后在rc.local文件中加入执行改脚本的命令,这样启动就会执行我的shell script了。这种方式比较快捷简便,不需要对该启动脚本做其他操作,所以有一定的局限性

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.

# This is my code

test_path=/root/test.log

if [! -f $test_path] ; then

touch $test_path

fi

cat "Load my code successfully ">>$test_path

方法二:在需要启动的用户的/.bash_profile文件中 加入脚本或命令。在.bash_profile中加载的脚本或命令只能以单用户login时启动。并不是在Linux启动时启动。局限性很大

方法三:通过启动脚本来创建一个服务

需要制作一个shell script放置到/etc/init.d/里面,在/etc/rc*.d中制作相关的link。K开头是kill, S开头是start, 数字顺序代表启动的顺序。

首先编写一个shell script在rc的脚本testInit

#! /bin/sh

### BEGIN INIT INFO

# Provides: testInit

# Required-Start: $remote_fs $network

# Required-Stop: $remote_fs $network

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: starts testInit

# Description:

### END INIT INFO

testPath=/root/test.txt

case "$1" in

start)

echo -n "Starting testInit "

echo "start testInit">>$testPath

;;

stop)

echo -n "Gracefully shutting down testInit "

echo "shutting down testInit">>$testPath

;;

status)

echo "status "

;;

restart)

$0 stop

$0 start

;;

*)

echo "Usage: $0 {start|stop|restart|status}"

exit 1

;;

esac

然后

[root@x ~]# cd /etc/init.d/

[root@x init.d]# ln -s /root/testInit testInit //软链接

[root@x init.d]# chmod +x testInit //可执行权限

下一步,需要根据不同Linux版本采用不同的执行命令

在Debian中

使用 update-rc.d制作链接

[root@x ~]# update-rc.d testInit defaults

就会产生以下链接:

Adding system startup for /etc/init.d/testInit …

/etc/rc0.d/K20testInit -> ../init.d/testInit

/etc/rc1.d/K20testInit -> ../init.d/testInit

/etc/rc6.d/K20testInit -> ../init.d/testInit

/etc/rc2.d/S20testInit -> ../init.d/testInit

/etc/rc3.d/S20testInit -> ../init.d/testInit

/etc/rc4.d/S20testInit-> ../init.d/testInit

/etc/rc5.d/S20testInit -> ../init.d/testInit

在Centos中使用chkconfig来指定启动服务的级别,并在ntsysv工具下加载让其自动运行

[root@x ~]# chkconfig --list

[root@x ~]# chkconfig --add testInit

[root@x ~]# chkconfig --list testInit

[root@x ~]# service testInit start

[root@x ~]# ntsysv

基本知识之update-rc.d

update-rc.d命令,是用来自动的升级System V类型初始化脚本,

简单的讲就是,哪些东西是你想要系统在引导初始化的时候运行的,哪些是希望在关机或重启时停止的,可以用它来帮你设置。

这些脚本的连接位于/etc/rc*.d/LnName,对应脚本位于/etc/init.d/Script-name.

ubuntu与Debian 的update-rc.d与Centos的chkconfig工具相类似。

然而chkconfig是一个二进制程序,而update-rc.d是一个Perl脚本。

这些工具有不同的命令行选项,但是却执行类似的功能。

1.添加服务

[1]

# sudo update-rc.d apache2 defaults

Adding system startup for /etc/init.d/apache2 …

/etc/rc0.d/K20apache2 -> ../init.d/apache2

/etc/rc1.d/K20apache2 -> ../init.d/apache2

/etc/rc6.d/K20apache2 -> ../init.d/apache2

/etc/rc2.d/S20apache2 -> ../init.d/apache2

/etc/rc3.d/S20apache2 -> ../init.d/apache2

/etc/rc4.d/S20apache2 -> ../init.d/apache2

/etc/rc5.d/S20apache2 -> ../init.d/apache2

[2]

# sudo update-rc.d apache2 defaults 91

Adding system startup for /etc/init.d/apache2 …

/etc/rc0.d/K91apache2 -> ../init.d/apache2

/etc/rc1.d/K91apache2 -> ../init.d/apache2

/etc/rc6.d/K91apache2 -> ../init.d/apache2

/etc/rc2.d/S91apache2 -> ../init.d/apache2

/etc/rc3.d/S91apache2 -> ../init.d/apache2

/etc/rc4.d/S91apache2 -> ../init.d/apache2

/etc/rc5.d/S91apache2 -> ../init.d/apache2

[3]

#sudo update-rc.d apache2 defaults 20 80

Adding system startup for /etc/init.d/apache2 …

/etc/rc0.d/K80apache2 -> ../init.d/apache2

/etc/rc1.d/K80apache2 -> ../init.d/apache2

/etc/rc6.d/K80apache2 -> ../init.d/apache2

/etc/rc2.d/S20apache2 -> ../init.d/apache2

/etc/rc3.d/S20apache2 -> ../init.d/apache2

/etc/rc4.d/S20apache2 -> ../init.d/apache2

/etc/rc5.d/S20apache2 -> ../init.d/apache2

[4]

# sudo update-rc.d apache2 start 20 2 3 4 5 . stop 80 0 1 6 .

Adding system startup for /etc/init.d/apache2 …

/etc/rc0.d/K80apache2 -> ../init.d/apache2

/etc/rc1.d/K80apache2 -> ../init.d/apache2

/etc/rc6.d/K80apache2 -> ../init.d/apache2

/etc/rc2.d/S20apache2 -> ../init.d/apache2

/etc/rc3.d/S20apache2 -> ../init.d/apache2

/etc/rc4.d/S20apache2 -> ../init.d/apache2

/etc/rc5.d/S20apache2 -> ../init.d/apache2

[5]

# sudo update-rc.d apache2 start 20 2 3 4 . start 30 5 . stop 80 0 1 6 .

Adding system startup for /etc/init.d/apache2 …

/etc/rc0.d/K80apache2 -> ../init.d/apache2

/etc/rc1.d/K80apache2 -> ../init.d/apache2

/etc/rc6.d/K80apache2 -> ../init.d/apache2

/etc/rc2.d/S20apache2 -> ../init.d/apache2

/etc/rc3.d/S20apache2 -> ../init.d/apache2

/etc/rc4.d/S20apache2 -> ../init.d/apache2

/etc/rc5.d/S30apache2 -> ../init.d/apache2

例如:

update-rc.d script-name start 90 1 2 3 4 5 . stop 52 0 6 .

start 90 1 2 3 4 5 . : 表示在1、2、3、4、5这五个运行级别中,按先后顺序,由小到大,第90个开始运行这个脚本。

stop 52 0 6 . :表示在0、6这两个运行级别中,按照先后顺序,由小到大,第52个停止这个脚本的运行。

2.删除服务

# sudo update-rc.d 服务名 remove

update-rc.d removes any links in the /etc/rcrunlevel.d directories to the script /etc/init.d/name.

The script  must  have  been deleted already – update-rc.d checks for this.

# sudo -f update-rc.d 服务名 remove

-f     Force removal of symlinks even if /etc/init.d/name still exists

# sudo update-rc.d 服务名 stop 80 0 1 2 3 4 5 6 .

This command will only disable the service until next time the service is upgraded.

If you want to make sure the service won’t be re-enabled upon upgrade, you should also type the above.

例如:

update-rc.d -f foobar remove

update-rc.d foobar stop 20 2 3 4 5 .

3.临时重启服务:

/etc/init.d/服务名 restart

4.临时关闭服务:

/etc/init.d/服务名 stop

5.临时启动服务:

/etc/init.d/服务名 start

FILES

/etc/init.d/

The directory containing the actual init scripts.

/etc/rc*.d/

The directories containing the links used by init and managed by

update-rc.d.

/etc/init.d/skeleton

Model for use by writers of init.d scripts.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值