系统启动时需要加载的配置文件
/etc/profile、/root/.bash_profile/etc/bashrc、/root/.bashrc/etc/profile.d/*.sh、/etc/profile.d/lang.sh/etc/sysconfig/i18n、/etc/rc.local(/etc/rc.d/rc.local)
一、修改开机启动文件:/etc/rc.local(或者/etc/rc.d/rc.local)
# 1.编辑rc.local文件[root@localhost ~]# vi /etc/rc.local# 2.修改rc.local文件,在 exit 0 前面加入以下命令。保存并退出。/etc/init.d/mysqld sTart# mysql开机启动/etc/init.d/nginx start# nginx开机启动supervisord -c /etc/supervisor/supervisord.conf# supervisord开机启动/bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null# 3.最后修改rc.local文件的执行权限[root@localhost ~]# chmod +x /etc/rc.local[root@localhost ~]# chmod 755 /etc/rc.local
二、自己写一个shell脚本
将写好的脚本(.sh文件)放到目录 /etc/profile.d/ 下,系统启动后就会自动执行该目录下的所有shell脚本。
三、通过chkconfig命令设置
# 1.将(脚本)启动文件移动到 /etc/init.d/或者/etc/rc.d/init.d/目录下。(前者是后者的软连接)mv /www/wwwroot/test.sh /etc/rc.d/init.d# 2.启动文件前面务必添加如下三行代码,否侧会提示chkconfig不支持。#!/bin/sh告诉系统IDC.com/xtywjcwz/22677.html" target="_blank">使用的shell,所以的shell脚本都是这样#chkconfig: 35 20 80分别代表运行级别,启动优先权,关闭优先权,此行代码必须#description: http server自己随便发挥!!!,此行代码必须/bin/echo $(/bin/date +%F_%T) >> /tmp/test.log# 3.增加脚本的可执行权限chmod +x /etc/rc.d/init.d/test.sh# 4.添加脚本到开机自动启动项目中。添加到chkconfig,开机自启动。[root@localhost ~]# cd /etc/rc.d/init.d[root@localhost ~]# chkconfig --add test.sh[root@localhost ~]# chkconfig test.sh on# 5.关闭开机启动[root@localhost ~]# chkconfig test.sh off# 6.从chkconfig管理中删除test.sh[root@localhost ~]# chkconfig --del test.sh# 7.查看chkconfig管理[root@localhost ~]# chkconfig --list test.sh
四、自定义服务文件,添加到系统服务,通过Systemctl管理
1.写服务文件:如nginx.service、redis.service、supervisord.service
[Unit]:服务的说明Description:描述服务After:描述服务类别
[Service]服务运行参数的设置Type=forking 是后台运行的形式ExecStart 为服务的具体运行命令ExecReload 为服务的重启命令Execstop 为服务的停止命令PrivateTmp=True 表示给服务分配独立的临时空间注意:启动、重启、停止命令全部要求使用绝对路径
[Install] 服务安装的相关设置,可设置为多用户WantedBy=multi-user.target
2.文件保存在目录下:以754的权限。目录路径:/usr/lib/systemd/system。如上面的supervisord.service文件放在这个目录下面。
[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service[root@localhost ~]# cat /usr/lib/systemd/system/supervisord.service
3.设置开机自启动(任意目录下执行)。如果执行启动命令报错,则执行:systemctl daemon-reload
设置开机自启动[root@localhost ~]# systemctl enable nginx.service[root@localhost ~]# systemctl enable supervisord停止开机自启动[root@localhost ~]# systemctl disable nginx.service[root@localhost ~]# systemctl disable supervisord验证一下是否为开机启动[root@localhost ~]# systemctl is-enabled nginx[root@localhost ~]# systemctl is-enabled supervisord
4.其他命令

启动nginx服务[root@localhost ~]# systemctl start nginx.service停止nginx服务[root@localhost ~]# systemctl start nginx.service重启nginx服务[root@localhost ~]# systemctl restart nginx.service查看nginx服务当前状态[root@localhost ~]# systemctl status nginx.service查看所有已启动的服务[root@localhost ~]# systemctl list-units --type=service
5.服务文件示例:
# supervisord.service进程管理服务文件[Unit]Description=Process Monitoring and Control Daemon # 内容自己定义:Description=Supervisor daemonAfter=rc-local.service nss-user-lookup.target
[Service]Type=forkingExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.confExecStop= /usr/bin/supervisorctl shutdownExecReload=/usr/bin/supervisorctl reloadRestart=on-failureRestartSec=42sKillMode=process
[Install]WantedBy=multi-user.target
# nginx.service服务文件[Unit]Description=nginx – high performance web serverAfter=network.target remote-fs.target nss-lookup.target
[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]WantedBy=multi-user.target
# redis.service服务文件[Unit]Description=RedisAfter=network.target remote-fs.target nss-lookup.target
[Service]Type=forkingExecStart=/usr/local/bin/redis-server /etc/redis.confExecStop=kill -INT `cat /tmp/redis.pid`User=wwwGroup=www
[Install]WantedBy=multi-user.target
本篇文章到此结束,如果您有相关技术方面疑问可以联系我们技术人员远程解决,感谢大家支持本站!
香港服务器首选树叶云,2H2G首月10元开通。树叶云(shuyeidc.com)提供简单好用,价格厚道的香港/美国云 服务器 和独立服务器。IDC+ISP+ICP资质。ARIN和APNIC会员。成熟技术团队15年行业经验。
什么是 daemon 与服务 (service):daemon 的启动方式: service
知道了一些有关 daemon 的相关知识后,再来,那么我们如何启动一个 daemon 呢? 其实,我们知道所谓的 daemon 就是一支可以在系统背景下面运作的程式 (program) 啊, 所以,要启动该 daemon ,就是找到他的执行档,执行他就是了。 不过,因为该 daemon 的执行档所需要加的参数太多了!举例来说,你可以使用‘man syslogd’及‘man sshd’来查阅一下该 daemon 要启动时的设定参数!为了克服这样的困扰,所以各主要 Linux distributions 都会针对该服务设计一个比较亲和的 shell script 来进行启动的程序啊!那就是 /etc/init.d/ 底下的档案,以及 /etc/xinetd.d/ 底下的设定资料。 因此,启动服务的方法就变得很简单了。 只要设定好该服务的设定档,然后下达:1. 启动 stand alone 服务的方式:以 syslog 为例:[root@linux ~]# /etc/init.d/syslog start2. 启动 super daemon 服务的方式:以 telnet 为例:[root@linux ~]# vi /etc/xinetd.d/telnet (设定方式参考下节)[root@linux ~]# /etc/init.d/xinetd restart另外,除了这样的启动方式之外,我们还可以透过 Fedora ( Red Hat 系统 ) 所提供的 service 这个程式来进行 daemon 的启动喔!其实 service 仅是一支 script 啦, 他可以解析后面带有的参数,然后去到 /etc/init.d/ 去启动相对应的服务名称的 script 而已! 有兴趣的话,可以自行去解析 /sbin/service 这支 shell script 啊! 底下我们大略说明一下他的用法![root@linux ~]# service [service name] (start|stop|restart|...)参数:service name:亦即是需要启动的服务名称,需与 /etc/init.d/ 对应;start|... :亦即是该服务要进行的工作。 范例:范例一:重新启动 crond 这支 daemon :[root@linux ~]# service crond restart[root@linux ~]# /etc/init.d/crond restart在上面的范例当中,其实启动方式以 service 这个程式,或者直接去到 /etc/init.d/ 底下启动, 都一样啦!自行去解析 /sbin/service 就知道为啥了! ^_^Tips:事实上,在 Linux 系统中,要‘开或关某个 port ’,就是需要‘ 启动或关闭某个服务’啦!因此,你可以找出某个 port 对应的服务,程式对应的服务, 进而启动或关闭他,那么那个经由该服务而启动的 port ,自然就会关掉了!
如何在linux嵌入式开发板上开机自动运行用户应用程序
开机后自动运行用户的应用程序或启动系统服务的命令保存在开发板根文件系统的/usr/etc/文件中。 有的开发板开机后自动运行图形界面程序,需要按住ctrl+c让开发板进入到linux的SHELL提示符界面。 其实可通过注释掉文件中调用图形界面的命令,增加运行用户应用程序的命令,达到开机自动运行用户应用程序的目的。 下面以我做的实验为例,描述具体的实现步骤。 该方法源于网络,我加以验证,稍做修改,此文相当于转载。 1.进入pc机的Linux 操作系统,在/nfs/usr/下通过mkdir lz 命令新建一个名为lz的文件夹,进入lz文件夹,通过mkdir hello新建一个hello文件夹用来存放我们将要编写的hello.c文件和编译生成的可执行文件。 2.在/nfs/usr/lz/hello下通过vi hello.c命令新建hello.c文件,编辑如下测试程序:#include int main(){ printf(Hello,test arm-linux!\n); return 0; } 完成编辑后通过:wq保存后退出。 3.主机通过如下命令交叉编译环境编译hello.c: #arm-linux-gcc –o hello hello.c 4.通过ls 命令可以看到在/nfs/usr/lz/hello/下已经生成了hello可执行文件,我们可以在开发板上通过./hello来测试自己编写的hello.c执行情况 5.修改文件,在文件的最后通过‘#’释掉启动图形界面的指令,增加执行用户应用程序hello的指令,具体实现如下: #export PATH=$QPEDIR/bin:$PATH #qtopia #/usr/qtopia/bin/qtopia /usr/lz/hello/./hello 注:前三行是注释掉启动图形界面,最后一行是添加的执行用户的hello测试程序。 6.重启开发板,通过vivi参数配置让开发板通过nfs挂载主机上的文件系统,这时我们就可以通过超级终端看到开发板已经运行了我们编写的hello程序。
linux下如何启动dhcp服务
先用yum安装DHCP服务, 命令:yum -y install dhcp 启动:service dhcp start
发表评论