script 經 cron 執行異常但直接執行ok ? - Linux

By Eden
at 2017-03-04T03:42
at 2017-03-04T03:42
Table of Contents
script 功能:
可移除 qnap NAS 的 /share/tmp 中, mtime 超過 7 天的檔案。
利用 cron 定時執行。
(補充一下,相關工具如find之類全是以 ipkg 安裝之外部工具)
由於怕誤刪(rm -rf / 之類....),所以我另創帳號 sysbot_tmp_cleaner 來執行
刪除動作。/share/tmp也已經允許 sysbot_tmp_cleaner 讀寫,也實際 rmdir 過了。
設定 cron 後,的確有執行,但 find 卻找不到任何檔案。
而直接執行 script 則是可以找到檔案。
請問可能原因為何呢?
script 在最下方,或是:
http://pastebin.com/aPeNv2zp
script 透過 cron 執行後,輸出至 log 的內容為:
------------------------
Clear time: 2017-03-04 03:15:01
sysbot_tmp_cleaner
sysbot_tmp_cleaner
Format is: mtime(yyyy-mm-dd) \t size(KB) \t username \t filename \n , dir name end by '/'
Clear finish: 2017-03-04 03:15:01
-----------------------
而直接執行(以admin)則是在中間的 sysbot_tmp_cleaner 前充滿檔案路徑。
另外一個奇怪的問題。為何直接以 sysbot_tmp_cleaner 執行
find /share/tmp ......
找不到檔案,但加了/後就可以?
find /share/tmp/ ......
(update: 想起來了,/share/tmp 是 soft link , 直接以 ls /share/tmp 也只是顯示
連結本身,要 ls /share/tmp/ 。大概是這原因吧!)
--------- 以下 script --------------
#!/bin/sh
# This script clear too old files in tmp folder . Execute by cron.
# Run in every day 06:00
# echo '00 6 * * * /share/homes/admin/bin/clear_old_file.sh' > /etc/config/crontab
tmp_dir="/share/tmp/"
log_path="/share/system/log"
log="${log_path}/clear_nas_tmp.log"
lifetime_day=8
operator="sysbot_tmp_cleaner"
#------------------------------------
time_start=$(date "+%F %T")
# Add "/" at $tmp_dir end position, must be it or sysbot_tmp_cleaner can't get list.
tmp_dir=$(echo "$tmp_dir" |sed 's/\/*$//')"/"
# find and delete folders by operator
# Don't use rm , it will cause error "Argument list too long".
# dir first for fast remove.
# Info format is: mtime(yyyy-mm-dd) \t size(KB) \t username \t filename \n , dir name end by '/'
dirs_info=$(coreutils-su "$operator" -s "/bin/sh" -c "
findutils-find "$tmp_dir" ! -path "$tmp_dir" -type d -mtime +${lifetime_day} ! -user admin -printf '%TY-%Tm-%Td\t%k\t%u\t%p/\n' ;whoami")
# find and delete files by operator
files_info=$(coreutils-su "$operator" -s "/bin/sh" -c "
findutils-find "$tmp_dir" ! -path "$tmp_dir" -type f -mtime +${lifetime_day} ! -user admin -printf '%TY-%Tm-%Td\t%k\t%u\t%p\n' ;whoami")
num=$(( $(wc -l <<< "$dirs_info") + $(wc -l <<< "$files_info") ))
time_end=$(date "+%F %T")
if [ "$num" -gt "0" ] ; then
# record remove files
# check if log.gz exist then unzip
[ -f "${log}".gz ] && gzip -d "${log}".gz
echo "Clear time: $time_start" >> "${log}"
echo "$dirs_info" >> "${log}"
echo "$files_info" >> "${log}"
echo "Format is: mtime(yyyy-mm-dd) \t size(KB) \t username \t filename \n , dir name end by '/'" >> "${log}"
echo "Clear finish: $time_end" >> "${log}"
gzip "${log}"
# move too old log file when > 50 MB, you can change to use logrotate
log_size=$(du -ms "${log}".gz |cut -f 1 )
if [ "$log_size" -gt "50" ] ; then
mv "${log}".gz ${log_path}/clear_nas_tmp.old.log.gz
fi
fi
--
可移除 qnap NAS 的 /share/tmp 中, mtime 超過 7 天的檔案。
利用 cron 定時執行。
(補充一下,相關工具如find之類全是以 ipkg 安裝之外部工具)
由於怕誤刪(rm -rf / 之類....),所以我另創帳號 sysbot_tmp_cleaner 來執行
刪除動作。/share/tmp也已經允許 sysbot_tmp_cleaner 讀寫,也實際 rmdir 過了。
設定 cron 後,的確有執行,但 find 卻找不到任何檔案。
而直接執行 script 則是可以找到檔案。
請問可能原因為何呢?
script 在最下方,或是:
http://pastebin.com/aPeNv2zp
script 透過 cron 執行後,輸出至 log 的內容為:
------------------------
Clear time: 2017-03-04 03:15:01
sysbot_tmp_cleaner
sysbot_tmp_cleaner
Format is: mtime(yyyy-mm-dd) \t size(KB) \t username \t filename \n , dir name end by '/'
Clear finish: 2017-03-04 03:15:01
-----------------------
而直接執行(以admin)則是在中間的 sysbot_tmp_cleaner 前充滿檔案路徑。
另外一個奇怪的問題。為何直接以 sysbot_tmp_cleaner 執行
find /share/tmp ......
找不到檔案,但加了/後就可以?
find /share/tmp/ ......
(update: 想起來了,/share/tmp 是 soft link , 直接以 ls /share/tmp 也只是顯示
連結本身,要 ls /share/tmp/ 。大概是這原因吧!)
--------- 以下 script --------------
#!/bin/sh
# This script clear too old files in tmp folder . Execute by cron.
# Run in every day 06:00
# echo '00 6 * * * /share/homes/admin/bin/clear_old_file.sh' > /etc/config/crontab
tmp_dir="/share/tmp/"
log_path="/share/system/log"
log="${log_path}/clear_nas_tmp.log"
lifetime_day=8
operator="sysbot_tmp_cleaner"
#------------------------------------
time_start=$(date "+%F %T")
# Add "/" at $tmp_dir end position, must be it or sysbot_tmp_cleaner can't get list.
tmp_dir=$(echo "$tmp_dir" |sed 's/\/*$//')"/"
# find and delete folders by operator
# Don't use rm , it will cause error "Argument list too long".
# dir first for fast remove.
# Info format is: mtime(yyyy-mm-dd) \t size(KB) \t username \t filename \n , dir name end by '/'
dirs_info=$(coreutils-su "$operator" -s "/bin/sh" -c "
findutils-find "$tmp_dir" ! -path "$tmp_dir" -type d -mtime +${lifetime_day} ! -user admin -printf '%TY-%Tm-%Td\t%k\t%u\t%p/\n' ;whoami")
# find and delete files by operator
files_info=$(coreutils-su "$operator" -s "/bin/sh" -c "
findutils-find "$tmp_dir" ! -path "$tmp_dir" -type f -mtime +${lifetime_day} ! -user admin -printf '%TY-%Tm-%Td\t%k\t%u\t%p\n' ;whoami")
num=$(( $(wc -l <<< "$dirs_info") + $(wc -l <<< "$files_info") ))
time_end=$(date "+%F %T")
if [ "$num" -gt "0" ] ; then
# record remove files
# check if log.gz exist then unzip
[ -f "${log}".gz ] && gzip -d "${log}".gz
echo "Clear time: $time_start" >> "${log}"
echo "$dirs_info" >> "${log}"
echo "$files_info" >> "${log}"
echo "Format is: mtime(yyyy-mm-dd) \t size(KB) \t username \t filename \n , dir name end by '/'" >> "${log}"
echo "Clear finish: $time_end" >> "${log}"
gzip "${log}"
# move too old log file when > 50 MB, you can change to use logrotate
log_size=$(du -ms "${log}".gz |cut -f 1 )
if [ "$log_size" -gt "50" ] ; then
mv "${log}".gz ${log_path}/clear_nas_tmp.old.log.gz
fi
fi
--
Tags:
Linux
All Comments

By Mason
at 2017-03-08T22:54
at 2017-03-08T22:54

By Carolina Franco
at 2017-03-13T00:47
at 2017-03-13T00:47

By Brianna
at 2017-03-13T14:52
at 2017-03-13T14:52

By Donna
at 2017-03-14T06:36
at 2017-03-14T06:36

By Isla
at 2017-03-17T18:26
at 2017-03-17T18:26

By Steve
at 2017-03-19T09:14
at 2017-03-19T09:14

By Margaret
at 2017-03-21T06:04
at 2017-03-21T06:04

By Lucy
at 2017-03-25T19:46
at 2017-03-25T19:46

By Rebecca
at 2017-03-26T17:52
at 2017-03-26T17:52
Related Posts
sed 用法請教

By Todd Johnson
at 2017-02-28T09:47
at 2017-02-28T09:47
無法 hibernate

By Ivy
at 2017-02-27T13:19
at 2017-02-27T13:19
Gitlab server 架設問題求救

By Elma
at 2017-02-25T18:03
at 2017-02-25T18:03
重灌之後,pcmanx ssh連線亂碼

By Leila
at 2017-02-23T23:32
at 2017-02-23T23:32
連不上raspbian的ssh

By Olive
at 2017-02-23T18:53
at 2017-02-23T18:53