Linux 设置程序开机自动启动 |
您所在的位置:网站首页 › linux设置开机启动项目 › Linux 设置程序开机自动启动 |
目录
1. 基础说明 2. 编辑启动文件 3. 给启动文件添加权限 4. 设置软连接 1. 基础说明将程序设置为开机启动的方法不止一种,这里记录的,应该是最常用的一种。 root用户执行命令:ll /etc/rc.d/ 可以看到有下面这些文件: [centos@ip-172-31-42-26 init.d]$ ll /etc/rc.d/ 总用量 16K drwxr-xr-x. 10 root root 127 10/30 22:57 . drwxr-xr-x. 85 root root 8.0K 06/02 03:42 .. drwxr-xr-x. 2 root root 70 06/02 03:10 init.d drwxr-xr-x. 2 root root 45 10/30 22:57 rc0.d drwxr-xr-x. 2 root root 45 10/30 22:57 rc1.d drwxr-xr-x. 2 root root 45 10/30 22:57 rc2.d drwxr-xr-x. 2 root root 45 10/30 22:57 rc3.d drwxr-xr-x. 2 root root 45 10/30 22:57 rc4.d drwxr-xr-x. 2 root root 45 10/30 22:57 rc5.d drwxr-xr-x. 2 root root 45 10/30 22:57 rc6.d -rw-r--r--. 1 root root 473 04/25 17:19 rc.local其中 rc0~6.d 中的 0~6 代表运行级别: 0:系统停机(关机)模式,系统默认运行级别不能设置为0,否则不能正常启动,一开机就自动关机。 1:单用户模式,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式登录。 2:多用户模式,没有NFS网络支持。 3:完整的多用户文本模式,有NFS,登陆后进入控制台命令行模式。 4:系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置。 5:图形化模式,登陆后进入图形GUI模式或GNOME、KDE图形化界面,如X Window系统。 6:重启模式,默认运行级别不能设为6,否则不能正常启动,就会一直开机重启开机重启。设置程序开机启动,要做的事情就是: 在 init.d 目录下创建一个启动脚本(将程序启动命令放在里面)然后增加它的可执行权限最后设置 rcxx.d 到启动脚本的软连接 2. 编辑启动文件启动文件示例如下,编辑之后保存(我设置的名字是:ssserverctl): #!/bin/bash ### BEGIN INIT INFO # Provides: Neucrack # Required-Start: $remote_fs $syslog $network $named # Required-Stop: $remote_fs $syslog $network # Should-Start: $network $portmap # Should-Stop: $network $portmap # X-Start-Before: nis # X-Stop-After: nis # Default-Start: 3 5 # Default-Stop: 0 1 2 4 6 # X-Interactive: true # Short-Description: ss auto start script # Description: shadowsocks auto start script, # This file should be used to construct scripts to be # placed in /etc/init.d. ### END INIT INFO case "$1" in start) `nohup ssserver -c /opt/config/shadowsocks.json > ~/backup/temp/connect.log 2>&1 &` ;; stop) `nohup ps aux | grep ssserver | awk '{print $2}' | xargs kill -9 2>&1 &` ;; restart) $0 stop $0 start ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 ;; esac需要注意的是,启动命令不要是阻塞式的,避免脚本执行之后等待输入。 3. 给启动文件添加权限执行命令:chmod +x ssserverctl [root@izj6ca57bbyivzigu89mfoz:init.d]$ ll 总用量 64 -rwxr-xr-x 1 root root 972 8月 25 23:36 ssserverctl设置完成之后,可以执行下面的命令来检查脚本是否有效(start/stop对应脚本里面的参数): /etc/init.d/ssserverctl start /etc/init.d/ssserverctl stop 4. 设置软连接设置软连接的时候,要注意命名规则。 查看已有的软连接:ll /etc/rc.d/rc3.d/ [root@izj6ca57bbyivzigu89mfoz:init.d]$ ll /etc/rc.d/rc3.d/ 总用量 0 lrwxrwxrwx. 1 root root 20 8月 18 2017 K50netconsole -> ../init.d/netconsole lrwxrwxrwx. 1 root root 17 8月 18 2017 S10network -> ../init.d/network lrwxrwxrwx 1 root root 22 8月 24 2017 S15staragentctl -> ../init.d/staragentctl lrwxrwxrwx 1 root root 15 8月 13 19:42 S50aegis -> ../init.d/aegis lrwxrwxrwx 1 root root 16 8月 13 19:42 S64mysqld -> ../init.d/mysqld lrwxrwxrwx 1 root root 19 8月 24 2017 S85apachectl -> ../init.d/apachectl lrwxrwxrwx 1 root root 17 8月 24 2017 S85php-fpm -> ../init.d/php-fpm lrwxrwxrwx 1 root root 23 8月 25 23:42 S91ssserverctl -> /etc/init.d/ssserverctl可以发现在启动脚本前面都加了 “K数字”,或者 “S数字” 其中 K 表示 Kill 某个程序,S 表示 Start 某个程序 后面紧跟着的数字,表示启动/停止某个程序的顺序,数字越小的越先启动(数字的具体值可以自己根据情况自行设定) 我这里设置软连接用的命令是: ln -s /etc/init.d/ssserverctl /etc/rc3.d/S91ssserverctl如果要设置程序关机自动停止,可以新增一个软连接: ln -s /etc/init.d/ssserverctl /etc/rc0.d/K91ssserverctl如此设置之后,开机重启,检查程序是否可以开机自动启动。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |