/etc/init.d/functions运用实战配置system服务详解

简介: /etc/init.d/functions运用实战配置system服务详解

/etc/init.d/functions详解

functions这个脚本是给/etc/init.d里边的文件使用的。提供了一些基础的功能,看看里边究竟有些什么。首先会设置umask,path,还有语言环境,然后会设置success,failure,warning,normal几种情况下的字体颜色。下面再看看提供的重要方法:
checkpid:检查是否已存在pid,如果有一个存在,返回0(通过查看/proc目录)
daemon:启动某个服务。/etc/init.d目录部分脚本的start使用到这个
killproc:杀死某个进程。/etc/init.d目录部分脚本的stop使用到这个
pidfileofproc:寻找某个进程的pid
pidofproc:类似上面的,只是还查找了pidof命令
status:返回一个服务的状态
echo_success,echo_failure,echo_passed,echo_warning分别输出各类信息
success,failure,passed,warning分别记录日志并调用相应的方法
action:打印某个信息并执行给定的命令,它会根据命令执行的结果来调用 success,failure方法
strstr:判断$1是否含有$2
confirm:显示 "Start service $1 (Y)es/(N)o/(C)ontinue? [Y]"的提示信息,并返回选择结果

练习#调用函数

#调用函数

source /etc/init.d/functions
. /etc/init.d/functions

#以守护进程形式启动

daemon /usr/local/nginx-1.16.0/sbin/nginx

#退出当前进程

killproc /usr/local/nginx-1.16.0/sbin/nginx

#查看进程

pidofproc  /usr/local/nginx-1.16.0/sbin/nginx

nginx.sh

#!/bin/bash
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
. /etc/init.d/functions
exec=/usr/local/nginx/sbin/nginx
lock=/var/lock/subsys/nginx
prog=nginx
RETVAL=0
start(){
     pidofproc $exec > /dev/null
     [ $? = 0 ] && echo "$prog is already running" && exit
     daemon $exec
     [ $? = 0 ] && echo  "start $prog success" && touch $lock
}
reload(){
     pidofproc $exec > /dev/null
     [ $? = 0 ] && echo "$prog is running" && killproc $exec -HUP
     [ $? = 0 ] && echo "$prog does not run" && daemon $exec 
}
stop(){
      pidofproc $exec > /dev/null
      [ $? != 0 ] && echo "$prog have been stopped" && exit
      kill $exec 
      [ $? = 0 ] && echo "stop $prog success" && rm -rf $lock
}
case $1 in
     start)
       start
     ;;
     stop)
       stop
     ;;
     restart)
       stop
       start
     ;;
     reload)
       reload
     ;;
     status)
       status nginx
     ;;
     *)
      echo "USAGE: nginx {start | stop | restart | reload | status}"
     ;;
esac
exit 0

复制脚本到init.d目录

cp /home/shell/nginx.sh  /etc/init.d/nginx

查看当前系统启动数据

chkconfig --list
systemctl list-unit-files

添加服务到系统服务

chkconfig --add  nginx

服务操作

$ service  nginx start
Starting nginx (via systemctl):
$ systemctl status nginx
● nginx.service - SYSV: nginx is a World Wide Web server. It is used to serve
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: active (exited) since Thu 2020-06-11 16:11:25 CST; 4min 14s ago
     Docs: man:systemd-sysv-generator(8)
Jun 11 16:11:25 monitor1 systemd[1]: Starting SYSV: nginx is a World Wide Web server. It is used to serve...
Jun 11 16:11:25 monitor1 nginx[1361]: nginx is already running
Jun 11 16:11:25 monitor1 systemd[1]: Started SYSV: nginx is a World Wide Web server. It is used to serve.

其他操作

service nginx start
service nginx restart
service nginx reload
service nginx stop
systemctl start nginx
systemctl restart nginx
systemctl reload nginx 
systemctl status nginx
systemctl stop nginx

参考连接:

https://blog.51cto.com/11726705/2403439

相关文章
|
7月前
|
Python
【Error】DeprecationWarning: executable_path has been deprecated, please pass in a Service object
【Error】DeprecationWarning: executable_path has been deprecated, please pass in a Service object
110 2
|
7月前
|
JavaScript 前端开发
call和apply的区别
call和apply的区别
|
3月前
|
Linux Python
【Azure Function】Python Function部署到Azure后报错No module named '_cffi_backend'
ERROR: Error: No module named '_cffi_backend', Cannot find module. Please check the requirements.txt file for the missing module.
|
5月前
|
C++ Windows
vs2019 This application failed to start because it could not find or load the QT platform plugin
这篇文章介绍了在VS2019中解决QT程序运行时出现的“无法找到或加载QT平台插件”错误的步骤,通过将必要的DLL文件和插件目录复制到项目解决方案中解决了问题。
|
5月前
|
C# C++
【Azure Function】在VS Code中创建Function项目遇见 No .NET worker runtimes found
【Azure Function】在VS Code中创建Function项目遇见 No .NET worker runtimes found
DevTools failed to load source map: Could not load content for…System error: net::ERR_FILE_NOT_FOUN
DevTools failed to load source map: Could not load content for…System error: net::ERR_FILE_NOT_FOUN
|
存储 Shell Python
Python中os.system()、subprocess.run()、call()、check_output()的用法
Python中os.system()、subprocess.run()、call()、check_output()的用法
248 0
torch.distributed.init_process_group(‘gloo’, init_method=‘file://tmp/somefile’, rank=0, world_size=1
torch.distributed.init_process_group(‘gloo’, init_method=‘file://tmp/somefile’, rank=0, world_size=1
612 0
torch.distributed.init_process_group(‘gloo’, init_method=‘file://tmp/somefile’, rank=0, world_size=1
成功解决问题"h5py\h5r.pyx", line 145, in init h5py.h5r AttributeError: type object 'h5py.h5r.Reference' ha
成功解决问题"h5py\h5r.pyx", line 145, in init h5py.h5r AttributeError: type object 'h5py.h5r.Reference' ha

热门文章

最新文章