需要整理
通过update-rc.d来管理Ubuntu系统的自动启动程序
转载, 并记下我的使用心得。
Linux services can be started, stopped and reloaded with the use of scripts stocked in /etc/init.d/. However, during start up or when changing runlevel, those scripts are searched in /etc/rcX.d/ where X is the runlevel number. This tutorial will explain how one can activate, deactivate or modify a service start up. When installing a new service under debian, the default is to enable it. So for instance, if you just installed apache2 package, after you installed it, apache service will be started and so will it be upon the next reboots. If you do not use apache all the time, you might want to disable this service from starting up upon boot up and simply start it manually when you actually need it by running this command:
1
| |
You could either disable this service on boot up by removing any symbolic links in /etc/rcX.d/SYYapache2 or by using update-rc.d. The advantage of using update-rc.d is that it will take care of removing/adding any required links to /etc/init.d automatically. Taking apache2 as an example, let’s examine how /etc/rcX.d is looking like:
1 2 3 4 5 6 7 8 | |
As you can see, for runlevels 0, 1 and 6 there is a K at the beginning of the link, for runlevels 2, 3, 4 and 5, there is a S. Those two letters stands for Kill and Start. On Debian and Ubuntu, runlevels 2, 3, 4 and 5 are multi-users runlevels. Runlevel 0 is Halt. Runlevel 1 is single user mode Runlevel 6 is reboot
1. Removing A Service
If you want to totally disable apache2 service by hand, you would need to delete every single link in /etc/rcX.d/. Using update-rc.d it is as simple as:
1
| |
The use of -f is to force the removal of the symlinks even if there is still /etc/init.d/apache2.
Note: This command will only disable the service until next time the service is upgraded. If you want to make sure the service won't be re-enabled upon upgrade, you should also type the following:
# update-rc.d apache2 stop 80 0 1 2 3 4 5 6 .
2. Adding A Service
2.1. Default Priorities
Now, if you want to re-add this service to be started on boot up, you can simply use:
1 2 3 4 5 6 7 8 9 | |
2.2. Custom Priorities
But as you can see, the default value is 20 which is pretty different than 91 … a S20 link is started before a S91 and and K91 is kill before K20. To force apache2 to be started with priorities 91 for both Start and Kill, we need to use the following command:
1 2 3 4 5 6 7 8 9 | |
2.3. Different Priorities For Start And Kill
Alternatively, if you want to set different priorities for Start than for Kill, let say Start with 20 and Kill with 80, you will need to run:
1 2 3 4 5 6 7 8 9 | |
3. Specifying Custom Runlevels
Finally, if you only want to Start and Kill on specific runlevels, like for instance starting apache with priority 20 on runlevels 2, 3, 4 and 5 and Kill with priority 80 on runlevels 0, 1 and 6:
1 2 3 4 5 6 7 8 9 | |
Or, to start with priority 20 for runlevel 2, 3 and 4 and priority 30 for runlevel 5 and kill with priority 80 for runlevel 0, 1 and 6:
1 2 3 4 5 6 7 8 9 | |
我的总结
通过 update-rc.d 来管理Linux下开机自动运行,的确很方便,但是我在实际部署实践中,还是遇到了一些问题,导致开机后没有正常自动启动,但是手工通过service xxx start可以启动。我经过排查发现,原因是在Linux系统启动中,在执行/etc/init.d/中的脚本时,此时系统有可能没有加载好系统中的PATH变量,所以需要在init.d脚本中手工指定,对于使用Ruby脚本写的程序,需要GEM_HOME\GEM_PATH等环境变量,我这里是用RVM来管理Ruby的,这是我使用的:
1 2 3 4 5 6 7 8 | |
另外还有一些其它的变量(如果你的启动程序需要),也需要手工指定,如在Ruby 1.9中,就有可能需要指定,比如说unicorn启动脚本:
1
| |
最后附上一个常用的init.d启动脚本的样本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
Resources:
Use Rubyencoder Encrypt Ruby Code
前些时候研究了一下ruby encoder的加密技术应用,在这里记录一下它的使用过程。
Ruby Encoder将ruby代码加密成不易阅读的格式,然后通过它提供的加载类型来载入ruby代码,从而达到加密的目的。它是一个付费软件,一个许可证要159美金,提供试用版,可以试用一个星期,支持Linux \ FreeBSD \ Mac OS X \ Windows系统。
首先在Ruby Encoder上注册一个帐号,登录进去后,我这里选择下载Mac OS X版的,RubyEncoder Trial for Mac OS X (RubyEncoder_1.3_Trial.dmg),下载后,将dmg中的程序拖到applications目录,就算安装好了。
1 2 3 4 | |
第一次使用的话,需要它的官方网站给你提供一个许可证文件(文件名为encode.lic),进行Ruby Encoder的bin目录,执行其中的rubyencoder文件, 它会输出一些它的软件声明信息,一直回车后几次后,阅读完许可声明,它还要求你输入’I AGREE’后,然后输出一个注册码(Reg Key),要求你登录到它的网站中,在个人帐户里,在页面最下面的Available Licenses中填入注册码,然后下载它提供的encode.lic文件,放在Ruby Encoder的bin目录中,现在就可以使用Ruby Encoder了。
直接执行一下rubyencoder命令,可以先了解一下它的用法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
像我给公司做的一个小项目,我只需要加密rails目录中的app目录就足够了,官方也建议只加密app目录就行了,可能是为了避免一些加密问题?
1
| |
这样就完成了app目录中的rb代码加密,同时它还备份了原来的rb文件,是同名的后缀为bak的文件,在部署中应该将.bak文件都删除掉。还有一件事情要做,需要把Ruby Encoder的相当于解密的加载器文件复制到app目录中,也就是加密代码的上一级目录里面来,这样项目可以正常执行了。
1 2 3 | |
下面是我咨询的关于Ruby Encoder的许可证相关的几个问题的官方回复邮件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
Create Custom Tags in Smarty 3
接着上一篇,还是来说说PHP模板引擎Smarty。PHP的众多CMS框架,如DEDE CMS,对于前端展示都是有一套自己开发的标签,用于显示管理后台维护的数据。在DEDECMS中,如下的代码
1 2 3 4 5 6 | |
就可以显示最近type id为1的10条新闻。在Smarty中创建属于自己的标签是很容易的,我之前在网络上搜索的关于创建smarty标签的内容大多数都是基于Smarty2的,我基于Smarty3中的plugins目录的代码了解到在Smarty3中创建自定义标签更为简单直观。
在smarty程序包的plugins目录中,可以看到有block \ function \ modifier 等几种前缀的php文件。像block前缀的php文件可以创建闭合的标签,就如上文提到的那个dede cms新闻标签的例子。现在我正是要创建这样类型的标签。
我结合CodeIgniter来说明,我现在创建一个用户列表的标签,可以显示最近注册的用户。 在smarty的plugins目录中创建的文件会自动被smarty加载而识别的,文件名和其中的function 名称需要特定的约定好的格式。 我现在想创建一个users标签,还有一个limit参数,用来显示取多少条数据
1 2 3 4 5 6 | |
那么文件名就应该指定为block.users.php,然后function应该命名为smarty_block_users:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
这个function中有四个参数,$params参数存储的是标签中的属性信息,如上面的limit。$content是标签中间的内容,$smarty是smarty的实例对象,$repeat用于指定block是否重复执行。 解释完了function的参数,我想其中的执行原理应该很容易看明白了吧。
Use Smarty Template in CodeIgniter
CodeIgniter是一个不错的PHP开发框架,很合我的口味,我做的一些PHP项目就是用的它。
Smarty是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起的PHP代码逻辑分离。
一般来说使用原生PHP作为视图会比使用模板引擎高效,一些简单的项目或者个人开发者可以直接使用PHP,如果是大一点的项目,再加上要和美工配合,使用模板引擎会是分工明确,合作高效一些。
在CodeIgniter使用Smarty模板引擎一点也不复杂,这是因为CodeIgniter提供了创建类库的方便方法。
我这里使用的CodeIgniter和Smarty都是最新版本,2.1.0和3.1.7
1: 将Smarty包下载后,解压后,放入CodeIgniter项目中的application/libraries目录.
1 2 3 4 | |
2: 在application/libraries目录中创建Smarty.php文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
根据代码中的smarty目录配置,需要在application/views中建立templates_c\templates\cache三个目录. 这就算创建好了。
3: 使用方法。 控制器中的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
模板代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Customizing Rails Generator Templates
Rails framework and twitter bootstrap are good for your startup project, build-in generators generate source code by its way. It is a good starting point.
But I often have to change default generted source code:
- Add encoding: utf-8 into ruby file header, for Ruby 1.9.2 encoding.
- Change scaffold view files to apply bootstrap css.
- Other things you want to override default templates.
Rails provides the mechanism to custom generator behavior.
I have already got it, and source code is on github, follow README for usage.
How to Update a Forked Repository From Original Repository at Github?
If you use github, you can follow this.
I use huachlee/ruby-china repository for example.
At huacnlee/ruby-china repository homepage, clicked “Fork” button, then after serval minutes, you will have you own forked repository copy.Then you clone to local.
1
| |
Then you commit your changes, and push to it.When you found the original repository has some updated feature,you can do as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
That is it.
Resources:
在Rails3中使用ajax
今天写代码的时候突然发现 我之前在Rails3中使用ajax的方式是不太正确的,我走了弯路。
我之前在Rails3项目中使用ajax是这样的:
如果是直接发送一个ajax请求,我就给标签加上:remote => true。如果我在此基础上还要做其它的操作,如ajax表单提交前要对表单字段验证,我之前以为那样UJS就不能搞定了,需要我自己写代码来处理。然后我就使用jquery中的相关ajax操作来处理。
今天我才了解到UJS的AJAX操作已经为我们提供了6个自定义事件方法:
1 2 3 4 5 6 | |
所以对于上面我说的那个事例,如果要对一个ajax表单在提交前作表单验证或者其它的一些检查,只需要写提交前的检查代码,然后通过事件绑定的方式,将余下的表单ajax提交让UJS来完成,这是多么地简单啊,我以前真是做了好多无用功。
1 2 3 4 5 6 7 | |
参考资料
Using Unobtrusive JavaScript and AJAX with Rails 3 Rails 3 Remote Links and Forms Part 2: Data-type (with jQuery)
mongodb的可视化客户端工具:MongoHub
近期iOS学习点
虽然我业余在学iOS,但是感觉进步缓慢。为了使我的iOS开发的学习效率高一点,我决定先列一个简要的提纲,明确当前一个阶段的学习计划。
iOS体系,了解清楚UI组件的体系
Objective-C中的基本数据结构使用
iOS中的基本的网络编程,主要是iOS客户端方的,如json,http交互等。
先就这么多,每天要进步一点.
