Skip to content

启动

systemctl

  • 「service」 命令本身是一个shell脚本,它会在 /etc/init.d/ 目录查找指定的服务脚本,然后调用该服务脚本来完成任务;
  • service命令用于对系统服务进行管理,比如启动(start)、停止(stop)、重启(restart)、查看状态(status)等。
  • 命令格式:service 服务名 [start|stop|restart|reload|status]
bash
service redis start  # 等于在 /etc/init.d/ 目录下运行 ./redis start
  • 「systemctl」 命令兼容了 service 命令,且包含其他更强大功能
  • systemctl 用来管理 linux系统的多种资源:系统服务、硬件设备、挂载点、socket等;
  • 下面则主要介绍关于 service 系统服务的相关命令使用。因为 systemctl 一般用于实现服务自启动的脚本
点击查看完整代码实现
点击查看完整代码实现
bash
~~ 系统自启动时 启动 cron 服务,启用或禁用它
# systemctl enable crond.service
# systemctl disable ccrond.service
# systemctl is-active crond.service  ~~ 是否正在运行
# systemctl is-enabled crond.service ~~  是否建立了启动链接

~~ 启动、重启、停止、重载、杀死服务以及查看服务 httpd
# systemctl start httpd.service
# systemctl restart httpd.service
# systemctl stop httpd.service
# systemctl reload httpd.service
# systemctl status httpd.service
# systemctl kill apache.service

~~ 列出所有服务(包括启用的和禁用的)
# systemctl list-units      ~~  列出所有管理的资源单元
# systemctl list-unit-files --type=service   ~~ 只列出所有 service 类型资源

~~ 获取某个服务(httpd)的依赖性列表
# systemctl list-dependencies httpd.service

~~ 检查 httpd 服务的所有配置细节
# systemctl show httpd

:::

  • xxx.service 文件的详细配置介绍请上网自行查看,下面给个简单的模板介绍
ini
[Unit]
Description:描述
After:auditd.service 在auditd.service启动后才启动
ConditionPathExists: 执行条件

[Service]
EnvironmentFile: 变量所在文件
ExecStart: 执行启动命令
Restart: fail时重启

[Install]
Alias:服务别名
WangtedBy: 多用户模式下需要的

结束

  • kill [pid]:杀死或停止某进程,一般直接 kill 或者 kill -9,表示直接杀死
    • 可以用kill -l 查看全部可以使用的信号

nohup 和 &

  • nohup 和 & 组合可以不挂断地在后台运行进程,命令格式:nohup command [agrs..] [&]
  • & 表示程序可以在linux 后台运行,在当前 shell 界面 ctrl C 退出,该程序也能继续运行,它可以忽略 SIGINT 信号,不过它会随着 shell 程序的关闭而停止,这是因为 & 运行的进程对 SIGHUP 信号不免疫
  • 加上 nohup 就可以做到忽略SIGHUP信号
bash
# 在后台运行 lwl.py,且不随着 shell 关闭而死亡。永远存在
nohup python lwl.py &> /var/log/lwl.log &

查看

  • job -l:查看后台任务
  • ps aux

正在精进