惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
D
Docker
J
Java Code Geeks
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
腾讯CDC
罗磊的独立博客
U
Unit 42
爱范儿
爱范儿
Vercel News
Vercel News
MyScale Blog
MyScale Blog

博客园 - 走到天亮

设计模式之“适配器模式” 设计模式之“门面模式” 设计模式之“抽象工厂模式” 设计模式之“单例模式” 设计模式之“代理模式” 设计模式之“策略模式” 《C# to IL》第三章 选择和循环 《C# to IL》第二章 IL基础 《C# to IL》第一章 IL入门 淘宝下单高并发解决方案(转载) java linux 配置环境 Spring Aop之(二)--Aop 切面声明和通知 Spring aop Spring RegexpMethodPointcutAdvisor和NameMatchMethodPointcutAdvisor Spring BeanNameAutoProxyCreator 与 ProxyFactoryBean CentOS的IP配置专题 Spring Bean属性绑定Bean返回值 【阿里的感悟】质量该如何做? .(转载) Ubuntu开机自动启动script(2)
Ubuntu开机自动启动Script
走到天亮 · 2011-04-28 · via 博客园 - 走到天亮

实现目标:

(1).在Ubuntu启动时,自动运行位于普通用户test1根目录下的脚本程序start.py,该程序会在每次执行时自动向本地日志文件追加一条记录,源码如下:

from datetime import datetime
now
=datetime.now()
f
=open('test1.log','a')
f.write(
'%s '%now)
f.close()

Ubuntu在启动时,会自动执行/etc/rc.d目录下的初始化程序,因此我们可以把启动任务放到该目录下:

  1、init.d目录下都为可执行程序,他们其实是服务脚本,按照一定格式编写,Ubuntu在启动时会自动执行,类似Windows下的服务

  2、用root帐号登录,vi /etc/rc.d/init.d/mystart,追加如下内容:

#!/bin/bash
#
chkconfig:2345 80 05 --指定在哪几个级别执行,0一般指关机,
6指的是重启,其他为正常启动。80为启动的优先级,05为关闭的优先机
#description:mystart service
RETVAL=0
start(){ 
--启动服务的入口函数
echo 
-"mystart serive ..."
cd 
/home/test1
su test1 
-"python /home/test1/test.py"
}
stop(){ 
--关闭服务的入口函数
echo 
"mystart service is stoped..."
}
case $
1 in --使用case,可以进行交互式操作
start)
start
;;
stop)
stop
;;
esac
exit $RETVAL

     3、运行chmod +r /etc/rc.d/init.d/mystart,使之可直接执行

  4、运行chkconfig --add mystart,把该服务添加到配置当中

  5、运行chkconfig --list mystart,可以查看该服务进程的状态