Run custom script/program on linux hibernate/suspend/thaw/resume/lid-close/lid-open

For hibernate/suspend/resume:

Any executable file under /etc/pm/sleep.d will be executed one any of the above actions with a string inside the second parameter(which is args[1] in C or $1 in shell). This parameter may reads hibernate, thaw, suspend or resume depending on the operation the system is doing.

Just write a script or a program that does a certain operation according to the parameter recieved and throw it into the dictionary, mark it as executable(chmod) and it should do the job.

For lid close/lid open:
Solutions from here:
http://forums.fedoraforum.org/showthread.php?t=291877
and here:
https://wiki.archlinux.org/index.php/Acpid
looks pretty relevant. However, None of them works.

Still finding solutions.

2017/4/18 19:48

A conky profile that allows notes from file to be displayed on desktop

$hr
${color black}Notes:
    ${color black}Overview:
${color blue}${execpi 10 catpath/to/file|sed 's/^/        /g'|grep -v \*d}
    ${color black}Important:
${color red}${execpi 10 cat path/to/file|grep \*i|sed 's/^/        /g'|sed 's/*i//g'|sed 's/*p//g'|grep -v \*d}
    ${color black}In Progress:
${color green}${execpi 10 cat path/to/file|grep \*p|sed 's/^/        /g'|sed 's/*i//g'|sed 's/*p//g'|grep -v \*d}
    ${color black}Done:
${color grey}${execpi 10 cat path/to/file|grep \*d|sed 's/^/        /g'|sed 's/*i//g'|sed 's/*p//g'|sed 's/*d//g'} 
${color black}$hr
The above section should be placed on the `conky.text` section. Please replace all the `path/to/file` with the actual path of the file. Run command `cat path/to/file|grep -v \*d|tee path/to/file>/dev/null` to clean the done section. It could be deployed in crontab or any schedulers to do so. ==I tried ` ${execpi 360 cat ~/桌面/fast_note|grep -v \*d|tee ~/桌面/fast_note>/dev/null}` in the file but this will clear the whole note file for unknown reason.== Markdowns: use a `*i` sign to mark a line as important. The sign won't be included in the note. use a `*p` sign to mark a line as in progress. The sign won't be included in the note. use a `*d` sign to mark a line as done. The sign won't be included in the note. Since it's pretty easy and straightforward to add other markdowns to it, more customized functions could be added. 2017/4/10 18:58 This is my full config file version for reference:
-- vim: ts=4 sw=4 noet ai cindent syntax=lua
--[[
Conky, a system monitor, based on torsmo

Any original torsmo code is licensed under the BSD license

All code written since the fork of torsmo is licensed under the GPL

Please see COPYING for details

Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
Copyright (c) 2005-2012 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)
All rights reserved.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.  If not, see .
]]

conky.config = {
    alignment = 'top_right',
    background = false,
    border_width = 10,
    cpu_avg_samples = 2,
    default_color = 'black',
    default_outline_color = 'white',
    default_shade_color = 'white',
    draw_borders = false,
    draw_graph_borders = true,
    draw_outline = false,
    draw_shades = false,
    use_xft = true,
    font = 'DejaVu Sans Mono:bold:size=12',
    gap_x = 30,
    gap_y = 90,
    max_text_width = 5,
    maximum_width = 400,
    minimum_height = 5,
    minimum_width = 0,
    net_avg_samples = 2,
    no_buffers = true,
    out_to_console = false,
    out_to_stderr = false,
    extra_newline = true,
    own_window = true,
    own_window_class = 'Conky',
    own_window_type = 'dock',
    own_window_transparent = true,
    own_window_argb_visual = true,
    own_window_argb_value = 30000,
    own_window_hints = 'undecorated,below,sticky,skip_taskbar,skip_pager',
    stippled_borders = 0,
    update_interval = 1.0,
    uppercase = false,
    use_spacer = 'none',
    show_graph_scale = false,
    show_graph_range = false
}

conky.text = [[
$hr
${color black}$nodename - $sysname $kernel on $machine
$hr
${color black}Uptime:$color $uptime
${color black}Frequency (in MHz):$color $freq
${color black}Frequency (in GHz):$color $freq_g
${color black}RAM Usage:$color $mem/$memmax-$memperc% ${membar 4}
${color black}Swap Usage:$color $swap/$swapmax-$swapperc% ${swapbar 4}
${color black}CPU Usage:$color $cpu% ${cpubar 4}
${color black}Processes:$color $processes  ${color black}Running:$color $running_processes
$hr
${color black}File systems:
 /: $color${fs_used /}/${fs_size /} ${fs_bar 6 /}
${color black}Networking:
IP:${color black}${execpi 3600 myip}
Up:$color ${upspeed wlo1} ${color black} - Down:$color ${downspeed wlo1}
$hr
${color black}Name              PID   CPU%   MEM%
${color black} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}
${color black} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}
${color black} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}
${color black} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}
$hr
${color black}Notes:
    ${color black}Overview:
${color blue}${execpi 10 cat ~/桌面/fast_note|sed 's/^/        /g'|grep -v \*d}
    ${color black}Important:
${color red}${execpi 10 cat ~/桌面/fast_note|grep \*i|sed 's/^/        /g'|sed 's/*i//g'|sed 's/*p//g'|grep -v \*d}
    ${color black}In Progress:
${color green}${execpi 10 cat ~/桌面/fast_note|grep \*p|sed 's/^/        /g'|sed 's/*i//g'|sed 's/*p//g'|grep -v \*d}
    ${color black}Done:
${color grey}${execpi 10 cat ~/桌面/fast_note|grep \*d|sed 's/^/        /g'|sed 's/*i//g'|sed 's/*p//g'|sed 's/*d//g'}
${color black}$hr
]]

Solve the conky transparent issue under unity desktop

The unity desktop is wired and simply setting .conkyrc‘s transparent setting won’t make it work correctly.
After some google, I found these solution:


own_window = true,
own_window_class = ‘Conky’,
own_window_type = ‘dock’,
own_window_transparent = true,
own_window_argb_visual = true,
own_window_argb_value = 128,
own_window_hints = ‘undecorated,below,sticky,skip_taskbar,skip_pager’,

But this way it will somehow interrupt with the show-desktop shortcut so the shortcut will not work every-time. Still looking for solutions.

2017/4/9 21:56

Ways to share the same profile among chromium and chrome under Ubuntu

Chromium is the open source version of chrome while without a flash player. The point is, their profiles are totally compatible with each other.

The chromium browser storage its user-specific profile(config files, plugins, bookmarks, histories and so on) under ~/.config/chromium/.

The chrome browser storage its user-specific profile(config files, plugins, bookmarks, histories and so on) under ~/.config/google-chrome/(if using an beta or unstable version, the name might be something like ~/.config/google-chrome-beta/).

The point is, by simply moving around the contents, users can move around all their configurations and data in the browser.

Also, creating a symbolic link called chromium pointed to the google-chrome or the reverse allows the two browser to share the same config file.

2017/4/5

Difference between shallow copy (default) and deep copy of objects

When using “=” operator coping an object, the default or so-called shallow copy will be executed.

In java, shallow copy is like another name for the object(like the reference or pointer in C++), expect the basic data types, both objects at each side of “=” operator will be pointed to the same piece of memory.

In C++, shallow copy will cause a “=” operation from all the members of the first objects to the members in the second object, including those pointers. Thus deleting the second object might cause the destructor to release the pointers in the first object and further more cause troubles.

For deep copy, it is actually a function or method that allows developer themselves to define what to do during a copy.

For Java, a clone method from class Object can be override for cloning an object: Object clone(); To call it, a new instance shall be returned.

For C++, a constructor will the parameter of a reference of the class is used for the copy constructor:


class T{
public:
T(T &t);
};

Use T tCopy(tOriginal); to deep copy an object.

2017/3/20

使用脚本自动更新host实现不用vpn,不用服务器的翻墙。。。

(Ignore this if you can’t read it coz you won’t need it anyways ;p)

(这篇只为某些人打算省事而写。。。该host可靠性好步骤少。。。可以少踩点坑。。)

恩。。。鉴于看不懂中文的大概也不需要这个,需要这个的也不想看英文,这篇用中文写好了。。。

总之该方法只能访问下谷歌油管之类的国外网站,在各种vpn被封的场景下很好用。

那么现在就正式开始吧。。。这样的玩意请在终端里输入

window平台:

某github开发者很贴心的吧工具都写好了。。so。。。

https://github.com/HostsTools/Windows

Unix平台(mac/linux。。之类):

MAC似乎有人写了:https://github.com/HostsTools/OSX

linux的话靠自己吧:

1.安装git。 具体方式取决于发行版,有包管理器的话软件包叫git-core。(像什么sudo apt-get install git-core啦。。yum install gitcore啦。。。)没有的话请自己编译。

2.随便找个github上还有人维护的hosts文件,再随便找个目录,pull下来。。:
git pull https://github.com/racaljk/hosts.git

3.写个脚本。。。:
nano hosts_updater.sh
里面输入:


cd /你/现在/的路径/hosts
git pull
cat /你/现在/的路径/hosts/hosts | sudo tee /etc/hosts ##这句话没有写错。。。
/etc/init.d/networking restart

ctrl+x然后回车保存退出

4.给上脚本权限并且执行一次试试:

chmod +x hosts_updater.sh

./hosts_updater.sh

过一会看看能不能上谷歌什么的。。。可能需要重启。。。(目测仅debian系可用,红帽党们估计也用不到吧。。)

5.安装crontab。。。(像什么sudo apt-get install crontab啦。。yum install crontab啦。。。或者自己编译)

6.登录到root用户中:

su root

7.在crontab里面注册指令让它定时执行。。。:

crontab -e

如果要你选择编辑器,选nano吧。。。

在最后一行加上 0 0 * * * /到你脚本的路径/hosts_updater.sh

一样保存退出。。。完事

2017/3/19 22:06