/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

相关文章
|
3月前
|
机器学习/深度学习 缓存 Python
Cannot find reference ‘TruncatedNormal‘ in ‘__init__.py‘
本文提供了解决在PyCharm中运行论文复现代码时出现的"Cannot find reference 'TruncatedNormal' in '__init__.py'"错误的两种方法:清除缓存和重启,以及在`__init__.py`文件中加入`TruncatedNormal`类。
|
3月前
|
C++ Windows
vs2019 This application failed to start because it could not find or load the QT platform plugin
这篇文章介绍了在VS2019中解决QT程序运行时出现的“无法找到或加载QT平台插件”错误的步骤,通过将必要的DLL文件和插件目录复制到项目解决方案中解决了问题。
|
4月前
|
Python
Cannot find reference ‘args‘ in ‘__init__.pyi‘ ,request要写对
Cannot find reference ‘args‘ in ‘__init__.pyi‘ ,request要写对
|
5月前
|
Unix Docker 容器
使用docker 启动naocs 报错出现:standard_init_linux.go:241: exec user process caused "exec format error"
```markdown Error in Docker container startup: "standard_init_linux.go:241: exec user process caused \"exec format error\"". Occurred at 2024-06-29 09:26:19.910, followed by a failed hook with a syslog delivery error at 09:27:20.193. Seeking solutions from experts. ```
|
5月前
|
存储 设计模式 缓存
《500 Lines or Less》(4)Contingent: A Fully Dynamic Build System(构建系统)
《500 Lines or Less》(4)Contingent: A Fully Dynamic Build System(构建系统)
|
6月前
|
弹性计算 Dubbo Serverless
Serverless 应用引擎操作报错合集之阿里函数计算中配置完fc,出现‘Function instance exited unexpectedly(code 1, message:operation not permitted) with start command 'npm run start '. 报错如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
202 5
|
存储 Shell Python
Python中os.system()、subprocess.run()、call()、check_output()的用法
Python中os.system()、subprocess.run()、call()、check_output()的用法
238 0
安装 xgboost 报错ERROR: Command "python setup.py egg_info" failed with error code 1 in /private/var/fold
安装 xgboost 报错ERROR: Command "python setup.py egg_info" failed with error code 1 in /private/var/fold
安装 xgboost 报错ERROR: Command "python setup.py egg_info" failed with error code 1 in /private/var/fold
Could not initialize English chunker/Could not load file from classpath: ‘/en-token.bin‘
Could not initialize English chunker/Could not load file from classpath: ‘/en-token.bin‘
101 0
|
Oracle 关系型数据库 Linux
Oracle Linux: Shell Script to Calculate Values Recommended Linux HugePages / HugeTLB Configuration (Doc ID 401749.1)
Oracle Linux: Shell Script to Calculate Values Recommended Linux HugePages / HugeTLB Configuration (Doc ID 401749.1)
215 0