顯示具有 指令 標籤的文章。 顯示所有文章
顯示具有 指令 標籤的文章。 顯示所有文章

2016年12月31日

Linux 快速取得時間(前後X日)

shell script 常用
date=$(date +%Y%m%d_%H%M)
date=$(date -d'-1 days' +%Y%m%d_%H%M)
date=$(date -d'+1 days' +%Y%m%d_%H%M)

#date
Sat Dec 31 17:01:30 CST 2016

#date +%F
2016-12-31

前一日
#date -d'-1 days' +%F
2016-12-30

明天
#date -d'+1 days' +%F
2017-01-01


reference:Linux 使用 date 計算時間(昨天、明天)

2014年12月22日

Linux 下計算資料夾大小

1.du -h | sort -h
列出目前資料夾下每個資料夾大小、並且排序

2.du -h --max-depth=1 | grep [0-9]G
只顯示第一層並大於1G的目錄

3.du 顯示磁盤空間的使用情況
預設為 KB 顯示
-B M
使用 MB 顯示
-B G
使用 GB 顯示

4.sort 排序
-n
當成數字排序
-r
反向排序
-k
指定用某欄位排序

5.head 取出前面幾行

6.tail 取出後面幾行


EX.du -h --max-depth=1 | grep [0-9]G | sort -rn | head -3
計算並取出前三大的資料夾

2014年3月7日

解決 windows 服務 http 無法啟用的問題

辦公室有個阿呆因為要處理80 port 被佔用的問題,google到 "net stop http"這個指令。結果佔用是解決了,可是要下"net start http"時卻變成服務已經停用,無法啟動的窘境。

控制台\服務\列表內完全找不到 http 這個服務

這時候只能用 sc 這個指令來修改了

sc 常用指令
sc config 設定服務的啟動參數
sc query 查詢服務的狀態
sc qc 查詢服務的設定資訊
sc start 啟動服務
sc stop 停用服務
sc pause 暫停服務
sc continue 繼續服務

所以可以下這個指令
c:\sc config http start= demand
來把被停用的服務改成手動

然後再下
c:\sc start http
來啟動http服務


reference:管理 Windows Server 服務

2013年12月31日

遠端重啟 windows 主機 and 服務

command 模式

重啟主機

net use \\遠端主機IP "密碼" /user:"帳號"
shutdown -r -m \\遠端主機IP -t 3 -f

停止/啟動服務

net use \\遠端主機IP "密碼" /user:"帳號"
sc \\遠端主機IP stop 服務名稱
sc \\遠端主機IP start 服務名稱


遠端使用 Sc.exe 和 Netsvc.exe 控制服務
http://support.microsoft.com/kb/166819/zh-tw

2013年9月7日

顯示 apache2 log 中,次數最多的前 n 個 IP

tail -n 1000 apache2/logs/www.log | cut -d' ' -f1 | sort | uniq -c | sort -nr | head -n10
  • tail -n 1000 apache2/logs/www.log 取出最後一千行
  • cut -d' ' -f1 以空格為區分取出第一部分,也可以用 awk '{print $1}'
  • sort 第一次排序
  • uniq -c 同樣的只顯示一個、加上 -c 是計數的意思
  • sort -nr 第二次排序,用數字並且反向排序(預設是文字型態排序)
  • head -n5 取出前5名

這時候如果搭配上 watch 指令的話就更方便了

watch -n 10 "tail -n 1000 apache2/logs/www.log | cut -d' ' -f1 | sort | uniq -c | sort -nr | head -n10"

每 10 秒自動抓一次 log 並分析

2013年8月26日

[Linux] 定時執行程式並查看結果

有時候寫 shell script 設好 crontab 後,會需要一直 ll 察看檔案有沒有確實建立,然後手就要一直按上 + enter

這時候可以用 watch 這隻程式來代替重複的動作

ex.
watch ls (每兩秒執行一次 ls ,不用 ll 的原因是因為不給用XD)
watch -n 5 ls (每五秒執行一次 ls)
watch -d ls (-d 會把不同處反白出來)
watch -n 1 -d "ps aux | grep mysql"



參考來源:

Linux 定時執行程式 並 監看結果 - watch

http://blog.longwin.com.tw/2009/03/linux-command-watch-monitor-by-seconds-2009/

2013年7月23日

[Linux] 取得目前機器IP


所有IP
/sbin/ifconfig | awk -F'[ :]+' '/Mask/{print $4}'

/sbin/ifconfig | awk -F'[ :]+' '/Bcast/{print $4}'





ADSL 撥號取得 IP
/sbin/ifconfig | awk -F'[ :]+' '/P-t-P/{print $4}'

2012年12月3日

CentOS 6.3 學習筆記 (17) - grep & 正規表示式

先用 alias 功能將 grep 用 grep --color=auto 取代
#vi ~/.bashrc
alias grep=’grep --color=auto’
#source ~/.bashrc