Simple life, Complicated mind

Monday, August 18, 2014

7Zip does not terminate exit close after finished completion

Try adding the -y switch (assume Yes on all queries)

Or try to redirect the output content to a file:

cmd /c "C:\\Program Files\\7-Zip\\7z.exe" x Src.7z -oD:\\tmp -r -y > out.txt

Note: sometimes the problems with calling processes is that you need to process / clear the output the produce. once their output buffer is full, they wait for the buffer to come free again.

Wednesday, August 13, 2014

C# start run external process program

        private void startSQL(string datadir)
            ProcessStartInfo sqlprocessinfo = new ProcessStartInfo()
                CreateNoWindow = true,
                UseShellExecute = false,
                WindowStyle = ProcessWindowStyle.Hidden,
                FileName = "bin\\mysqld.exe",
                Arguments = "--defaults-file=\"" + datadir + "-my.ini\" --datadir=\"" + datadir + "\""

                Process sqlprocess = Process.Start(sqlprocessinfo);

                if (sqlprocess.HasExited)
                    MessageBox.Show("Start MySQL failed.");

            catch (Exception ex)

Tuesday, August 12, 2014

rename move database on MySQL

show tables from old_db;

rename table old_db.tableName1 to new_db.tableName1, old_db.tableName2 to new_db.tableName2, old_db.tableName3 to new_db.tableName3;

SELECT CONCAT('RENAME TABLE ',table_schema,'.',table_name,
' TO ','new_schema.',table_name,';')
FROM information_schema.TABLES
WHERE table_schema LIKE 'old_schema';

Monday, August 11, 2014

Optimize nginx and PHP-FPM (max_children)

As Apache MaxClients, PHP-FPM needs to be optimized to avoid taking your server down.


Configuring nginx is pretty easy and is described everywhere. For this article, we'll be just focusing on workers.

You should set your worker_process to the number of CPU cores available :

~ # cat /proc/cpuinfo| grep processor
processor : 0
processor : 1
processor : 2
processor : 3
In your nginx.conf file :

worker_processes 4;
You should now edit the worker_connections variable. It's the number of simultaneous requests nginx can handle per worker. So a 1024 value allows me to handle 4096 requests. That should be more than enough. This value mostly depends on your bandwidth capacity and not on your server resources as these requests are extremely lightweight.


Here are the three main settings you should know about when configuring your pool (note that I'm using only one pool to handle all sites on the server but you can dispatch resources according to your needs) :

1. pm
The pm variable should be set to dynamic instead of static:

pm = dynamic
By doing so, PHP-FPM processes are started only when needed instead of staying in memory.

2. pm.max_children
pm.max_children is really the most important setting in your configuration. It should be calculated by doing :

pm.max_children = (total RAM - RAM used by other process) / (average amount of RAM used by a PHP process)
For example : My awesome RamNode KVM VPS has 1Gb RAM available and the system and MySQL could use up to 300Mb. I'm hosting Bolt and WordPress sites that consumes around 15Mb during page load. My pm.max_children would be set like this :

(1024 - 300) / (15) ~= 48
3. pm.servers
Other variables should be set to one third of pm.max_children. Like this :

pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 10
The last thing to take into account is the pm.max_requests settings. It's the number of requests a process server can handle before respawing. You should set this value to high values like 200.

pm.max_requests = 200
Don't forget to restart PHP-FPM after you've changed your settings.

Benchmark !

With these settings, I can now launch a basic ab performance test on a WordPress site without any cache enabled :

~ # ab -c 100 -n 3000
We can observe amazing results :

Concurrency Level: 100
Time taken for tests: 41.534 seconds
Complete requests: 3000
Failed requests: 0
Write errors: 0
Total transferred: 32421000 bytes
HTML transferred: 31668000 bytes
Requests per second: 72.23 [#/sec] (mean)
Time per request: 1384.452 [ms] (mean)
Time per request: 13.845 [ms] (mean, across all concurrent requests)
Transfer rate: 762.30 [Kbytes/sec] received
72.23 requests per second handled by PHP-FPM is pretty good for such a small server. Most of the time needed for the requests is by waiting for a slot to be opened by PHP-FPM because I launched two times more concurrent requests than available PHP-FPM processes. But no errors or timeout were detected.

Have fun with nginx and PHP-FPM !

Sunday, August 10, 2014

Window Sysinternals has announced a brand new tool Sysmon

Window Sysinternals has announced a brand new tool, Sysmon, a Windows service and driver which logs process creations, network connections, and changes to a file’s creation time.
The service is designed to help users identify malware, but can also be helpful when troubleshooting, or whenever you need to know more about what’s happening on a PC.
Sysmon has no GUI. To install the service, enter  Sysmon -i at an elevated command prompt, or  Sysmon -i -n  to monitor network connections as well (run Sysmon with no switches and it displays all your options).
If everything has worked correctly, the Sysinternals EULA will be displayed. Agree to it, then reboot to run your first test.
Once Windows has started again, launch the Event Viewer (Eventvwr.msc), and browse to the Windows System log on XP, Applications and Services Logs\Microsoft\Windows\Sysmon\Operational for Vista and later.

Sysmon’s recorded events are available from Event Viewer
You should now see multiple events listing Sysmon as a source, along with their date and time, giving you much more detail about what happened during your system boot.
Events with an ID of 1 list a process creation, including the time of launch, Process ID and GUID, file name, command line, user, hash, the parent process and more.
Events with an ID of 2 highlight an attempt to change a file creation date. The report lists the responsible process, the file it’s trying to change, the previous and new dates.
Events with an ID of 3 record network connections, again listing the source process (ID/ GUID/ file name/ user), source and destination IP addresses, host names, ports and port names. (For some reason the host names weren’t resolved in our first test, but this worked properly after we rebooted.)
Basic log management tasks can be carried out with the regular Event Viewer tools. You’re able to filter the log, display just the events you need, search for something important, disable logging when it’s no longer needed, save the events to a file, and more: right-click Sysmon\Operational to see what’s available.
You can also change Sysmon to use its default configuration (no network connection logging) by running  Sysmon -c –  , or uninstall it entirely with  Sysmon -u. The service and driver are removed immediately, and there’s no reboot required.
Sysmon is relatively limited in what it can monitor, but does have several advantages. It survives reboots, is launched very early in the boot process (handy if you’re looking to detect stealthy malware), and is invisible to regular users. Recording its findings as events also ensures you’ve plenty of ways to view and filter them, and overall it looks like another handy Sysinternals tool for power users.

《電學之父 - 法拉第的故事》讀書心得

《電學之父 - 法拉第的故事》讀書心得

上篇記錄讀後的感受, 這篇純粹記錄讀書心得。

第一章提到法拉第在學徒時期閱讀撒華滋寫的提昇悟性的方法, 對法拉第學習的方式有深遠影響 (p26)。這幾點也讓我獲良多, 重新體視學習方法和其帶來的影響:
  1. 需要做個人手記
  2. 需要持續上課
  3. 要有讀書的伴
  4. 要成立讀書會
  5. 學習仔細的觀察與精確的用字
意即, 要不斷消化吸收知識, 與人討論, 並能簡單易懂地說明給別人聽。如此一來, 才算是習得知識, 提昇自己的思考能力。這都不是短時間能有所成效, 需經年累月不斷練習, 養成習慣, 才會有成效。剛好和我自己的學習經驗有些呼應, 而能體會得較多一些。自己寫筆記、寫給別人看、能因人而異說明清楚, 這三者反映出的理解程度, 有很大一段落差, 可當作另外一篇獨立議題來討論。

在第二章和後面的一些描述, 提到法拉第早年受到別人的輕視, 讓法拉第查覺追求知識的人們, 並不如他原先想像般的也有更高的道德良知, 反而因爭取名利而表現相反的行為。法拉第這麼回顧 (p68):
教育的開始與結束, 都是謙卑。謙卑是深深地感到自己的不足。這種不足感, 不是跟人比較的, 而是我們內心深處真實的貧乏。學的第一步是知道自己的不足。... 所以, 真正的教育在於自我反省, 這種自覺, 就是教育的真諦。
回想中國古人的一些論點, 如《禮記‧學記》提到:
不論東西方, 追求一些思想到本源後, 似乎會回歸到同樣的觀念。

法拉第查覺受過高等知識 (p177), 不代表有基本的邏輯明辨是非, 讓他很心灰意冷, 而提倡應該將數學和物理納入基本教育裡。
有一位法官攻擊他道: 「科學也不過是一種狹窄的思想, 怎麼能知道靈異世界背後的博大精深? 」 法拉第沒有直接回答他, 法拉第認為這位高級分子的反應, 是高等教育出問題, 以致於不能培養出在基本問題上有分辨能力的知識分子。法拉第回覆道: 「這不是一個高級知識分子所該問的問題, 這提醒了我, 今日許多知識分子已喪失對基本問題的分辨能力, 如同未受教育的人一樣。」
首先我想到莊子說的「曲士不可以語於道者, 束於教也」, 不過我沒有完全理解法拉第的論述, 我有時也會懷疑若科學的基本邏輯有誤 (假設 -> 求證), 是否就無法解釋一些事? 或是我沒有深刻地理解科學精神? 愈深入理解科學, 愈覺得這世上不能用科學解釋的事比能解釋的事多太多了。許多日常生活運作已久的現象, 學得愈深, 發覺愈難仔細地描述內部到底如何運作, 只能模糊猜測概觀。從法拉第的言行舉止可知, 法拉第知道科學的這些限制和人類渺小的知識, 然而, 法拉第選擇從正面用科學的方式探究背後的成因。就像他做了十年實驗仍無法確定電磁的交互作用 (145)。

另外令我好奇的是, 法拉第對上帝有堅定的信仰, 並提到這種信仰和科學精神並無衝突。過去我覺得兩者有所衝突, 還在摸索法拉第的觀點中。科學的精神讓人很難輕易接受任何事物, 相反的, 一但累積足夠證據接受一項事物後, 也不會輕易改變過去的認知。在穩定的知識基石上, 緩慢但確實地朝對的方向往上前進。

《電學之父 - 法拉第的故事》讀後感

《電學之父 - 法拉第的故事》讀後感

偶然的機會下, 看到別人推薦這本, 順手買來讀讀。沒想到, 光是翻閱前言就覺得這本書相當有趣。為了能慢慢享受閱讀本書的樂趣, 我沒急著一口氣讀完, 而是偶而讀個一小段, 分成多天讀完。

了解法拉第純樸到不知變通的個性, 以及對待科學的真誠, 又燃起我心中微弱的火種。在第十一章裡 (p109), 法拉第遭到學界的人中傷, 他妻子撒拉這麼對他說:
我寧願你像一個孩子, 因單純而受到傷害; 也不要像一個小人, 因受傷害而處處對人提防。
讓我想到, 在過去不長的人生歲月裡, 偶而會因一些堅持而很感到很累, 幸好有剛好看到母親的言行, 重新拾起我認為「正確的事」。現在看到法拉第的情況, 更有感觸。

書中提及法拉第為科學樂此不彼的一生, 拒絕接受名利來衡量他在科學的發現, 而以在研究科學中已獲得大大的喜悅, 不斷地回絕各種職務邀約和酬勞。讓我多少能明白, 為何有些數學家、科學家, 會不願接受獎項。那種為科學發現而喜悅, 深怕單純之心被摻雜其它情緒的思維, 不是身處在名利世界的我們所能體會的, 反而會用「只是想拒絕獲獎來提昇名氣」這種說詞看待他們。

法拉第的戀愛觀和他妻子默默的支持也令人為之動容, 在為他笨拙的表現大笑之餘, 也深深感到認同。像是他對妻子描述的這段話 (p95):


在書本的末頁, 讀著附錄的年表。從頭檢視法拉第的一生, 讀到「一八六七年 逝世」這行, 我不禁鼻頭一酸, 為這位謙遜的大科學家回歸天國感到不捨。