从 Cronjob 按顺序运行 Bash

从 Cronjob 按顺序运行 Bash
#!/bin/bash
# My first script
sleep 15 & wait
python pythonFileName.py & wait
python pythonFileName.py & wait
python pythonFileName.py & wait

我如何让它等待上一行执行完毕后再移动到下一行?

当您直接调用 bash 文件时,它工作正常,但是当从计划任务,它会执行所有操作而不等待前一个操作完成。

答案1

这将仅依次起作用,以下所有内容均相同:

#!/bin/bash
# My first script
sleep 15 & wait
python pythonFileName.py & wait
python pythonFileName.py & wait
python pythonFileName.py & wait


#!/bin/bash
# My first script
sleep 15 
python pythonFileName.py 
python pythonFileName.py 
python pythonFileName.py 

<Command> & wait 你不会因此得到任何特别的成就

&将在后台执行该过程

等待将使父进程等待子进程状态改变

UID       PID   PPID
root     28065 28064  0 12:48 ?        00:00:00 /bin/sh -c /bin/bash /home/8zero2/Desktop/b.sh 
root     28067 28065  0 12:48 ?        00:00:00 /bin/bash /home/8zero2/Desktop/b.sh
root     28074 28067  0 12:48 ?        00:00:00 sleep 15


root     28065 28064  0 12:48 ?        00:00:00 /bin/sh -c /bin/bash /home/8zero2/Desktop/b.sh 
root     28067 28065  0 12:48 ?        00:00:00 /bin/bash /home/8zero2/Desktop/b.sh
root     28075 28067  0 12:48 ?        00:00:00 python pythonFileName.py


root     28065 28064  0 12:48 ?        00:00:00 /bin/sh -c /bin/bash /home/8zero2/Desktop/b.sh 
root     28067 28065  0 12:48 ?        00:00:00 /bin/bash /home/8zero2/Desktop/b.sh
root     28077 28067  0 12:48 ?        00:00:00 python pythonFileName.py


root     28065 28064  0 12:48 ?        00:00:00 /bin/sh -c /bin/bash /home/8zero2/Desktop/b.sh 
root     28067 28065  0 12:48 ?        00:00:00 /bin/bash /home/8zero2/Desktop/b.sh
root     28074 28067  0 12:48 ?        00:00:00 python pythonFileName.py

将 ppid 和 pid 列关联起来,您会注意到在两种情况下都只按顺序执行

相关内容