作者 主題: 如何在crontab 執行shell script ?  (閱讀 7569 次)

0 會員 與 1 訪客 正在閱讀本文。

NARs

  • 活潑的大學生
  • ***
  • 文章數: 227
    • 檢視個人資料
如何在crontab 執行shell script ?
« 於: 2012-08-08 14:14 »
各位大大,
我想要在crontab中排程定時執行一支shell script

test.sh :
代碼: [選擇]
export PATH=/bin:/sbin:/usr/bin:/usr/sbin

test1=$(route)

echo "$test1" > aaa.txt

echo "#`date`" >> aaa.txt

我以root的身份執行crontab -e ,加入下面這行在下午一點38分時執行

38  13  *  * *  /home/pinky/test.sh

可是如果有執行的話,應該會新增資料在aaa.txt,可是卻沒有執行這支test.sh,請問是那裡錯了嗎??

twu2

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 5416
  • 性別: 男
    • 檢視個人資料
    • http://blog.teatime.com.tw/1
Re: 如何在crontab 執行shell script ?
« 回覆 #1 於: 2012-08-08 14:19 »
沒指定路徑... 你確定你去找那個檔案的地方是正確的嗎?

有沒有跑, 請查 log.

NARs

  • 活潑的大學生
  • ***
  • 文章數: 227
    • 檢視個人資料
Re: 如何在crontab 執行shell script ?
« 回覆 #2 於: 2012-08-08 14:28 »
路徑是正確的,查了 /var/log/cron

代碼: [選擇]
Aug  8 14:20:01 pinky_pc crond[24425]: (root) CMD (/home/pinky/test.sh)
Aug  8 14:20:03 pinky_pc crontab[24254]: (root) END EDIT (root)
Aug  8 14:21:01 pinky_pc crond[24461]: (root) CMD (/home/pinky/test.sh)
Aug  8 14:22:01 pinky_pc crond[24514]: (root) CMD (/home/pinky/test.sh)
Aug  8 14:22:24 pinky_pc crond[24532]: (CRON) STARTUP (V5.0)
Aug  8 14:23:01 pinky_pc crond[24542]: (root) CMD (/home/pinky/test.sh)
Aug  8 14:24:01 pinky_pc crond[24553]: (root) CMD (/home/pinky/test.sh)
這是有執行嗎?

Nansen

  • 活潑的大學生
  • ***
  • 文章數: 212
    • 檢視個人資料
Re: 如何在crontab 執行shell script ?
« 回覆 #3 於: 2012-08-08 15:49 »
ls -la /home/pinky/test.sh
是否具有可執行屬性 x-- ?

NARs

  • 活潑的大學生
  • ***
  • 文章數: 227
    • 檢視個人資料
Re: 如何在crontab 執行shell script ?
« 回覆 #4 於: 2012-08-08 15:53 »
有~~~
-rwxr-xr-x 1 root root 120 Aug  8 14:53 /home/pinky/test.sh

twu2

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 5416
  • 性別: 男
    • 檢視個人資料
    • http://blog.teatime.com.tw/1
Re: 如何在crontab 執行shell script ?
« 回覆 #5 於: 2012-08-08 16:44 »
請指定用什麼 shell 跑.
你確定那個檔案內的語法不管用到什麼 shell 都能用嗎?

NARs

  • 活潑的大學生
  • ***
  • 文章數: 227
    • 檢視個人資料
Re: 如何在crontab 執行shell script ?
« 回覆 #6 於: 2012-08-08 16:54 »
改成

代碼: [選擇]
#!/bin/sh
test1=$(route)

echo "$test1" > aaa.txt

echo "#`date`" >> aaa.txt

直接去執行 /home/pinky/test.sh,是可以執行的
但是在crontab中卻沒有執行(沒有產生aaa.txt)

yc123

  • 懷疑的國中生
  • **
  • 文章數: 84
    • 檢視個人資料
Re: 如何在crontab 執行shell script ?
« 回覆 #7 於: 2012-08-08 17:35 »
應該是有成功,只是aaa.txt產生在root的目錄裡。

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
Re: 如何在crontab 執行shell script ?
« 回覆 #8 於: 2012-08-08 18:03 »
記得用 cront 去砲行程,最好先確定一下有沒設 HOME ,
要不就用絕對路徑吧...

aizr1212

  • 可愛的小學生
  • *
  • 文章數: 11
    • 檢視個人資料
Re: 如何在crontab 執行shell script ?
« 回覆 #9 於: 2012-08-10 17:47 »
 
echo "$test1" > aaa.txt

在  > aaa.txt  必須指定路徑, 要不然cron 它不知道要放哪.  所以是  echo "$test1" > /路徑/aaa.txt . 

就看您想放哪囉~   :)

NARs

  • 活潑的大學生
  • ***
  • 文章數: 227
    • 檢視個人資料
Re: 如何在crontab 執行shell script ?
« 回覆 #10 於: 2012-08-17 17:17 »
原來是有成功的,只是aaa.txt產生在root的目錄裡。
但是我試另一個shell script,但是在17:9時卻沒有執行,
但是如果直接執行是可以執行的
#sh /root/backup.sh


可是在crontab中卻沒有執行,請問各位大大是那裡錯了嗎?

backup.sh
代碼: [選擇]
export PATH=/bin:/sbin:/usr/bin:/usr/sbin

source /root/.bash_profile
sudo /opt/shop/bin/clean_pSB_db.pl \
--mysqldump-opts="--skip-lock-tables" --dbtype=employee --maxdays=3 \
--owmesh-dir=/opt/shop/etc/ \
--dumpdir=/var/log/BACKUP/employee



crontab 的設定:
代碼: [選擇]
9 17 * * *  /bin/sh /root/backup.sh



# pwd backup.sh
/root

# ls -l /root/backup.sh
-rwxr-xr-x 1 root root 287 Aug 17 17:06 /root/backup.sh

« 上次編輯: 2012-08-22 11:16 由 NARs »