linux下开机自启动应用程序

根据linux的开机启动流程,在加载内核后执行/sbin/init,然后根据运行级别i启动/etc/rci.d中的各个脚本,结束后启动个人化设定/etc/rc.d/rc.local脚本

应用程序的执行可以在/etc/rc.d/rc.local这个脚本文件中进行设置

在/home/temp/test目录下放置你的程序,这里以一个简单的程序为例,打开一个file文件,往里写一段话

程序代码如下:
#include
#include
#include
#include
int main()
{
char buf[]="hello,this is a test program!\n";
int fd;
int ret;
fd=open("/home/temp/test/file",O_RDWR|O_NONBLOCK);

ret=write(fd,buf,sizeof(buf)); 
return 0;
}

编译后生成可执行文件test,放在/home/temp/test,然后在该目录下创建一个空文件file,以便验证启动后程序是否被执行

修改/etc/rc.d/rc.local脚本文件

添加这么一段换:

echo "start my test program now.......... "
/home/temp/test/test
sleep 3
echo "test program starts OK"

保存后重启系统,查看启动信息,看是否有上面的话打印出来,启动后到/home/temp/test看file文件里是否有hello,this is a test program!这句话,有则程序启动成功。根据linux的开机启动流程,在加载内核后执行/sbin/init,然后根据运行级别i启动/etc/rci.d中的各个脚本,结束后启动个人化设定/etc/rc.d/rc.local脚本


应用程序的执行可以在/etc/rc.d/rc.local这个脚本文件中进行设置


在/home/temp/test目录下放置你的程序,这里以一个简单的程序为例,打开一个file文件,往里写一段话


程序代码如下:

#include

#include

#include

#include

int main()

{

char buf[]="hello,this is a test program!\n";

int fd;

int ret;

fd=open("/home/temp/test/file",O_RDWR|O_NONBLOCK);


ret=write(fd,buf,sizeof(buf)); 

return 0;

}


编译后生成可执行文件test,放在/home/temp/test,然后在该目录下创建一个空文件file,以便验证启动后程序是否被执行


修改/etc/rc.d/rc.local脚本文件


添加这么一段换:


echo "start my test program now.......... "

/home/temp/test/test

sleep 3

echo "test program starts OK"


保存后重启系统,查看启动信息,看是否有上面的话打印出来,启动后到/home/temp/test看file文件里是否有hello,this is a test program!这句话,有则程序启动成功。

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值