linux脚本调用job,linux-bash shell脚本中job控制

前言

今天在制作docker镜像的时候,需要让nginx在前端执行,但是在nginx执行之后,需要获取nginx端口,增加加一个针对nginx监控的程序,而且要求假如nginx挂掉,这个容器就必需销毁。

2b4992ea2bbc1576b1239929cd65df1f.png

思路

在bash中,后端执行程序可以在命令参数之后加上 & 就可,假如需要继续前端执行 fg jobid。

例如下面写一个enterypoint脚本#!/bin/bash

/opt/nginx/sbin/nginx -g "daemon off;" &

PORT=$(ss -lntpd | grep '\"nginx\"' | awk '{split($5,a,":");print a[2]}'|head -n 1)

/nginx-monitor http://localhost:$PORT/status &

fg 1 >> /data/startup.log 2>&1

发现容器启动之后就关闭了,说明脚本有问题,没有程序在前端执行导致容器直接退出。

通过日志文件发现有报错:fg: no job control

4683b45e368518e5488fa962e88566be.png

通过man bash查看帮助

找到JOB CONTROL

A user typically employs this facility via an interactive interface supplied jointly by the operating system kernel's terminal driver and bash.

使用户一般在交互的人机界面中用这种功能。界面是由系统的终端驱动和 bash 共同提供的。

上面可以看错需要有界面才能控制,那在脚本里面有可能调使用吗?继续往下查找JOB关键字,可以看到有一个set -m参数,可以设置monitor模式

-m Monitor mode. Job control is enabled. This option is on by default for interactive shells on systems that support it (see JOB CONTROL above). Background processes run in a separate process group and a line containing their exit status is printed upon their completion.

monitor模式。作业控制被启使用。在支持这个选项的系统中,它在交互 shell中是默认启使用的 (参见上面的 JOB CONTROL 作业控制)。后端进程在单独的进程组中运行,结束时将打印出包含它们退出状态的一行信息。

24f310c4665f41041dbcbcf04954b381.png

修改后的脚本#!/bin/bash

set -m

/opt/nginx/sbin/nginx -g "daemon off;" &

PORT=$(ss -lntpd | grep '\"nginx\"' | awk '{split($5,a,":");print a[2]}'|head -n 1)

/nginx-monitor http://localhost:$PORT/status &

fg 1 >> /data/startup.log 2>&1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值