【併發】shell調用shell nohup command & 後臺執行

本萌(๑•ᴗ•๑)有一堆的爬蟲任務需要定時跑,且任務設置有大佬在後臺操控,本萌懶,想通過讀庫生成腳本操作,且調度crontab輔助

於是乎,本寶寶(づ。◕‿‿◕。)づ寫了一個腳本用crontab調用,來生成了一大坨的shell

那麼問題來了,一大坨( ⊙ o ⊙ )啊!而且每天變[大佬開心開心就好好啦。]

咦?是不是可以把我這一坨sh丟到一個sh裏用crontab調用!!!之後不就可以懶洋洋是賣萌啦~\(≧▽≦)/~啦啦啦

1. crontab比較懶(不想改) 只能寫一條

2. shell一大坨 合併到一個sh

3.任務實在實在是太多啦,不能排隊 要併發併發併發!!!

OK: [attention立正!(口令)]

nohup sh /webroot/xiayutian/t1.sh >> /webroot/xiayutian/t1.log 2>&1 &

測試用例 sh

root@xiayutian:/webroot/xiayutian
▶ ll t*.sh
-rw-r--r-- 1 root root 351 Apr 16 06:58 t.sh
-rw-r--r-- 1 root root 148 Apr 16 06:45 t1.sh
-rw-r--r-- 1 root root 155 Apr 16 06:45 t2.sh
-rw-r--r-- 1 root root 149 Apr 16 06:48 t3.sh
-rw-r--r-- 1 root root 148 Apr 16 06:48 t4.sh
-rw-r--r-- 1 root root 148 Apr 16 06:48 t5.sh

輸出log 

root@xiayutian:/webroot/xiayutian
▶ ll t*.log
-rw-r--r-- 1 root root 6.6M Apr 16 07:00 t1.log
-rw-r--r-- 1 root root 3.3M Apr 16 07:00 t2.log
-rw-r--r-- 1 root root 6.6M Apr 16 07:00 t3.log
-rw-r--r-- 1 root root 6.6M Apr 16 07:00 t4.log
-rw-r--r-- 1 root root 6.6M Apr 16 07:00 t5.log

sh內容,其中t.sh爲調用[父類]

root@xiayutian:/webroot/xiayutian
▶ tail t*.sh
==> t.sh <==
#!/bin/sh

nohup sh /webroot/xiayutian/t1.sh >> /webroot/xiayutian/t1.log 2>&1 &
nohup sh /webroot/xiayutian/t2.sh >> /webroot/xiayutian/t2.log 2>&1 &
nohup sh /webroot/xiayutian/t3.sh >> /webroot/xiayutian/t3.log 2>&1 &
nohup sh /webroot/xiayutian/t4.sh >> /webroot/xiayutian/t4.log 2>&1 &
nohup sh /webroot/xiayutian/t5.sh >> /webroot/xiayutian/t5.log 2>&1 &

==> t1.sh <==
echo `date +"%Y-%m-%d %H:%M:%S"`
i=0
while [ $i -lt 1000000 ];
do
    echo $i
    i=$((i+1))
done
echo hello
echo `date +"%Y-%m-%d %H:%M:%S"`


==> t2.sh <==
i=0
while [ $i -lt 500000 ];
do
        echo $i
    i=$((i+1))
done

echo world
echo `date +"%Y-%m-%d %H:%M:%S"`


==> t3.sh <==
echo `date +"%Y-%m-%d %H:%M:%S"`
i=0
while [ $i -lt 1000000 ];
do
    echo $i
    i=$((i+1))
done
echo hello3
echo `date +"%Y-%m-%d %H:%M:%S"`


==> t4.sh <==
echo `date +"%Y-%m-%d %H:%M:%S"`
i=0
while [ $i -lt 1000000 ];
do
    echo $i
    i=$((i+1))
done
echo hello
echo `date +"%Y-%m-%d %H:%M:%S"`


==> t5.sh <==
echo `date +"%Y-%m-%d %H:%M:%S"`
i=0
while [ $i -lt 1000000 ];
do
    echo $i
    i=$((i+1))
done
echo hello
echo `date +"%Y-%m-%d %H:%M:%S"`

 

開始執行☆✨

root@xiayutian:/webroot/xiayutian
▶ sh /webroot/xiayutian/t.sh

一下就不見啦~~~~(>_<)~~~~ 莫慌

 

root@xiayutian:/webroot/xiayutian
▶ head -n 3 t*.log

==> t1.log <==

2019-04-16 07:00:38
0

==> t2.log <==

2019-04-16 07:00:38
0

==> t3.log <==

2019-04-16 07:00:38
0

==> t4.log <==

2019-04-16 07:00:38
0

==> t5.log <==

2019-04-16 07:00:38
0
 

:-O喔喔喔,同時開始吧👀

別急哈

root@xiayutian:/webroot/xiayutian
▶ tail -n 2 t*.log
==> t1.log <==
hello
2019-04-16 07:00:49

==> t2.log <==
world
2019-04-16 07:00:44

==> t3.log <==
hello3
2019-04-16 07:00:48

==> t4.log <==
hello
2019-04-16 07:00:48

==> t5.log <==
hello
2019-04-16 07:00:49

 

是不是結束的超級快,棒棒噠b( ̄▽ ̄)d

大結局:

把 sh /webroot/xiayutian/t.sh 加入crontab

Bingo彩蛋:加個鎖(lock🔐)更完美(⊙o⊙)哦


#* * * * * /usr/bin/flock -w0 /tmp/t.lock -c '/webroot/xiayutian/t.sh'

PS.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章