shell 个在调用者脚本结束时结束的脚本
shell scripts that's ends when the caller script end
我有两个脚本:
/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结束它在后台启动的所有脚本(使用&)结束!
a.sh 脚本在启动时被 /usr/bin/custom-script 调用:
#!/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
有人可以帮忙吗?
我刚刚解决了问题:
custom-script 的 systemd 服务文件应该有 type=forking 而不是 type=simple:
/lib/systemd/system/custom-script.服务
[Unit]
Description=start custom script at boot
[Service]
Type=forking
ExecStart=/usr/bin/custom-script
[Install]
WantedBy=multi-user.target
谢谢!
我有两个脚本: /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结束它在后台启动的所有脚本(使用&)结束!
a.sh 脚本在启动时被 /usr/bin/custom-script 调用:
#!/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
有人可以帮忙吗?
我刚刚解决了问题:
custom-script 的 systemd 服务文件应该有 type=forking 而不是 type=simple:
/lib/systemd/system/custom-script.服务
[Unit]
Description=start custom script at boot
[Service]
Type=forking
ExecStart=/usr/bin/custom-script
[Install]
WantedBy=multi-user.target
谢谢!