2011年9月30日星期五

linux中的cron服务

[root@huage ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 1 * * * root run-parts /etc/cron.daily
03 2 * * 0 root run-parts /etc/cron.weekly
04 3 1 * * root run-parts /etc/cron.monthly

       The first four lines are variables used to configure the environment in which the cron tasks are run. The SHELL variable tells the system which shell environment to use (in this example the bash shell), while the PATH variable defines the path used to execute commands. The output of the cron tasks are emailed to the username defined with the MAILTO variable. If the MAILTO variable is defined as an empty string (MAILTO=""), email is not sent. The HOME variable can be used to set the home directory to use when executing commands or scripts.
 
      As shown in the /etc/crontab file, the run-parts script executes the scripts in the /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/ directories on an hourly, daily, weekly, or monthly basis respectively. The files in these directories should be shell scripts.

      If a cron task is required to be executed on a schedule other than hourly, daily, weekly, or monthly, it can be added to the /etc/cron.d/ directory. All files in this directory use the same syntax as /etc/crontab.

      Users other than root can configure cron tasks by using the crontab utility. All user-defined crontabs are stored in the /var/spool/cron/ directory and are executed using the usernames of the users that created them. To create a crontab as a user, login as that user and type the command crontab -e to edit the user's crontab using the editor specified by the VISUAL or EDITOR environment variable. The file uses the same format as /etc/crontab. When the changes to the crontab are saved, the crontab is stored according to username and written to the file /var/spool/cron/username.

      The cron daemon checks the /etc/crontab file, the /etc/cron.d/ directory, and the /var/spool/cron/ directory every minute for any changes. If any changes are found, they are loaded into memory. Thus, the daemon does not need to be restarted if a crontab file is changed.

Controlling Access to Cron
      The /etc/cron.allow and /etc/cron.deny files are used to restrict access to cron. The format of both access control files is one username on each line. Whitespace is not permitted in either file. The cron daemon (crond) does not have to be restarted if the access control files are modified. The access control files are read each time a user tries to add or delete a cron task.

      The root user can always use cron, regardless of the usernames listed in the access control files.

      If the file cron.allow exists, only users listed in it are allowed to use cron, and the cron.deny file is ignored.

      If cron.allow does not exist, users listed in cron.deny are not allowed to use cron.

2011年9月28日星期三

经典实用的Shell命令

1.[root@desktop huage]# yum provides */filename
   查找filename是由哪个RPM包提供

2.[root@desktop huage]# locate filename
   快速查找名为filename的文件

3.[root@desktop huage]#rename .JPG .jpg *.JPG
   批量修改文件的后缀名(适用于redhat分支)

4.[root@desktop huage]#ls |grep jpg|awk -F'.' '{print $1}'|xargs -i mv {}.jpg {}.JPG
   批量修改文件的后缀名(两组单引号之间有个空格)

   [root@desktop huage]# ls |grep jpg|awk -F'.' '{print $1}'|xargs -i -t mv {}.jpg {}.JPG
   批量修改文件的后缀名(-t:详细显示xargs命令后面的操作)

5.[root@Server huage]# ls |grep -v  rpm$ |xargs -i -t rm -rf {}
   删除当前目录下所有后缀不是rpm的文件(包括目录文件)

6.[root@Server huage]# ls -F |egrep -v "rpm$|/$" |xargs -i -t rm -rf {}
   删除当前目录下所有后缀不是rpm的文件(不包括目录文件)

7.[root@Server huage]# find . -type f -print |grep -v rpm$ |xargs -i -t rm -rf {}
   删除当前目录及其子目录下所有后缀不是rpm的文件

8.[root@Server huage]# ls -lSh
   用易于人类读写的单位按从大到小的顺序排序(主要用于文件的排序,不统计目录的具体大小)

9.[root@Server huage]# ls -lShr
   用易于人类读写的单位按从小到大的顺序排序(主要用于文件的排序,不统计目录的具体大小)

10.[root@Server huage]# ls |xargs du -sh |sort -h
     用易于人类读写的单位按从小到大的顺序排序(包括目录)

11.[huage@fedora ~]$ script
     记录接下来的所有终端输出

12.[huage@fedora ~]$ script huage
     记录所有终端输出到文件huage

13.[huage@fedora ~]$ time shell-command
     对shell-command命令计时,time后直接跟shell命令即可

14.[root@Server tmp]# find . -type f -exec chmod 644 {} \;
     或者:
     [root@Server tmp]# find . -type f -exec chmod 644 {} +
     将当前目录及其子目录下的所有文件的权限设为644

15.[root@Server tmp]# find . -type d -exec chmod 755 {} \;
     或者:
     [root@Server tmp]# find . -type d -exec chmod 755 {} +
     将当前目录及其子目录的权限设为755

16.[root@fedora huage]# find . -type f |xargs chmod 644
     将当前目录及其子目录下的所有文件的权限设为644(如果目录名中包含空格,则chmod对该目录无效)

17.[root@fedora huage]# find . -type d |xargs chmod 755
     将当前目录及其子目录的权限设为755(如果目录名中包含空格,则chmod对该目录无效)

注:在18和19例中,如果文件(包括目录文件)名中包含空格,则chmod命令对该文件无效,实际上是xargs命令无法正确处理文件名中包含空格的文件和目录文件。

28.[root@fedora huage]# python -m SimpleHTTPServer
     通过python的SimpleHTTPServer模块来快速搭建一个HTTP Server,之后可以通过 
     http://server-ip:8000或者http://localhost:8000来访问

19.[root@fedora huage]#
mail admin@huage.com -s "Backuping HUA is successfully done" < /root/huage/backup
用一条命令来发送邮件给admin@huage.com,且主题为"Backuping HUA is successfully done",邮件正文内容为"/root/huage/backup"。

20.[root@fedora huage]# mail huage@gmail.com -s "Backuping ADG is successfully done" < /root/hauge/backup-adg -- -f admin@google.com -F "The Admin of Google"

      发送一封主题为"Backuping ADG is successfully done"、邮件正文为"/root/hauge/backup-adg"的内容、发件人为admin@google.com且发件人显示名称为"The Admin of Google"的邮件给huage@gmail.com

21.[root@fedora huage]# du -sh /home/huage/public_html |mail -s "Moving Successful" huage@gmail.com -- -f admin@google.com -F "The Admin of Google"

      发送一封以"Moving Successful"为主题、以"du -sh /home/huage/public_html"输出内容为邮件正文、以admin@google.com为发件人且发件人的显示名称为"The Admin of Google"的邮件给huage@gmail.com

自动备份网站根目录

Auto backup up the root directory of website(自动备份网站根目录):

root@huage [~/huage]# cat /usr/local/bin/hua-backup
#!/bin/bash
HUA=/home/huage/
NAME=hua-`date +%Y%m%d`.tgz
cd $HUA
rm -rf public_html/bmz_cache/*
echo "Cache is cleaned."
tar -zcvf $NAME public_html
echo "The backuping process is successfully done."
mail admin@huage.com -s "Backuping HUA is successfully done" < /root/huage/backup
echo "Everything is successful completion."
exit 0

然后使用"crontab -e"命令编辑root用户的crontab文件,加入如下一行:
"0 0 * * 0 /usr/local/bin/hua-backup >/dev/null 2>&1"
,然后保存文件到"/var/spool/cron/root"就行了。

注:虽然hua-backup脚本放在系统PATH路径中,但/etc/crontab单独定义了"PATH=/sbin:/bin:/usr/sbin:/usr/bin",所以此处的脚步文件仍然要写完整路径,否则计划不能成功执行。

2011年9月27日星期二

你们在哪里

西摩.西蒙斯:多年以后 他们会问我们 
                   他们占领这星球的时候 你们在哪里?
                   我们回答说
                   我们只是袖手旁观
                                              ——变形金刚3 

海盗湾宣言

      请记住,我们是关不掉的!各位,你们可能已经听说了,那些家伙又一次试图关闭我们。他们不会成功的。我们的带宽提供商是好人,所以我们决定把网站搬个地方,不让他们为难。海盗湾是不会沉没的。只要我们愿意,它将永远航行在互联网上。请记住这一点。自从2003年海盗湾上线以来,他们从我们这里得到的,只是一次又一次的失败。

      只要这个世界还有一个热爱自由的人存在,我们就不会下线。

      那些与互联网为敌的人们,你们是不会成功的。(多么激动人心的宣言啊!
    
                                                                                                                ——海盗湾
  

我们被判有罪

我们被判有罪,这真是相当令人惊奇的事,他们也可以罚款我们一亿美元,这无关紧要,因为我们不能也不会做出赔偿。

                          ——海盗湾的四名创始人之一彼得·桑德·科尔米索皮(Peter Sunde Kolmisoppi)

2011年9月26日星期一

Firefox经典实用扩展

1.Adblock Plus
有了Adblock Plus,广告已成往事

2.AutoProxy
翻墙必备,翻墙于无形,撞墙已成往事

3.Domain Hammer SEO Analysis
简洁实用的SEO工具

4.DownThemAll!
下载必备,支持断点续传,同时也能限速

5.Firebug
给力的Web开发工具,也可以用来查看访问一个网址所请求的所有HTTP Request

6.FireGestures
鼠标手势,好网民必备

7.Flagfox
显示当前网站服务器所在的地理位置和域名所属区域

8.Forecastfox
一个非常好的显示天气的扩展

9.IE Tab 2
使Firefox也能使用IE核心浏览一些必需用IE的网站(如网银)

10.LastPass
最好的密码管理工具,没有之一

11.ReminderFox
好用的事件提醒扩展

12.Tab Mix Plus(Firefox Add-ons中已经搜索不到了)
增强标签浏览体验

13.Tamper Data
追踪所有HTTP Request

14.Xmarks
最好的书签同步扩展,没有之一