如何解决调用者脚本结束时结束的shell脚本
我有两个脚本: /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的所有此版本都存在相同的问题:
使用exec:
#!/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
有人可以在这里帮忙吗?
解决方法
我只是解决了这个问题:
自定义脚本的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
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。