Linux 服务/脚本/命令自启动配置

Linux服务自启动

 若程序(脚本)已注册成服务,则可通过service serviceName start/stop/status来启动服务、停止服务和查看服务状态,也可通过/etc/init.d/serviceName start/stop来开启和停止服务。
 可通过/etc/rc.d/init.d或/etc/init.d查看linux全部服务。

方法一:修改配置文件

 修改 /etc/rc.d/rc.local或/etc/rc.local文件(该文件在系统启动时,在输入用户名和密码之前最后读取的文件),加入服务启动命令来实现服务自启动。

#!/bin/sh
touch /var/lock/subsys/local
# 系统启动时,tomcat6服务自启动
/etc/rc.d/init.d/tomcat6 start

方法二:chkconfig命令

 chkconfig命令格式:chkconfig serviceName on/off

#自启动tomcat服务
chkconfig tomcat on
#关闭tomcat自启动
chkconfig tomcat off

方法三:通过ntsysv调用窗口模式来管理服务的自启动

 ntsysv命令格式:ntsysv [–level 运行级别]
          --level 运行级别:可以指定设定自启动的运行级别;

#只设定2、3、5级别的服务自启动
[root@localhost ~]# ntsysv --level 235
#按默认的运行级别设置服务自启动
[root@localhost ~]# ntsysv

执行上述命令之后linux界面如下,使用空格键来选定/取消服务的自启动,其中*号代表该服务自启动。

注意:ntsysv属于redhat系列linux的专有命令

Linux脚本自启动

方法一:把脚本放到/etc/rc.local

脚本文件start_tomcat.sh

#!/bin/bash
#启动tomcat
/etc/init.d/tomcat6 start

把需自启动的脚本文件放入/etc/rc.local

#!/bin/bash
touch /var/lock/subsys/local
#当系统启动时便执行start_tomcat.sh脚本
/root/start_tomcat.sh #脚本执行方式:绝对路径执行方式,path/脚本文件

方法二:把sh脚本移动到/etc/profile.d

 /etc/profile.d中的脚本文件执行情景:当一个用户登录Linux系统或使用切换到另一个用户时才会调用,所以不建议使用

Linux自启动命令

 若要自启动命令,则把需要自启动的命令放在/etc/rc.local中。

Author: HB
Link: http://www.huangbin.fun/Linux-服务-脚本-命令自启动配置.html
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.