【问题标题】:shell scripts that's ends when the caller script end调用者脚本结束时结束的 shell 脚本
【发布时间】:2020-10-19 10:29:30
【问题描述】:

我有两个脚本: /mnt/tmp/a.sh:

#!/bin/bash

echo hello >> /tmp/a.sh.log
i=0
/mnt/tmp/b.sh &

while [[ $i -lt "5" ]]
do
        echo "seconde : $i " >> /tmp/a.sh.log
        sleep 1
        i=$(($i+1))
done

和/mnt/tmp/b.sh

#!/bin/bash

echo hello >> /tmp/b.sh.log
i=0

while [[ $i -lt "10" ]]
do
        echo "seconde : $i " >> /tmp/b.sh.log
        sleep 1
        i=$(($i+1))
done

当我手动启动 /mnt/tmp/a.sh 时,这里是输出文件: /tmp/a.sh.log

hello
seconde : 0
seconde : 1
seconde : 2
seconde : 3
seconde : 4

和/tmp/b.sh.log

hello
seconde : 0
seconde : 1
seconde : 2
seconde : 3
seconde : 4
seconde : 5
seconde : 6
seconde : 7
seconde : 8
seconde : 9

但是在系统启动期间调用 /mnt/tmp/a.sh 时,输出文件如下:

/tmp/a.sh.log

hello
seconde : 0
seconde : 1
seconde : 2
seconde : 3
seconde : 4

和 /tmp/b.sh.log 你好

seconde : 0
seconde : 1
seconde : 2
seconde : 3
seconde : 4

当 /mnt/tmp/a.sh 结束时,它在后台启动的所有脚本(使用 &)结束!

/usr/bin/custom-script 在启动时调用 a.sh 脚本:

#!/bin/bash


echo "===========================================" > /tmp/start
echo "        custom script                      " >> /tmp/start
echo "===========================================" >> /tmp/start

mount /dev/mmcblk0p1 /mnt
/mnt/tmp/a.sh

此自定义脚本由 /lib/systemd/system/custom-script.service 配置为:

[Unit]
Description=start custom script at boot

[Service]
Type=simple
ExecStart=/bin/sh -c '/usr/bin/custom-script'

[Install]
WantedBy=multi-user.target

我正在使用基于 yocto 的系统在 SOM 板 imx6 上工作。

已经尝试过的解决方案:

所有这些版本的 /mnt/tmp/a.sh 都会出现同样的问题:

使用执行:

#!/bin/bash

echo hello >> /tmp/a.sh.log
i=0
(exec /mnt/tmp/b.sh )&

while [[ $i -lt "5" ]]
do
        echo "seconde : $i " >> /tmp/a.sh.log
        sleep 1
        i=$(($i+1))
done

使用来源:

#!/bin/bash

echo hello >> /tmp/a.sh.log
i=0
source /mnt/tmp/b.sh &

while [[ $i -lt "5" ]]
do
        echo "seconde : $i " >> /tmp/a.sh.log
        sleep 1
        i=$(($i+1))
done

使用 nohup:

#!/bin/bash

echo hello >> /tmp/a.sh.log
i=0
nohup /mnt/tmp/b.sh > /var/log/nohup.log & # I used /var/log/nohup.log because my system is read-only:nohup: can't open '/home/root/nohup.out': Read-only file system 

while [[ $i -lt "5" ]]
do
        echo "seconde : $i " >> /tmp/a.sh.log
        sleep 1
        i=$(($i+1))
done

有人可以帮忙吗?

【问题讨论】:

  • 尝试使用nohup
  • @DigvijayS 与 nohup 有同样的问题!
  • @azeddinebenomar :我不太明白你的问题。从您发布的输出中,a.sh 运行相同,无论是否在启动时启动。看起来你从 b.sh 得到了不同的输出,但是你没有解释你是如何从 b.sh 开始这两种情况的;所以区别在于b,而不是a
  • @user1934428 b.sh 是从 a.sh 内部启动的(第 5 行)
  • 我明白了。但是你在后台启动 b 。你至少应该等到a 结束,直到b 结束。

标签: bash shell exec screen systemd


【解决方案1】:

我只是解决问题:

自定义脚本的 systemd 服务文件应该有 type=forking 而不是 type=simple:

/lib/systemd/system/custom-script.service

[Unit]
Description=start custom script at boot

[Service]
Type=forking
ExecStart=/usr/bin/custom-script

[Install]
WantedBy=multi-user.target

谢谢!

【讨论】:

    猜你喜欢
    • 2015-03-04
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2014-07-26
    • 2011-01-03
    • 1970-01-01
    • 2011-01-15
    相关资源
    最近更新 更多