-
2021-05-14 18:35:34
Linux配置开机自启动执行脚本的方法有很多,这里分享两种方法,分别是修改/etc/rc.local方法和chkconfig管理设置的方法,均可实现Linux配置开机自启动执行脚本的功能!
**设置test.sh为开机要启动的脚本**
[root@oldboy scripts]# vim /server/scripts/test.sh
[root@oldboy scripts]# cat /server/scripts/ test.sh
#!/bin/bash
/bin/echo $(/bin/date +%F_%T) >> /tmp/ test.log
**方法一:修改/etc/rc.local**
[root@oldboy ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Mar 30 10:50 /etc/rc.local -> rc.d/rc.local
**修改/etc/rc.local文件**
[root@oldboy scripts]# tail -n 1 /etc/rc.local
/bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null
**重启系统,查看结果**
[root@oldboy ~]# cat /tmp/test.log
2018-03-30_12:00:10
**方法二:chkconfig管理**
**删除掉方法一的配置**
[root@oldboy ~]# vim /etc/init.d/test
#!/bin/bash
# chkconfig: 3 88 88
/bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null
[root@oldboy ~]# chmod +x /etc/init.d/test
**添加到chkconfig,开机自启动**
[root@oldboy ~]# chkconfig --add test
[root@oldboy ~]# chkconfig --list test
test 0:off1:off2:off3:on4:off5:off6:off
**重启系统,查看结果**
[root@oldboy ~]# cat /tmp/test.log
2018-03-30_12:00:10
2018-03-30_12:33:20
**操作成功**
**关闭开机启动**
[root@oldboy ~]# chkconfig test off
[root@oldboy ~]# chkconfig --list test
test 0:off1:off2:off3:off4:off5:off6:off
**从chkconfig管理中删除test**
[root@oldboy ~]# chkconfig --list test
test 0:off1:off2:off3:off4:off5:off6:off
[root@oldboy ~]# chkconfig --del test
[root@oldboy ~]# chkconfig --list test
service test supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add test')
有疑问加站长微信联系(非本文作者)
更多相关内容 -
Linux开机自启动目录
2017-01-24 10:40:28开机启动脚本是存放在 /etc/rc.d/rcX.d 目录中的,这里的X是runlevel,就是linux的运行级别,可以看到有rc0~rc6,共7种级别: 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动 运行级别1:...1、开机启动脚本
开机启动脚本是存放在 /etc/rc.d/rcX.d 目录中的,这里的X是runlevel,就是linux的运行级别,可以看到有rc0~rc6,共7种级别:
运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
运行级别2:多用户状态(没有NFS)
运行级别3:完全的多用户状态(有NFS),登陆后进入控制台命令行模式
运行级别4:系统未使用,保留
运行级别5:X11控制台,登陆后进入图形GUI模式
运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动
[root@wc1 Desktop]# pwd /root/Desktop [root@wc1 Desktop]# cd /etc/rc.d [root@wc1 rc.d]# ls -l total 60 drwxr-xr-x. 2 root root 4096 Aug 16 14:58 init.d -rwxr-xr-x. 1 root root 2617 Oct 16 2014 rc drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc0.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc1.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc2.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc3.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc4.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc5.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc6.d -rwxr-xr-x. 1 root root 220 Oct 16 2014 rc.local -rwxr-xr-x. 1 root root 19914 Oct 16 2014 rc.sysinit
这里切换到rc3.d目录下,也就是完全的多用户模式:
[root@wc1 rc.d]# cd rc3.d [root@wc1 rc3.d]# ls -l total 0 lrwxrwxrwx. 1 root root 16 Aug 4 21:08 K01smartd -> ../init.d/smartd lrwxrwxrwx. 1 root root 17 Aug 4 21:06 K02oddjobd -> ../init.d/oddjobd lrwxrwxrwx. 1 root root 17 Aug 4 21:09 K05wdaemon -> ../init.d/wdaemon lrwxrwxrwx. 1 root root 16 Aug 4 21:08 K10psacct -> ../init.d/psacct lrwxrwxrwx. 1 root root 19 Aug 4 21:06 K10saslauthd -> ../init.d/saslauthd lrwxrwxrwx. 1 root root 22 Aug 4 21:06 K15htcacheclean -> ../init.d/htcacheclean lrwxrwxrwx. 1 root root 15 Aug 4 21:06 K15httpd -> ../init.d/httpd lrwxrwxrwx. 1 root root 24 Aug 4 21:08 K30spice-vdagentd -> ../init.d/spice-vdagentd lrwxrwxrwx. 1 root root 17 Aug 4 21:06 K50dnsmasq -> ../init.d/dnsmasq lrwxrwxrwx. 1 root root 20 Aug 4 21:05 K50netconsole -> ../init.d/netconsole lrwxrwxrwx. 1 root root 15 Aug 4 21:06 K50snmpd -> ../init.d/snmpd lrwxrwxrwx. 1 root root 19 Aug 4 21:06 K50snmptrapd -> ../init.d/snmptrapd lrwxrwxrwx. 1 root root 13 Aug 4 21:05 K60nfs -> ../init.d/nfs lrwxrwxrwx. 1 root root 20 Aug 4 21:05 K69rpcsvcgssd -> ../init.d/rpcsvcgssd lrwxrwxrwx. 1 root root 17 Aug 4 21:05 K73winbind -> ../init.d/winbind lrwxrwxrwx. 1 root root 14 Aug 4 13:47 K74ntpd -> ../init.d/ntpd lrwxrwxrwx. 1 root root 17 Aug 4 21:06 K75ntpdate -> ../init.d/ntpdate lrwxrwxrwx. 1 root root 19 Aug 4 21:08 K75quota_nld -> ../init.d/quota_nld lrwxrwxrwx. 1 root root 16 Aug 4 21:06 K76ypbind -> ../init.d/ypbind lrwxrwxrwx. 1 root root 15 Aug 4 13:47 K80kdump -> ../init.d/kdump lrwxrwxrwx. 1 root root 24 Aug 16 15:35 K84wpa_supplicant -> ../init.d/wpa_supplicant lrwxrwxrwx. 1 root root 21 Aug 4 21:05 K87restorecond -> ../init.d/restorecond lrwxrwxrwx. 1 root root 14 Aug 16 15:35 K88sssd -> ../init.d/sssd lrwxrwxrwx. 1 root root 15 Aug 4 21:05 K89rdisc -> ../init.d/rdisc lrwxrwxrwx. 1 root root 19 Aug 4 13:47 K95firstboot -> ../init.d/firstboot lrwxrwxrwx. 1 root root 14 Aug 4 21:08 K99rngd -> ../init.d/rngd lrwxrwxrwx. 1 root root 17 Aug 4 21:07 S01sysstat -> ../init.d/sysstat lrwxrwxrwx. 1 root root 22 Aug 4 21:07 S02lvm2-monitor -> ../init.d/lvm2-monitor lrwxrwxrwx. 1 root root 22 Aug 16 14:59 S03vmware-tools -> ../init.d/vmware-tools lrwxrwxrwx. 1 root root 19 Aug 4 21:06 S08ip6tables -> ../init.d/ip6tables lrwxrwxrwx. 1 root root 18 Aug 4 21:05 S08iptables -> ../init.d/iptables lrwxrwxrwx. 1 root root 17 Aug 4 21:05 S10network -> ../init.d/network lrwxrwxrwx. 1 root root 16 Aug 4 21:08 S11auditd -> ../init.d/auditd lrwxrwxrwx. 1 root root 21 Aug 4 21:02 S11portreserve -> ../init.d/portreserve lrwxrwxrwx. 1 root root 17 Aug 4 21:06 S12rsyslog -> ../init.d/rsyslog lrwxrwxrwx. 1 root root 18 Aug 4 21:08 S13cpuspeed -> ../init.d/cpuspeed lrwxrwxrwx. 1 root root 20 Aug 4 21:06 S13irqbalance -> ../init.d/irqbalance lrwxrwxrwx. 1 root root 17 Aug 4 21:03 S13rpcbind -> ../init.d/rpcbind lrwxrwxrwx. 1 root root 19 Aug 4 21:05 S15mdmonitor -> ../init.d/mdmonitor lrwxrwxrwx. 1 root root 20 Aug 4 21:02 S22messagebus -> ../init.d/messagebus lrwxrwxrwx. 1 root root 24 Aug 4 21:06 S23NetworkManager -> ../init.d/NetworkManager lrwxrwxrwx. 1 root root 17 Aug 16 15:35 S24nfslock -> ../init.d/nfslock lrwxrwxrwx. 1 root root 17 Aug 16 15:35 S24rpcgssd -> ../init.d/rpcgssd lrwxrwxrwx. 1 root root 26 Aug 4 21:07 S25blk-availability -> ../init.d/blk-availability lrwxrwxrwx. 1 root root 14 Aug 4 21:05 S25cups -> ../init.d/cups lrwxrwxrwx. 1 root root 15 Aug 4 21:05 S25netfs -> ../init.d/netfs lrwxrwxrwx. 1 root root 15 Aug 4 21:08 S26acpid -> ../init.d/acpid lrwxrwxrwx. 1 root root 19 Aug 4 21:05 S26haldaemon -> ../init.d/haldaemon lrwxrwxrwx. 1 root root 19 Aug 4 21:05 S26udev-post -> ../init.d/udev-post lrwxrwxrwx. 1 root root 16 Aug 4 21:06 S28autofs -> ../init.d/autofs lrwxrwxrwx. 1 root root 19 Aug 4 21:06 S50bluetooth -> ../init.d/bluetooth lrwxrwxrwx. 1 root root 14 Aug 4 21:07 S55sshd -> ../init.d/sshd lrwxrwxrwx. 1 root root 32 Aug 16 14:59 S57vmware-tools-thinprint -> ../init.d/vmware-tools-thinprint lrwxrwxrwx. 1 root root 15 Aug 16 15:35 S64mysql -> ../init.d/mysql lrwxrwxrwx. 1 root root 17 Aug 4 21:06 S80postfix -> ../init.d/postfix lrwxrwxrwx. 1 root root 19 Aug 4 21:03 S82abrt-ccpp -> ../init.d/abrt-ccpp lrwxrwxrwx. 1 root root 15 Aug 4 21:03 S82abrtd -> ../init.d/abrtd lrwxrwxrwx. 1 root root 15 Aug 4 21:06 S90crond -> ../init.d/crond lrwxrwxrwx. 1 root root 13 Aug 4 21:03 S95atd -> ../init.d/atd lrwxrwxrwx. 1 root root 20 Aug 4 21:06 S99certmonger -> ../init.d/certmonger lrwxrwxrwx. 1 root root 11 Aug 4 21:05 S99local -> ../rc.local
发现有很多K、S开头的后面跟着2位数字的文件,比如:K15httpd,K表示关闭,而15是指关闭的 优先级,也就是linux内核会按照系统预先设置好的runlevel,比如设置为3,那么就读取/etc/rc.d/rc3.d这个目录中的文件,然后按照先执行K开头,后执行S开头,并且从数字较小的开始执行,比如这里K15httpd,也就是排在第7个开始执行。
而且可以看到 K15httpd -> ../init.d/httpd ,也就是说 K15httpd是一个连接文件,连接到了相对于当前目录的上层目录下面的init.d/httpd,也就是目录/etc/rc.d/init.d :
[root@wc1 rc3.d]# ls -l /etc/rc.d/init.d total 408 -rwxr-xr-x. 1 root root 1288 Oct 16 2014 abrt-ccpp -rwxr-xr-x. 1 root root 1628 Oct 16 2014 abrtd -rwxr-xr-x. 1 root root 1642 Oct 16 2014 abrt-oops -rwxr-xr-x. 1 root root 1725 Aug 19 2010 acpid -rwxr-xr-x. 1 root root 2062 Jan 30 2012 atd -rwxr-xr-x. 1 root root 3580 Oct 15 2014 auditd -rwxr-xr-x. 1 root root 4043 Oct 16 2014 autofs -r-xr-xr-x. 1 root root 1340 Oct 16 2014 blk-availability -rwxr-xr-x. 1 root root 710 Nov 11 2010 bluetooth -rwxr-xr-x. 1 root root 2094 Oct 16 2014 certmonger -rwxr-xr-x. 1 root root 11355 Aug 13 2013 cpuspeed -rwxr-xr-x. 1 root root 2826 Nov 23 2013 crond -rwxr-xr-x. 1 root root 3034 Oct 15 2014 cups -rwxr-xr-x. 1 root root 1734 Jun 16 2014 dnsmasq -rwxr-xr-x. 1 root root 3245 Jul 9 2013 firstboot -rw-r--r--. 1 root root 19295 Jul 22 2014 functions -rwxr-xr-x. 1 root root 1801 Oct 15 2014 haldaemon -rwxr-xr-x. 1 root root 5866 Jul 22 2014 halt -rwxr-xr-x. 1 root root 2001 Oct 16 2014 htcacheclean -rwxr-xr-x. 1 root root 3371 Oct 16 2014 httpd -rwxr-xr-x. 1 root root 10804 Oct 15 2014 ip6tables -rwxr-xr-x. 1 root root 10688 Oct 15 2014 iptables -rwxr-xr-x. 1 root root 1938 Jun 2 2014 irqbalance -rwxr-xr-x. 1 root root 19476 Oct 16 2014 kdump -rwxr-xr-x. 1 root root 652 Jul 22 2014 killall -r-xr-xr-x. 1 root root 2134 Oct 16 2014 lvm2-lvmetad -r-xr-xr-x. 1 root root 2757 Oct 16 2014 lvm2-monitor -rwxr-xr-x. 1 root root 2571 Sep 5 2014 mdmonitor -rwxr-xr-x. 1 root root 2200 Sep 14 2012 messagebus -rwxr-xr-x. 1 root root 10815 Jul 3 2013 mysql -rwxr-xr-x. 1 root root 2989 Jul 22 2014 netconsole -rwxr-xr-x. 1 root root 6064 Jul 22 2014 netfs -rwxr-xr-x. 1 root root 6334 Jul 22 2014 network -rwxr-xr-x. 1 root root 2205 Oct 15 2014 NetworkManager -rwxr-xr-x. 1 root root 6878 Oct 16 2014 nfs -rwxr-xr-x. 1 root root 3526 Oct 16 2014 nfslock -rwxr-xr-x. 1 root root 1923 Jul 15 2013 ntpd -rwxr-xr-x. 1 root root 2043 Jul 15 2013 ntpdate -rwxr-xr-x. 1 root root 2261 Jun 25 2011 oddjobd -rwxr-xr-x. 1 root root 2023 Apr 3 2012 portreserve -rwxr-xr-x. 1 root root 3912 Feb 20 2014 postfix -rwxr-xr-x. 1 root root 1556 Jul 17 2012 psacct -rwxr-xr-x. 1 root root 2034 Mar 5 2014 quota_nld -rwxr-xr-x. 1 root root 1513 Sep 17 2013 rdisc -rwxr-xr-x. 1 root root 1822 Oct 16 2014 restorecond -rwxr-xr-x. 1 root root 1808 Dec 18 2011 rngd -rwxr-xr-x. 1 root root 2073 Feb 22 2013 rpcbind -rwxr-xr-x. 1 root root 2518 Oct 16 2014 rpcgssd -rwxr-xr-x. 1 root root 2305 Oct 16 2014 rpcidmapd -rwxr-xr-x. 1 root root 2464 Oct 16 2014 rpcsvcgssd -rwxr-xr-x. 1 root root 2011 Aug 15 2013 rsyslog -rwxr-xr-x. 1 root root 1698 Oct 16 2014 sandbox -rwxr-xr-x. 1 root root 2056 Jun 23 2014 saslauthd -rwxr-xr-x. 1 root root 647 Jul 22 2014 single -rwxr-xr-x. 1 root root 3002 Feb 22 2013 smartd -rwxr-xr-x. 1 root root 2162 Sep 15 2014 snmpd -rwxr-xr-x. 1 root root 1738 Sep 15 2014 snmptrapd -rwxr-xr-x. 1 root root 2472 Oct 15 2014 spice-vdagentd -rwxr-xr-x. 1 root root 4621 Oct 15 2014 sshd -rwxr-xr-x. 1 root root 2690 Oct 16 2014 sssd -rwxr-xr-x. 1 root root 1144 Oct 16 2014 sysstat -rwxr-xr-x. 1 root root 2294 Oct 16 2014 udev-post -rwxr-xr-x. 1 root root 38525 Aug 16 14:58 vmware-tools -rwxr-xr-x. 1 root root 15208 Aug 16 14:58 vmware-tools-thinprint -rwxr-xr-x. 1 root root 1674 Feb 22 2013 wdaemon -rwxr-xr-x. 1 root root 1608 Oct 15 2014 winbind -rwxr-xr-x. 1 root root 1866 Feb 4 2013 wpa_supplicant -rwxr-xr-x. 1 root root 4799 Feb 22 2013 ypbind [root@wc1 rc3.d]#
再来看一下/etc/rc.d/init.d/httpd文件的内容:
[root@wc1 rc3.d]# cat /etc/rc.d/init.d/httpd #!/bin/bash # # httpd Startup script for the Apache HTTP Server # # chkconfig: - 85 15 # description: The Apache HTTP Server is an efficient and extensible \ # server implementing the current HTTP standards. # processname: httpd # config: /etc/httpd/conf/httpd.conf # config: /etc/sysconfig/httpd # pidfile: /var/run/httpd/httpd.pid # ### BEGIN INIT INFO # Provides: httpd # Required-Start: $local_fs $remote_fs $network $named # Required-Stop: $local_fs $remote_fs $network # Should-Start: distcache # Short-Description: start and stop Apache HTTP Server # Description: The Apache HTTP Server is an extensible server # implementing the current HTTP standards. ### END INIT INFO # Source function library. . /etc/rc.d/init.d/functions if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi # Start httpd in the C locale by default. HTTPD_LANG=${HTTPD_LANG-"C"} # This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS="" # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server # with the thread-based "worker" MPM; BE WARNED that some modules may not # work correctly with a thread-based MPM; notably PHP will refuse to start. # Path to the apachectl script, server binary, and short-form for messages. apachectl=/usr/sbin/apachectl httpd=${HTTPD-/usr/sbin/httpd} prog=httpd pidfile=${PIDFILE-/var/run/httpd/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd} RETVAL=0 STOP_TIMEOUT=${STOP_TIMEOUT-10} # The semantics of these two functions differ from the way apachectl does # things -- attempting to start while running is a failure, and shutdown # when not running is also a failure. So we just do it the way init scripts # are expected to behave here. start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } # When stopping httpd, a delay (of default 10 second) is required # before SIGKILLing the httpd parent; this gives enough time for the # httpd parent to SIGKILL any errant children. stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} } reload() { echo -n $"Reloading $prog: " if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then RETVAL=6 echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else # Force LSB behaviour from killproc LSB=1 killproc -p ${pidfile} $httpd -HUP RETVAL=$? if [ $RETVAL -eq 7 ]; then failure $"httpd shutdown" fi fi echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart|try-restart) if status -p ${pidfile} $httpd >&/dev/null; then stop start fi ;; force-reload|reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}" RETVAL=2 esac exit $RETVAL [root@wc1 rc3.d]#
2、另一个开机自启动目录
这个目录就是:/etc/init.d,目录里也存放了开机自启动脚本。
不过可以发现这个目录只不过是一个连接目录,实际上对应的就是/etc/rc.d/init.d,也就是上面讲到的目录,目录中的文件和/etc/rc.d/init.d 中的文件完全一致。
[root@wc1 rc3.d]# ls -l /etc/init.d lrwxrwxrwx. 1 root root 11 Aug 4 21:02 /etc/init.d -> rc.d/init.d [root@wc1 rc3.d]# ls -l /etc/init.d/ total 408 -rwxr-xr-x. 1 root root 1288 Oct 16 2014 abrt-ccpp -rwxr-xr-x. 1 root root 1628 Oct 16 2014 abrtd -rwxr-xr-x. 1 root root 1642 Oct 16 2014 abrt-oops -rwxr-xr-x. 1 root root 1725 Aug 19 2010 acpid -rwxr-xr-x. 1 root root 2062 Jan 30 2012 atd -rwxr-xr-x. 1 root root 3580 Oct 15 2014 auditd -rwxr-xr-x. 1 root root 4043 Oct 16 2014 autofs -r-xr-xr-x. 1 root root 1340 Oct 16 2014 blk-availability -rwxr-xr-x. 1 root root 710 Nov 11 2010 bluetooth -rwxr-xr-x. 1 root root 2094 Oct 16 2014 certmonger -rwxr-xr-x. 1 root root 11355 Aug 13 2013 cpuspeed -rwxr-xr-x. 1 root root 2826 Nov 23 2013 crond -rwxr-xr-x. 1 root root 3034 Oct 15 2014 cups -rwxr-xr-x. 1 root root 1734 Jun 16 2014 dnsmasq -rwxr-xr-x. 1 root root 3245 Jul 9 2013 firstboot -rw-r--r--. 1 root root 19295 Jul 22 2014 functions -rwxr-xr-x. 1 root root 1801 Oct 15 2014 haldaemon -rwxr-xr-x. 1 root root 5866 Jul 22 2014 halt -rwxr-xr-x. 1 root root 2001 Oct 16 2014 htcacheclean -rwxr-xr-x. 1 root root 3371 Oct 16 2014 httpd -rwxr-xr-x. 1 root root 10804 Oct 15 2014 ip6tables -rwxr-xr-x. 1 root root 10688 Oct 15 2014 iptables -rwxr-xr-x. 1 root root 1938 Jun 2 2014 irqbalance -rwxr-xr-x. 1 root root 19476 Oct 16 2014 kdump -rwxr-xr-x. 1 root root 652 Jul 22 2014 killall -r-xr-xr-x. 1 root root 2134 Oct 16 2014 lvm2-lvmetad -r-xr-xr-x. 1 root root 2757 Oct 16 2014 lvm2-monitor -rwxr-xr-x. 1 root root 2571 Sep 5 2014 mdmonitor -rwxr-xr-x. 1 root root 2200 Sep 14 2012 messagebus -rwxr-xr-x. 1 root root 10815 Jul 3 2013 mysql -rwxr-xr-x. 1 root root 2989 Jul 22 2014 netconsole -rwxr-xr-x. 1 root root 6064 Jul 22 2014 netfs -rwxr-xr-x. 1 root root 6334 Jul 22 2014 network -rwxr-xr-x. 1 root root 2205 Oct 15 2014 NetworkManager -rwxr-xr-x. 1 root root 6878 Oct 16 2014 nfs -rwxr-xr-x. 1 root root 3526 Oct 16 2014 nfslock -rwxr-xr-x. 1 root root 1923 Jul 15 2013 ntpd -rwxr-xr-x. 1 root root 2043 Jul 15 2013 ntpdate -rwxr-xr-x. 1 root root 2261 Jun 25 2011 oddjobd -rwxr-xr-x. 1 root root 2023 Apr 3 2012 portreserve -rwxr-xr-x. 1 root root 3912 Feb 20 2014 postfix -rwxr-xr-x. 1 root root 1556 Jul 17 2012 psacct -rwxr-xr-x. 1 root root 2034 Mar 5 2014 quota_nld -rwxr-xr-x. 1 root root 1513 Sep 17 2013 rdisc -rwxr-xr-x. 1 root root 1822 Oct 16 2014 restorecond -rwxr-xr-x. 1 root root 1808 Dec 18 2011 rngd -rwxr-xr-x. 1 root root 2073 Feb 22 2013 rpcbind -rwxr-xr-x. 1 root root 2518 Oct 16 2014 rpcgssd -rwxr-xr-x. 1 root root 2305 Oct 16 2014 rpcidmapd -rwxr-xr-x. 1 root root 2464 Oct 16 2014 rpcsvcgssd -rwxr-xr-x. 1 root root 2011 Aug 15 2013 rsyslog -rwxr-xr-x. 1 root root 1698 Oct 16 2014 sandbox -rwxr-xr-x. 1 root root 2056 Jun 23 2014 saslauthd -rwxr-xr-x. 1 root root 647 Jul 22 2014 single -rwxr-xr-x. 1 root root 3002 Feb 22 2013 smartd -rwxr-xr-x. 1 root root 2162 Sep 15 2014 snmpd -rwxr-xr-x. 1 root root 1738 Sep 15 2014 snmptrapd -rwxr-xr-x. 1 root root 2472 Oct 15 2014 spice-vdagentd -rwxr-xr-x. 1 root root 4621 Oct 15 2014 sshd -rwxr-xr-x. 1 root root 2690 Oct 16 2014 sssd -rwxr-xr-x. 1 root root 1144 Oct 16 2014 sysstat -rwxr-xr-x. 1 root root 2294 Oct 16 2014 udev-post -rwxr-xr-x. 1 root root 38525 Aug 16 14:58 vmware-tools -rwxr-xr-x. 1 root root 15208 Aug 16 14:58 vmware-tools-thinprint -rwxr-xr-x. 1 root root 1674 Feb 22 2013 wdaemon -rwxr-xr-x. 1 root root 1608 Oct 15 2014 winbind -rwxr-xr-x. 1 root root 1866 Feb 4 2013 wpa_supplicant -rwxr-xr-x. 1 root root 4799 Feb 22 2013 ypbind
3、总结
/etc/rc.d/rcX.d 目录下面的文件是连接文件,连接到/etc/rc.d/init.d 目录下的文件,而/etc/init.d 目录就是链接目录,连接到 /etc/rc.d/init.d 目录。
-
linux设置服务开机自启动的三种方式
2021-01-20 15:53:38这里介绍一下linux开机自动启动的几种方法,共计3种,大家可以借鉴一下!经验里面以centos 5.3系统为例! 方法1:.利用ntsysv命令进行设置,利用root登陆 终端命令下输入ntsysv 回车;如下图 在这个界面上进行操作... -
systemd(systemctl)编程Linux开机自启动服务脚本的方法(教程)
2020-11-30 20:34:31systemd(systemctl)编程Linux开机自启动服务脚本的方法(教程) 过去Linux采用的是init.d的服务启动管理方式, 新版的Linux采用systemd服务启动管理方式, 请看教程讲解 -
Linux中 开机自启动目录
2018-07-29 10:18:53开机启动脚本是存放在 /etc/rc.d/rcX.d 目录中的,这里的X是runlevel,就是linux的运行级别,可以看到有rc0~rc6,共7种级别: 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动 运行级别1:...1、开机启动脚本
开机启动脚本是存放在 /etc/rc.d/rcX.d 目录中的,这里的X是runlevel,就是linux的运行级别,可以看到有rc0~rc6,共7种级别:
运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
运行级别2:多用户状态(没有NFS)
运行级别3:完全的多用户状态(有NFS),登陆后进入控制台命令行模式
运行级别4:系统未使用,保留
运行级别5:X11控制台,登陆后进入图形GUI模式
运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动[root@wc1 Desktop]# pwd /root/Desktop [root@wc1 Desktop]# cd /etc/rc.d [root@wc1 rc.d]# ls -l total 60 drwxr-xr-x. 2 root root 4096 Aug 16 14:58 init.d -rwxr-xr-x. 1 root root 2617 Oct 16 2014 rc drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc0.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc1.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc2.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc3.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc4.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc5.d drwxr-xr-x. 2 root root 4096 Aug 16 15:35 rc6.d -rwxr-xr-x. 1 root root 220 Oct 16 2014 rc.local -rwxr-xr-x. 1 root root 19914 Oct 16 2014 rc.sysinit
这里切换到rc3.d目录下,也就是完全的多用户模式:
[root@wc1 rc.d]# cd rc3.d [root@wc1 rc3.d]# ls -l total 0 lrwxrwxrwx. 1 root root 16 Aug 4 21:08 K01smartd -> ../init.d/smartd lrwxrwxrwx. 1 root root 17 Aug 4 21:06 K02oddjobd -> ../init.d/oddjobd lrwxrwxrwx. 1 root root 17 Aug 4 21:09 K05wdaemon -> ../init.d/wdaemon lrwxrwxrwx. 1 root root 16 Aug 4 21:08 K10psacct -> ../init.d/psacct lrwxrwxrwx. 1 root root 19 Aug 4 21:06 K10saslauthd -> ../init.d/saslauthd lrwxrwxrwx. 1 root root 22 Aug 4 21:06 K15htcacheclean -> ../init.d/htcacheclean lrwxrwxrwx. 1 root root 15 Aug 4 21:06 K15httpd -> ../init.d/httpd lrwxrwxrwx. 1 root root 24 Aug 4 21:08 K30spice-vdagentd -> ../init.d/spice-vdagentd lrwxrwxrwx. 1 root root 17 Aug 4 21:06 K50dnsmasq -> ../init.d/dnsmasq lrwxrwxrwx. 1 root root 20 Aug 4 21:05 K50netconsole -> ../init.d/netconsole lrwxrwxrwx. 1 root root 15 Aug 4 21:06 K50snmpd -> ../init.d/snmpd lrwxrwxrwx. 1 root root 19 Aug 4 21:06 K50snmptrapd -> ../init.d/snmptrapd lrwxrwxrwx. 1 root root 13 Aug 4 21:05 K60nfs -> ../init.d/nfs lrwxrwxrwx. 1 root root 20 Aug 4 21:05 K69rpcsvcgssd -> ../init.d/rpcsvcgssd lrwxrwxrwx. 1 root root 17 Aug 4 21:05 K73winbind -> ../init.d/winbind lrwxrwxrwx. 1 root root 14 Aug 4 13:47 K74ntpd -> ../init.d/ntpd lrwxrwxrwx. 1 root root 17 Aug 4 21:06 K75ntpdate -> ../init.d/ntpdate lrwxrwxrwx. 1 root root 19 Aug 4 21:08 K75quota_nld -> ../init.d/quota_nld lrwxrwxrwx. 1 root root 16 Aug 4 21:06 K76ypbind -> ../init.d/ypbind lrwxrwxrwx. 1 root root 15 Aug 4 13:47 K80kdump -> ../init.d/kdump lrwxrwxrwx. 1 root root 24 Aug 16 15:35 K84wpa_supplicant -> ../init.d/wpa_supplicant lrwxrwxrwx. 1 root root 21 Aug 4 21:05 K87restorecond -> ../init.d/restorecond lrwxrwxrwx. 1 root root 14 Aug 16 15:35 K88sssd -> ../init.d/sssd lrwxrwxrwx. 1 root root 15 Aug 4 21:05 K89rdisc -> ../init.d/rdisc lrwxrwxrwx. 1 root root 19 Aug 4 13:47 K95firstboot -> ../init.d/firstboot lrwxrwxrwx. 1 root root 14 Aug 4 21:08 K99rngd -> ../init.d/rngd lrwxrwxrwx. 1 root root 17 Aug 4 21:07 S01sysstat -> ../init.d/sysstat lrwxrwxrwx. 1 root root 22 Aug 4 21:07 S02lvm2-monitor -> ../init.d/lvm2-monitor lrwxrwxrwx. 1 root root 22 Aug 16 14:59 S03vmware-tools -> ../init.d/vmware-tools lrwxrwxrwx. 1 root root 19 Aug 4 21:06 S08ip6tables -> ../init.d/ip6tables lrwxrwxrwx. 1 root root 18 Aug 4 21:05 S08iptables -> ../init.d/iptables lrwxrwxrwx. 1 root root 17 Aug 4 21:05 S10network -> ../init.d/network lrwxrwxrwx. 1 root root 16 Aug 4 21:08 S11auditd -> ../init.d/auditd lrwxrwxrwx. 1 root root 21 Aug 4 21:02 S11portreserve -> ../init.d/portreserve lrwxrwxrwx. 1 root root 17 Aug 4 21:06 S12rsyslog -> ../init.d/rsyslog lrwxrwxrwx. 1 root root 18 Aug 4 21:08 S13cpuspeed -> ../init.d/cpuspeed lrwxrwxrwx. 1 root root 20 Aug 4 21:06 S13irqbalance -> ../init.d/irqbalance lrwxrwxrwx. 1 root root 17 Aug 4 21:03 S13rpcbind -> ../init.d/rpcbind lrwxrwxrwx. 1 root root 19 Aug 4 21:05 S15mdmonitor -> ../init.d/mdmonitor lrwxrwxrwx. 1 root root 20 Aug 4 21:02 S22messagebus -> ../init.d/messagebus lrwxrwxrwx. 1 root root 24 Aug 4 21:06 S23NetworkManager -> ../init.d/NetworkManager lrwxrwxrwx. 1 root root 17 Aug 16 15:35 S24nfslock -> ../init.d/nfslock lrwxrwxrwx. 1 root root 17 Aug 16 15:35 S24rpcgssd -> ../init.d/rpcgssd lrwxrwxrwx. 1 root root 26 Aug 4 21:07 S25blk-availability -> ../init.d/blk-availability lrwxrwxrwx. 1 root root 14 Aug 4 21:05 S25cups -> ../init.d/cups lrwxrwxrwx. 1 root root 15 Aug 4 21:05 S25netfs -> ../init.d/netfs lrwxrwxrwx. 1 root root 15 Aug 4 21:08 S26acpid -> ../init.d/acpid lrwxrwxrwx. 1 root root 19 Aug 4 21:05 S26haldaemon -> ../init.d/haldaemon lrwxrwxrwx. 1 root root 19 Aug 4 21:05 S26udev-post -> ../init.d/udev-post lrwxrwxrwx. 1 root root 16 Aug 4 21:06 S28autofs -> ../init.d/autofs lrwxrwxrwx. 1 root root 19 Aug 4 21:06 S50bluetooth -> ../init.d/bluetooth lrwxrwxrwx. 1 root root 14 Aug 4 21:07 S55sshd -> ../init.d/sshd lrwxrwxrwx. 1 root root 32 Aug 16 14:59 S57vmware-tools-thinprint -> ../init.d/vmware-tools-thinprint lrwxrwxrwx. 1 root root 15 Aug 16 15:35 S64mysql -> ../init.d/mysql lrwxrwxrwx. 1 root root 17 Aug 4 21:06 S80postfix -> ../init.d/postfix lrwxrwxrwx. 1 root root 19 Aug 4 21:03 S82abrt-ccpp -> ../init.d/abrt-ccpp lrwxrwxrwx. 1 root root 15 Aug 4 21:03 S82abrtd -> ../init.d/abrtd lrwxrwxrwx. 1 root root 15 Aug 4 21:06 S90crond -> ../init.d/crond lrwxrwxrwx. 1 root root 13 Aug 4 21:03 S95atd -> ../init.d/atd lrwxrwxrwx. 1 root root 20 Aug 4 21:06 S99certmonger -> ../init.d/certmonger lrwxrwxrwx. 1 root root 11 Aug 4 21:05 S99local -> ../rc.local
发现有很多K、S开头的后面跟着2位数字的文件,比如:K15httpd,K表示关闭,而15是指关闭的 优先级,也就是linux内核会按照系统预先设置好的runlevel,比如设置为3,那么就读取/etc/rc.d/rc3.d这个目录中的文件,然后按照先执行K开头,后执行S开头,并且从数字较小的开始执行,比如这里K15httpd,也就是排在第7个开始执行。
而且可以看到 K15httpd -> ../init.d/httpd ,也就是说 K15httpd是一个连接文件,连接到了相对于当前目录的上层目录下面的init.d/httpd,也就是目录/etc/rc.d/init.d :
[root@wc1 rc3.d]# ls -l /etc/rc.d/init.d total 408 -rwxr-xr-x. 1 root root 1288 Oct 16 2014 abrt-ccpp -rwxr-xr-x. 1 root root 1628 Oct 16 2014 abrtd -rwxr-xr-x. 1 root root 1642 Oct 16 2014 abrt-oops -rwxr-xr-x. 1 root root 1725 Aug 19 2010 acpid -rwxr-xr-x. 1 root root 2062 Jan 30 2012 atd -rwxr-xr-x. 1 root root 3580 Oct 15 2014 auditd -rwxr-xr-x. 1 root root 4043 Oct 16 2014 autofs -r-xr-xr-x. 1 root root 1340 Oct 16 2014 blk-availability -rwxr-xr-x. 1 root root 710 Nov 11 2010 bluetooth -rwxr-xr-x. 1 root root 2094 Oct 16 2014 certmonger -rwxr-xr-x. 1 root root 11355 Aug 13 2013 cpuspeed -rwxr-xr-x. 1 root root 2826 Nov 23 2013 crond -rwxr-xr-x. 1 root root 3034 Oct 15 2014 cups -rwxr-xr-x. 1 root root 1734 Jun 16 2014 dnsmasq -rwxr-xr-x. 1 root root 3245 Jul 9 2013 firstboot -rw-r--r--. 1 root root 19295 Jul 22 2014 functions -rwxr-xr-x. 1 root root 1801 Oct 15 2014 haldaemon -rwxr-xr-x. 1 root root 5866 Jul 22 2014 halt -rwxr-xr-x. 1 root root 2001 Oct 16 2014 htcacheclean -rwxr-xr-x. 1 root root 3371 Oct 16 2014 httpd -rwxr-xr-x. 1 root root 10804 Oct 15 2014 ip6tables -rwxr-xr-x. 1 root root 10688 Oct 15 2014 iptables -rwxr-xr-x. 1 root root 1938 Jun 2 2014 irqbalance -rwxr-xr-x. 1 root root 19476 Oct 16 2014 kdump -rwxr-xr-x. 1 root root 652 Jul 22 2014 killall -r-xr-xr-x. 1 root root 2134 Oct 16 2014 lvm2-lvmetad -r-xr-xr-x. 1 root root 2757 Oct 16 2014 lvm2-monitor -rwxr-xr-x. 1 root root 2571 Sep 5 2014 mdmonitor -rwxr-xr-x. 1 root root 2200 Sep 14 2012 messagebus -rwxr-xr-x. 1 root root 10815 Jul 3 2013 mysql -rwxr-xr-x. 1 root root 2989 Jul 22 2014 netconsole -rwxr-xr-x. 1 root root 6064 Jul 22 2014 netfs -rwxr-xr-x. 1 root root 6334 Jul 22 2014 network -rwxr-xr-x. 1 root root 2205 Oct 15 2014 NetworkManager -rwxr-xr-x. 1 root root 6878 Oct 16 2014 nfs -rwxr-xr-x. 1 root root 3526 Oct 16 2014 nfslock -rwxr-xr-x. 1 root root 1923 Jul 15 2013 ntpd -rwxr-xr-x. 1 root root 2043 Jul 15 2013 ntpdate -rwxr-xr-x. 1 root root 2261 Jun 25 2011 oddjobd -rwxr-xr-x. 1 root root 2023 Apr 3 2012 portreserve -rwxr-xr-x. 1 root root 3912 Feb 20 2014 postfix -rwxr-xr-x. 1 root root 1556 Jul 17 2012 psacct -rwxr-xr-x. 1 root root 2034 Mar 5 2014 quota_nld -rwxr-xr-x. 1 root root 1513 Sep 17 2013 rdisc -rwxr-xr-x. 1 root root 1822 Oct 16 2014 restorecond -rwxr-xr-x. 1 root root 1808 Dec 18 2011 rngd -rwxr-xr-x. 1 root root 2073 Feb 22 2013 rpcbind -rwxr-xr-x. 1 root root 2518 Oct 16 2014 rpcgssd -rwxr-xr-x. 1 root root 2305 Oct 16 2014 rpcidmapd -rwxr-xr-x. 1 root root 2464 Oct 16 2014 rpcsvcgssd -rwxr-xr-x. 1 root root 2011 Aug 15 2013 rsyslog -rwxr-xr-x. 1 root root 1698 Oct 16 2014 sandbox -rwxr-xr-x. 1 root root 2056 Jun 23 2014 saslauthd -rwxr-xr-x. 1 root root 647 Jul 22 2014 single -rwxr-xr-x. 1 root root 3002 Feb 22 2013 smartd -rwxr-xr-x. 1 root root 2162 Sep 15 2014 snmpd -rwxr-xr-x. 1 root root 1738 Sep 15 2014 snmptrapd -rwxr-xr-x. 1 root root 2472 Oct 15 2014 spice-vdagentd -rwxr-xr-x. 1 root root 4621 Oct 15 2014 sshd -rwxr-xr-x. 1 root root 2690 Oct 16 2014 sssd -rwxr-xr-x. 1 root root 1144 Oct 16 2014 sysstat -rwxr-xr-x. 1 root root 2294 Oct 16 2014 udev-post -rwxr-xr-x. 1 root root 38525 Aug 16 14:58 vmware-tools -rwxr-xr-x. 1 root root 15208 Aug 16 14:58 vmware-tools-thinprint -rwxr-xr-x. 1 root root 1674 Feb 22 2013 wdaemon -rwxr-xr-x. 1 root root 1608 Oct 15 2014 winbind -rwxr-xr-x. 1 root root 1866 Feb 4 2013 wpa_supplicant -rwxr-xr-x. 1 root root 4799 Feb 22 2013 ypbind [root@wc1 rc3.d]#
再来看一下/etc/rc.d/init.d/httpd文件的内容:
[root@wc1 rc3.d]# cat /etc/rc.d/init.d/httpd #!/bin/bash # # httpd Startup script for the Apache HTTP Server # # chkconfig: - 85 15 # description: The Apache HTTP Server is an efficient and extensible \ # server implementing the current HTTP standards. # processname: httpd # config: /etc/httpd/conf/httpd.conf # config: /etc/sysconfig/httpd # pidfile: /var/run/httpd/httpd.pid # ### BEGIN INIT INFO # Provides: httpd # Required-Start: $local_fs $remote_fs $network $named # Required-Stop: $local_fs $remote_fs $network # Should-Start: distcache # Short-Description: start and stop Apache HTTP Server # Description: The Apache HTTP Server is an extensible server # implementing the current HTTP standards. ### END INIT INFO # Source function library. . /etc/rc.d/init.d/functions if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi # Start httpd in the C locale by default. HTTPD_LANG=${HTTPD_LANG-"C"} # This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS="" # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server # with the thread-based "worker" MPM; BE WARNED that some modules may not # work correctly with a thread-based MPM; notably PHP will refuse to start. # Path to the apachectl script, server binary, and short-form for messages. apachectl=/usr/sbin/apachectl httpd=${HTTPD-/usr/sbin/httpd} prog=httpd pidfile=${PIDFILE-/var/run/httpd/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd} RETVAL=0 STOP_TIMEOUT=${STOP_TIMEOUT-10} # The semantics of these two functions differ from the way apachectl does # things -- attempting to start while running is a failure, and shutdown # when not running is also a failure. So we just do it the way init scripts # are expected to behave here. start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } # When stopping httpd, a delay (of default 10 second) is required # before SIGKILLing the httpd parent; this gives enough time for the # httpd parent to SIGKILL any errant children. stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} } reload() { echo -n $"Reloading $prog: " if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then RETVAL=6 echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else # Force LSB behaviour from killproc LSB=1 killproc -p ${pidfile} $httpd -HUP RETVAL=$? if [ $RETVAL -eq 7 ]; then failure $"httpd shutdown" fi fi echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart|try-restart) if status -p ${pidfile} $httpd >&/dev/null; then stop start fi ;; force-reload|reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}" RETVAL=2 esac exit $RETVAL [root@wc1 rc3.d]#
2、另一个开机自启动目录
这个目录就是:/etc/init.d,目录里也存放了开机自启动脚本。
不过可以发现这个目录只不过是一个连接目录,实际上对应的就是/etc/rc.d/init.d,也就是上面讲到的目录,目录中的文件和/etc/rc.d/init.d 中的文件完全一致。
[root@wc1 rc3.d]# ls -l /etc/init.d lrwxrwxrwx. 1 root root 11 Aug 4 21:02 /etc/init.d -> rc.d/init.d [root@wc1 rc3.d]# ls -l /etc/init.d/ total 408 -rwxr-xr-x. 1 root root 1288 Oct 16 2014 abrt-ccpp -rwxr-xr-x. 1 root root 1628 Oct 16 2014 abrtd -rwxr-xr-x. 1 root root 1642 Oct 16 2014 abrt-oops -rwxr-xr-x. 1 root root 1725 Aug 19 2010 acpid -rwxr-xr-x. 1 root root 2062 Jan 30 2012 atd -rwxr-xr-x. 1 root root 3580 Oct 15 2014 auditd -rwxr-xr-x. 1 root root 4043 Oct 16 2014 autofs -r-xr-xr-x. 1 root root 1340 Oct 16 2014 blk-availability -rwxr-xr-x. 1 root root 710 Nov 11 2010 bluetooth -rwxr-xr-x. 1 root root 2094 Oct 16 2014 certmonger -rwxr-xr-x. 1 root root 11355 Aug 13 2013 cpuspeed -rwxr-xr-x. 1 root root 2826 Nov 23 2013 crond -rwxr-xr-x. 1 root root 3034 Oct 15 2014 cups -rwxr-xr-x. 1 root root 1734 Jun 16 2014 dnsmasq -rwxr-xr-x. 1 root root 3245 Jul 9 2013 firstboot -rw-r--r--. 1 root root 19295 Jul 22 2014 functions -rwxr-xr-x. 1 root root 1801 Oct 15 2014 haldaemon -rwxr-xr-x. 1 root root 5866 Jul 22 2014 halt -rwxr-xr-x. 1 root root 2001 Oct 16 2014 htcacheclean -rwxr-xr-x. 1 root root 3371 Oct 16 2014 httpd -rwxr-xr-x. 1 root root 10804 Oct 15 2014 ip6tables -rwxr-xr-x. 1 root root 10688 Oct 15 2014 iptables -rwxr-xr-x. 1 root root 1938 Jun 2 2014 irqbalance -rwxr-xr-x. 1 root root 19476 Oct 16 2014 kdump -rwxr-xr-x. 1 root root 652 Jul 22 2014 killall -r-xr-xr-x. 1 root root 2134 Oct 16 2014 lvm2-lvmetad -r-xr-xr-x. 1 root root 2757 Oct 16 2014 lvm2-monitor -rwxr-xr-x. 1 root root 2571 Sep 5 2014 mdmonitor -rwxr-xr-x. 1 root root 2200 Sep 14 2012 messagebus -rwxr-xr-x. 1 root root 10815 Jul 3 2013 mysql -rwxr-xr-x. 1 root root 2989 Jul 22 2014 netconsole -rwxr-xr-x. 1 root root 6064 Jul 22 2014 netfs -rwxr-xr-x. 1 root root 6334 Jul 22 2014 network -rwxr-xr-x. 1 root root 2205 Oct 15 2014 NetworkManager -rwxr-xr-x. 1 root root 6878 Oct 16 2014 nfs -rwxr-xr-x. 1 root root 3526 Oct 16 2014 nfslock -rwxr-xr-x. 1 root root 1923 Jul 15 2013 ntpd -rwxr-xr-x. 1 root root 2043 Jul 15 2013 ntpdate -rwxr-xr-x. 1 root root 2261 Jun 25 2011 oddjobd -rwxr-xr-x. 1 root root 2023 Apr 3 2012 portreserve -rwxr-xr-x. 1 root root 3912 Feb 20 2014 postfix -rwxr-xr-x. 1 root root 1556 Jul 17 2012 psacct -rwxr-xr-x. 1 root root 2034 Mar 5 2014 quota_nld -rwxr-xr-x. 1 root root 1513 Sep 17 2013 rdisc -rwxr-xr-x. 1 root root 1822 Oct 16 2014 restorecond -rwxr-xr-x. 1 root root 1808 Dec 18 2011 rngd -rwxr-xr-x. 1 root root 2073 Feb 22 2013 rpcbind -rwxr-xr-x. 1 root root 2518 Oct 16 2014 rpcgssd -rwxr-xr-x. 1 root root 2305 Oct 16 2014 rpcidmapd -rwxr-xr-x. 1 root root 2464 Oct 16 2014 rpcsvcgssd -rwxr-xr-x. 1 root root 2011 Aug 15 2013 rsyslog -rwxr-xr-x. 1 root root 1698 Oct 16 2014 sandbox -rwxr-xr-x. 1 root root 2056 Jun 23 2014 saslauthd -rwxr-xr-x. 1 root root 647 Jul 22 2014 single -rwxr-xr-x. 1 root root 3002 Feb 22 2013 smartd -rwxr-xr-x. 1 root root 2162 Sep 15 2014 snmpd -rwxr-xr-x. 1 root root 1738 Sep 15 2014 snmptrapd -rwxr-xr-x. 1 root root 2472 Oct 15 2014 spice-vdagentd -rwxr-xr-x. 1 root root 4621 Oct 15 2014 sshd -rwxr-xr-x. 1 root root 2690 Oct 16 2014 sssd -rwxr-xr-x. 1 root root 1144 Oct 16 2014 sysstat -rwxr-xr-x. 1 root root 2294 Oct 16 2014 udev-post -rwxr-xr-x. 1 root root 38525 Aug 16 14:58 vmware-tools -rwxr-xr-x. 1 root root 15208 Aug 16 14:58 vmware-tools-thinprint -rwxr-xr-x. 1 root root 1674 Feb 22 2013 wdaemon -rwxr-xr-x. 1 root root 1608 Oct 15 2014 winbind -rwxr-xr-x. 1 root root 1866 Feb 4 2013 wpa_supplicant -rwxr-xr-x. 1 root root 4799 Feb 22 2013 ypbind
3、总结
/etc/rc.d/rcX.d 目录下面的文件是连接文件,连接到/etc/rc.d/init.d 目录下的文件,而/etc/init.d 目录就是链接目录,连接到 /etc/rc.d/init.d 目录。
-
基于Qt的开机自启动(二)(Linux)
2018-05-08 16:23:05基于Qt的开机自启,适用于Linux 环境下的程序自启动,易于重用 -
Linux下Redis设置密码及开机自启动
2020-09-15 13:23:02主要介绍了Linux下Redis设置密码及开机自启动的相关资料,需要的朋友可以参考下 -
Linux设置tomcat开机自启动
2016-12-27 15:22:08以上所有文档来源于ITIL先锋微信公众号 每天更新IT行业最前沿资讯和各种类别的IT资料,关注获取更多丰富内容 -
Linux jar包开机启动
2020-06-01 09:17:51Linux jar包开机自启脚本 ,更改linux的jdk目录 更改jar包名即可,然后在rc.local添加此脚本位置 -
ARM Linux 程序开机自启动方法
2015-05-28 23:01:53经常自己编写的应用程序,需要手动运行,本文档教你修改系统文件,让你的应用程序开机自启。 -
Linux配置开机自启动
2021-05-16 13:45:00自定义脚本通过系统提供的自启动脚本(这种方式适合添加需要开机自启的应用程序,比如 redis、nginx 等等)Linux 系统提供了一个开机启动的脚本( /etc/rc.local 或 /etc/rc.d/rc.local 这两个文件实际上是一个文件)1...先了解一下Linux一些脚本启动的顺序
/etc/rc.local -> /etc/profile -> 自定义脚本
通过系统提供的自启动脚本
(这种方式适合添加需要开机自启的应用程序,比如 redis、nginx 等等)
Linux 系统提供了一个开机启动的脚本( /etc/rc.local 或 /etc/rc.d/rc.local 这两个文件实际上是一个文件)
1、编辑 /etc/rc.local
vi /etc/rc.local
2、添加要执行的命令
注意:这里的执行命令都必须是全路径的,就算你添加到了 PATH 路径,自启动的时候,也是识别不到的(因为启动顺序是 /etc/rc.local -> /etc/profile )
例如,我这里添加的 redis 自启动:
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using thisfile.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
#Please note that you must run'chmod +x /etc/rc.d/rc.local'to ensure
# that this script will be executed during boot.touch /var/lock/subsys/local
# redis 启动命令/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
TIPS:如何你是第一次使用该脚本文件,需要给脚本文件授权可执行,命令如下:
chmod +x /etc/rc.local
chmod 说明:
+ 表示增加权限、- 表示取消权限、= 表示唯一设定权限
r 表示可读取,w 表示可写入,x 表示可执行
通过 chkconfig 添加自定义的启动脚本
(推荐用于linux开机自启动 java 程序)
1、切换到 init.d 目录
cd /etc/rc.d/init.d/
2、创建一个自启动脚本,这里就创建一个名为 autostart.sh 的脚本吧(名字自取)
touch autostart.sh //创建 autostart.sh 脚本文件
3、编辑脚本
vi autostart.sh //打开autostart.sh,进行编辑
4、编写脚本内容
(注意:为了防止脚本执行时,/etc/profile 里 export 的路径未生效,需要手动 source 一下,使配置立即生效)
#!/bin/sh#chkconfig:2345 80 90#description:开机自动启动的脚本程序
# 防止在脚本运行的时候,没有加载 JDK 在/etc/profile 的PATH添加的路径配置if [ ${JAVA_HOME} == '']; then
echo "source /etc/profile,使环境变量加载"source/etc/profilefi# 以下是脚本内容(自己根据需要进行编写)
# 待启动的application 路径
app_path_array=("/opt/eureka/"
"/opt/admin/")for app_path in ${app_path_array[*]}; do# 这里,很重要,如果不切换到 jar 包的文件夹,那么下面脚本执行的路径就是根路径(/),这会导致 springboot 读取不到 jar包下的 ./config/ 文件夹,而去读取根目录下的配置 /config 文件夹
cd ${app_path}
# 找到以后缀 .jar 结尾的文件
APP_NAME=`find -name *.jar `
# 启动
nohup java-jar ${APP_NAME} &
done
脚本文件内容说明:
第一行 “#!/bin/sh” 告诉系统使用的shell
第二行 “#chkconfig: 2345 80 90” 表示在2/3/4/5运行级别启动,启动序号(S80),关闭序号(K90);
第三行 “#description” 表示的是服务的描述信息
(注意:第一行和第二行必须写,否则后面使用 chkconfig 命令注册脚本到开机启动项时会报错 “服务 autostart.sh 不支持 chkconfig”)
扩展资料
Linux启动级别:
0 关机
1 单用户
2-5 多用户图形界面
6 重启
5、给脚本赋可执行权限
chmod +x autostart.sh
6、将脚本添加到开机启动项中,并启动脚本
chkconfig --add autostart.shchkconfig autostart.sh on
另:如果启动的脚本未达到想要的效果,可以查看脚本执行的日志来分析
查看脚本执行日志的方式(以脚本 autostart.sh为例):
1)journalctl -u autostart
2) systemctl status autostart
通过自定义service服务
(很少用到这种方式,不做评价)
以下是对上述博客的补充:
service脚本分为3个部分:[Unit] [Service] [Install]
1、Unit
Unit表明该服务的描述。最常用的场景就是定义该脚本执行的顺序,比如:
After=dbus.service
表示该service 在 dbus.service 之后再执行
2、Service
Service是脚本的关键部分,这一部分用于设置一些关键参数:
Type=forking: 后台运行模式
PIDFile: 存放PID文件的位置
ExecStart=xxx: 服务运行的具体执行命令
ExecReload=xxx: 服务重启的执行命令
EexcStop=xxx: 服务停止的执行命令
注:
该服务执行后,会有服务系统的日志
-
Linux下redis安装并设置开机自启(超详细)
2021-01-20 14:30:17也可以通过linux命令下载 wget http://download.redis.io/releases/redis-4.0.8.tar.gz 将安装包复制到指定的位置 cp redis-4.0.8.tar.gz /usr/local/redis 解压 tar -zxvf redis-4.0.8.tar.gz 通过make... -
Linux设置开机自启动脚本
2021-07-15 09:56:02这里介绍两种Linux开机自启动脚本的方式 也可用于开机自启动软件(把软件的启动写成脚本,然后再让脚本开机自启动即可) 方式1:/etc/profile.d/ 把自启动脚本放在/etc/profile.d/下 为了保证一定能执行,最好再加个... -
SUSE Linux下开机自动运行脚本命令的方法
2019-04-06 21:54:08以openSUSE 11和10 sp2启动文件的设置为例,开机设置程序自动执行 -
Linux系统开机自启动程序设置
2021-12-20 17:24:51用户可以在Linux系统配置自启动的程序,可以通过多种方式来实现。 1. rc.local 2. /etc/profile,~/.profile,~/.bashrc -
Linux设置程序开机自启动
2021-05-15 19:47:46Linux设置程序开机自启动大致有三种方法。一、通过rc.local设置自启动通过在/etc/rc.local的文件夹下添加shell指令即可在开机时自动启动指令。其内容的格式举例如下:#!/bin/shsudo mkdir /test该例子便是在开机时在... -
Linux环境关闭开机自启动服务
2022-04-12 12:53:51Linux关闭开机自启动服务 -
Linux系统下开机自动运行命令的方法
2020-03-04 11:34:33一般可以放在/etc/rc.d/rc.local文件中设置开机自动运行命令。文中介绍了在Bash和TCshell中的一些启动文件。 -
linux下设置自己的程序开机自启动
2021-05-09 06:22:321.开机启动时自动运行程序Linux加载后, 它将初始化硬件和设备驱动, 然后运行第一个进程init。init根据配置文件继续引导过程,启动其它进程。通常情况下,修改放置在 /etc/rc或 /etc/rc.d 或 /etc/rc?.d 目录下的... -
linux下redis开机自启动
2021-10-29 16:22:13若想将新的程序开机自启动,只需在该目录下添加一个自定义启动程序的脚本,然后设置相应规则即可。 修改redis内置的启动脚本 vi /data/redis-5.0.5/utils/redis_init_script 将图中箭头部分的内容修改为部署环境... -
Linux配置nginx自启动
2018-10-20 15:10:22Linux配置Nginx服务开机自启动(centos7.*配置方式不同),脚本中‘nginx=‘和‘NGINX_CONF_FILE=’后面的*****替换成自己的nginx安装路径。把脚本复制到init.d/目录下,添加chkconfig --add nginx即可。注意脚本的... -
Linux 设置程序开机自动启动
2019-08-31 23:29:11将程序设置为开机启动的方法不止一种,这里记录的,应该是最常用的一种。 root用户执行命令:ll /etc/rc.d/ 可以看到有下面这些文件: [centos@ip-172-31-42-26 init.d]$ ll /etc/rc.d/ 总用量 16K drwxr-xr... -
Linux 设置 jar 包开机自启动
2022-01-26 17:56:41初步学习Springboot编写服务,尝试部署到Linux环境下,并进行开机自启动。 Linux系统:Centos7.6 Java包:xxx.jar 流程 将 xxx.jar 包(及外部配置文件)放入Linuxu系统中,假设放到/home/test/路径下; 修改文件... -
Linux之开机自启动服务(两种方式)
2021-12-05 00:30:27linux开机启动自启动脚本两种方式 先要了解一下系统启动运行级别,请看这篇文章 链接: Linux之运行级别 rc.local 1首先创建一个要自启动的脚本 vi /etc/scripts/createFile.sh #!/bin/bash #开机创建一个文件夹 ... -
linux 配置开机自启动命令
2021-11-12 15:26:07touch /var/lock/subsys/local 是防止本文件被重复执行,当第一次执行了这个命令,之后就不会在读取和执行这个文件了,保障了开机自启动脚本只会被执行一次。 之后就是扩展我们自己的命令语句:这里我添加了三条语句... -
Linux 开机自启动多个脚本程序
2020-03-02 17:14:48Linux 开机自启动程序 -
Linux开机自动启动python脚本程序,或 Jetson nano或Jetson Xavier NX开机自动启动python脚本程序
2021-02-04 14:58:09文章目录:1 通过系统界面设置开机自动运行程序1.1 windows系统设置开机自动运行命令1.2 Linux开机设置开机自动运行命令2 Linux中设置开机自启动python脚本程序2.1 方法一:在`/etc/rc.local`中添加要运行的脚本命令... -
Linux下设置网卡开机自启动
2021-12-15 14:32:45每次开机后都要用 ifup eth0 命令 启动网卡,想着怎么可以开机自动启动网卡,于是就法百度了一下,方法...编辑开机自启动文件 vi /etc/rc.d/rc.local 添加 ifup eth0 或者 ifconfig eth0 up 感谢:纸短情长 ... -
Linux服务器配置redis开机自启动
2022-03-25 15:45:14登录服务器 vi /etc/init.d/redis 编辑模式,给文件添加以下内容: # chkconfig: 2345 10 90 # description: Start ...PATH=/usr/local/redis/bin #找到本机安装redis后,存放redis命令的目录 REDISPORT=6379